欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python利用腳本輕松實(shí)現(xiàn)ssh免密登陸配置

 更新時(shí)間:2023年12月12日 17:01:20   作者:碧藍(lán)幻想  
這篇文章主要為大家詳細(xì)介紹了python如何利用腳本輕松實(shí)現(xiàn)ssh免密登陸配置,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.安裝python和pip包

yum install -y epel-release
yum install -y python python-pip

2.pip安裝依賴庫(kù)

pip install pexpect     # 此庫(kù)用相當(dāng)于linux中的expect命令

3.完整腳本

# coding=UTF-8
import sys,os,pexpect,subprocess
 
host_controller="192.168.174.150"                            # 控制節(jié)點(diǎn)IP地址
host_addresses=["192.168.174.151","192.168.174.152"]           # 客戶端們的IP地址
host_domains=["client1","client2"]                          # 客戶端們的域名
host_username="root"                                         # ssh連接的用戶,控制端的用戶為root
host_passwd="110119"                                         # ssh連接的用戶密碼
 
 
# 本地創(chuàng)建ssh公鑰
if os.path.exists("/root/.ssh/id_rsa.pub") == True:
	print("\033[32m"+"ssh公鑰已創(chuàng)建"+"\033[0m")                # 輸出綠色字體
else:
	print("\033[32m"+"ssh公鑰未創(chuàng)建,開始創(chuàng)建"+"\033[0m")
	child = pexpect.spawn('ssh-keygen -t rsa -b 1024')
	child.expect('Enter file in which to save the key')
	child.sendline('')
	child.expect('Enter passphrase')
	child.sendline('')
	child.expect('Enter same passphrase again')
	child.sendline('')
 
	child.expect(pexpect.EOF)               # 用于等待子進(jìn)程的結(jié)束
	print(child.before.decode())            # 等待命令執(zhí)行完畢并打印輸出信息
	print("\033[32m" + "ssh公鑰已創(chuàng)建" + "\033[0m")
	print("\n")
 
 
# 向被控主機(jī)添加公鑰的方法
def add_ssh_public_key_client(address,username,password):
	print("\033[32m"+"{}正在被添加公鑰".format(address)+"\033[0m")
	# BatchMode=yes:表示使SSH在連接過程中不會(huì)提示輸入密碼,而直接嘗試免密連接,-o ConnectTimeout=5:表示限制連接超時(shí)時(shí)間為5秒
	public_key_flag=os.system("ssh {}@{} -o BatchMode=yes -o ConnectTimeout=5 'exit'".format(username,address))
	if public_key_flag== 0:
		print("\033[32m" + "{}已經(jīng)可以ssh連接".format(address) + "\033[0m")
		return
	child = pexpect.spawn('ssh-copy-id -i /root/.ssh/id_rsa.pub {}@{}'.format(username,address))
	try:
		child.expect('Are you sure you want to continue connecting')
	except pexpect.TIMEOUT:       # 如果try塊中的咨詢超時(shí)5秒沒有出現(xiàn)就會(huì)出現(xiàn)異常pexpect.TIMEOUT
		print("\033[32m"+"{}已經(jīng)不是首次ssh連接了".format(address)+"\033[0m")
	else:                         # 是否回答咨詢yes
		child.sendline('yes')
	finally:
		child.expect('password')
		child.sendline(password)
	child.expect(pexpect.EOF)               # 用于等待子進(jìn)程的結(jié)束
	print(child.before.decode())            # 等待命令執(zhí)行完畢并打印輸出信息
# 測(cè)試ssh連接的方法
def test_ssh_connection(all_flag,address,username):
	print("\033[32m" + "{}測(cè)試是否可以ssh連接".format(address) + "\033[0m")
	flag=os.system('ssh {}@{} -o ConnectTimeout=5 "exit"'.format(username,address))
	if flag==0:
		print("\033[32m" + "Success: {}可以ssh免密連接".format(address) + "\033[0m")
	else:
		print("\033[1;31m" + "Failed: {}ssh免密連接失敗".format(address) + "\033[0m")     # 輸出紅色字體
		all_flag=1
	return all_flag
 
# 本地的密鑰開始加入被控制主機(jī)
for i in range(0, len(host_addresses)):
	add_ssh_public_key_client(host_addresses[i],host_username,host_passwd)
	print("\n")
# 測(cè)試ssh連接
for i in range(0, len(host_addresses)):
	final_flag=test_ssh_connection(0,host_addresses[i],host_username)
