最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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安裝依賴庫

pip install pexpect     # 此庫用相當(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)文章

  • Django Admin后臺(tái)模型列表頁面如何添加自定義操作按鈕

    Django Admin后臺(tái)模型列表頁面如何添加自定義操作按鈕

    這篇文章主要介紹了Django Admin后臺(tái)模型列表頁面如何添加自定義操作按鈕,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 淺談numpy數(shù)組的幾種排序方式

    淺談numpy數(shù)組的幾種排序方式

    這篇文章主要介紹了淺談numpy數(shù)組的幾種排序方式,涉及對(duì)numpy的簡單介紹和創(chuàng)建數(shù)組的方式,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • python3之讀取redis數(shù)據(jù)帶有‘b’的問題

    python3之讀取redis數(shù)據(jù)帶有‘b’的問題

    這篇文章主要介紹了python3之讀取redis數(shù)據(jù)帶有‘b’的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python圖像平滑處理原理

    python圖像平滑處理原理

    這篇文章主要介紹了python圖像平滑處理原理,圖像濾波是圖像處理和計(jì)算機(jī)視覺中最常用、最基本的操作,文章基于python的相關(guān)資料展開詳細(xì)的內(nèi)容需要的小伙伴可以參考一下
    2022-06-06
  • 淺談python多進(jìn)程共享變量Value的使用tips

    淺談python多進(jìn)程共享變量Value的使用tips

    今天小編就為大家分享一篇淺談python多進(jìn)程共享變量Value的使用tips,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python中pandas庫中DataFrame對(duì)行和列的操作使用方法示例

    python中pandas庫中DataFrame對(duì)行和列的操作使用方法示例

    這篇文章主要介紹了python中pandas庫中DataFrame對(duì)行和列的操作使用方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • pandas實(shí)現(xiàn)數(shù)據(jù)讀取&清洗&分析的項(xiàng)目實(shí)踐

    pandas實(shí)現(xiàn)數(shù)據(jù)讀取&清洗&分析的項(xiàng)目實(shí)踐

    近期因工作需要,需對(duì)幾十萬條商品和訂單數(shù)據(jù)進(jìn)行初步的數(shù)據(jù)分析,本文主要pandas實(shí)現(xiàn)數(shù)據(jù)讀取&清洗&分析的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-05-05
  • python命令行參數(shù)sys.argv使用示例

    python命令行參數(shù)sys.argv使用示例

    這篇文章主要介紹了python命令行參數(shù)sys.argv使用示例,大家參考使用吧
    2014-01-01
  • python分析網(wǎng)頁上所有超鏈接的方法

    python分析網(wǎng)頁上所有超鏈接的方法

    這篇文章主要介紹了python分析網(wǎng)頁上所有超鏈接的方法,涉及Python使用urllib模塊操作頁面超鏈接的技巧,需要的朋友可以參考下
    2015-05-05
  • python collections模塊的使用

    python collections模塊的使用

    這篇文章主要介紹了python collections模塊的使用,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-10-10

最新評(píng)論

安化县| 繁峙县| 嘉黎县| 清水县| 共和县| 辛集市| 涿鹿县| 阿尔山市| 沾益县| 新密市| 云安县| 遂川县| 安平县| 枣强县| 西安市| 武定县| 镇康县| 昆明市| 韩城市| 江安县| 澳门| 大姚县| 杂多县| 澎湖县| 喀喇沁旗| 旬阳县| 邵东县| 阿克| 巴林左旗| 松江区| 南召县| 海丰县| 万州区| 迁西县| 汝南县| 额尔古纳市| 什邡市| 灵璧县| 黄浦区| 富平县| 德州市|