python利用腳本輕松實現(xiàn)ssh免密登陸配置
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é)點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) # 用于等待子進程的結(jié)束
print(child.before.decode()) # 等待命令執(zhí)行完畢并打印輸出信息
print("\033[32m" + "ssh公鑰已創(chuàng)建" + "\033[0m")
print("\n")
# 向被控主機添加公鑰的方法
def add_ssh_public_key_client(address,username,password):
print("\033[32m"+"{}正在被添加公鑰".format(address)+"\033[0m")
# BatchMode=yes:表示使SSH在連接過程中不會提示輸入密碼,而直接嘗試免密連接,-o ConnectTimeout=5:表示限制連接超時時間為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塊中的咨詢超時5秒沒有出現(xiàn)就會出現(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) # 用于等待子進程的結(jié)束
print(child.before.decode()) # 等待命令執(zhí)行完畢并打印輸出信息
# 測試ssh連接的方法
def test_ssh_connection(all_flag,address,username):
print("\033[32m" + "{}測試是否可以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
# 本地的密鑰開始加入被控制主機
for i in range(0, len(host_addresses)):
add_ssh_public_key_client(host_addresses[i],host_username,host_passwd)
print("\n")
# 測試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測試失敗,請檢查!")
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利用腳本輕松實現(xiàn)ssh免密登陸配置的文章就介紹到這了,更多相關(guān)python ssh免密登陸配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在RedHat系Linux上部署Python的Celery框架的教程
這篇文章主要介紹了在RedHat系Linux上部署Python的Celery框架的教程, Celery是一個并行分布框架,擁有良好的I/O性能,需要的朋友可以參考下2015-04-04
python使用response.read()接收json數(shù)據(jù)的實例
今天小編就為大家分享一篇python使用response.read()接收json數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Pytorch運行過程中解決出現(xiàn)內(nèi)存不足的問題
內(nèi)存不足是很多人感到頭疼的問題,本文主要介紹了Pytorch運行過程中解決出現(xiàn)內(nèi)存不足的問題,具有一定的參考價值,感興趣的可以了解一下2024-02-02