if final_flag ==1:
	sys.exit("ssh測(cè)試失敗,請(qǐng)檢查!")
else:
	print("\033[32m" + "Success: 全部可以ssh免密連接" + "\033[0m")
print("\n")

4.執(zhí)行結(jié)果

[root@server ~]# python ansible_auto.py
ssh公鑰未創(chuàng)建,開始創(chuàng)建

Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:RvdYf1KOFDyKBuEB6DbFQdfNP9aBPs1/0vIFnutEj5E root@server
The key's randomart image is:
+---[RSA 1024]----+
|     ++o+o o .o  |
|    . oo... o.oo |
|   . .  o...oo+oo|
|    +  . .o+.==B.|
|   . .  S.. .oE=+|
|       .     .=*=|
|              o=+|
|             .. .|
|             ..  |
+----[SHA256]-----+
 
ssh公鑰已創(chuàng)建
 
 
192.168.174.151正在被添加公鑰

 
Number of key(s) added: 1
 
Now try logging into the machine, with:   "ssh 'root@192.168.174.151'"
and check to make sure that only the key(s) you wanted were added.
 
 
 
 
192.168.174.152正在被添加公鑰

 
Number of key(s) added: 1
 
Now try logging into the machine, with:   "ssh 'root@192.168.174.152'"
and check to make sure that only the key(s) you wanted were added.
 
 
 
 
Success: 192.168.174.151可以ssh免密連接
Success: 全部可以ssh免密連接
 
 
Success: 192.168.174.152可以ssh免密連接
Success: 全部可以ssh免密連接

到此這篇關(guān)于python利用腳本輕松實(shí)現(xiàn)ssh免密登陸配置的文章就介紹到這了,更多相關(guān)python ssh免密登陸配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在RedHat系Linux上部署Python的Celery框架的教程

    在RedHat系Linux上部署Python的Celery框架的教程

    這篇文章主要介紹了在RedHat系Linux上部署Python的Celery框架的教程, Celery是一個(gè)并行分布框架,擁有良好的I/O性能,需要的朋友可以參考下
    2015-04-04
  • 如何把python項(xiàng)目部署到linux服務(wù)器

    如何把python項(xiàng)目部署到linux服務(wù)器

    這篇文章主要介紹了如何把python項(xiàng)目部署到linux服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • python使用response.read()接收json數(shù)據(jù)的實(shí)例

    python使用response.read()接收json數(shù)據(jù)的實(shí)例

    今天小編就為大家分享一篇python使用response.read()接收json數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python中自定義函數(shù)的教程

    Python中自定義函數(shù)的教程

    這篇文章主要介紹了簡(jiǎn)單講解Python中內(nèi)置函數(shù)的使用,函數(shù)的使用是Python學(xué)習(xí)當(dāng)中的基本功,需要的朋友可以參考下
    2015-04-04
  • python的rllib庫(kù)你了解嗎

    python的rllib庫(kù)你了解嗎

    這篇文章主要介紹了python urllib庫(kù)的使用,幫助大家更好的利用python學(xué)習(xí)爬蟲,感興趣的朋友可以了解下,希望能夠給你帶來幫助
    2021-11-11
  • python序列類型種類詳解

    python序列類型種類詳解

    這篇文章主要介紹了python序列類型種類詳解,需要的朋友們可以學(xué)習(xí)參考下。
    2020-02-02
  • Python 通過URL打開圖片實(shí)例詳解

    Python 通過URL打開圖片實(shí)例詳解

    這篇文章主要介紹了Python 通過URL打開圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Pytorch運(yùn)行過程中解決出現(xiàn)內(nèi)存不足的問題

    Pytorch運(yùn)行過程中解決出現(xiàn)內(nèi)存不足的問題

    內(nèi)存不足是很多人感到頭疼的問題,本文主要介紹了Pytorch運(yùn)行過程中解決出現(xiàn)內(nèi)存不足的問題,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • Pytorch 使用不同版本的cuda的方法步驟

    Pytorch 使用不同版本的cuda的方法步驟

    這篇文章主要介紹了Pytorch 使用不同版本的cuda的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • pytorch如何實(shí)現(xiàn)多個(gè)矩陣拼接

    pytorch如何實(shí)現(xiàn)多個(gè)矩陣拼接

    這篇文章主要介紹了pytorch如何實(shí)現(xiàn)多個(gè)矩陣拼接問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評(píng)論