首頁 > 軟體

Python的paramiko模組SSH自動登入Linux系統進行操作

2020-06-16 17:23:14

1). Linux系統首先要開啟SSH服務:service ssh status

如果沒安裝的話,則要:apt-get install openssh-server

service ssh restart

2). pip install paramiko


example 1:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.80.139', username = 'allen', password = 'allen', timeout = 300)
cmd = 'cd'
stdin, stdout, stderr = ssh.exec_command(cmd)
cmd = 'ls Python'
stdin, stdout, stderr = ssh.exec_command(cmd)
print stdout.read()
#for std in stdout.readlines():
#    print std
ssh.close()

如果執行此指令碼後“Multibackend cannot be initialized with no backends. If you are seeing this error when trying to use default_backend() please try uninstalling and reinstalling cryptography." 這個錯誤,那麼就:
pip uninstall paramiko
pip install paramiko==1.17參考:http://stackoverflow.com/questions/37135521/paramiko-transport-throws-runtime-valueerror-while-connecting-to-remote-server-u

指令碼二在遠端伺服器上執行相應命令
import sys
import paramiko
 
hostname = sys.argv[1]
command = " ".join(sys.argv[2:])
port=22
username="allen"
password="allen"
if __name__=="__main__":
    s=paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname,port,username,password)
    stdin,stdout,sterr=s.exec_command(command)
    print stdout.read()
    s.close()

指令碼三:管理多台伺服器:批次查詢ip列表中對應伺服器的磁碟使用情況
import paramiko
port = 22
username = "allen"
file=open("ip.list")
for line in file:
    hostname = str(line.split("t")[1])
    password = str(line.split("t")[4]).strip()
    print "##########################",hostname,"########################"
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname, port, username, password)
    stdin,stdout,sterr = s.exec_command("df -Th")
    print stdout.read()
    s.close()
file.close()

ip.list內容:
dx 192.168.0.1 22 root loveyou

指令碼四:類似於指令碼二,在所有遠端伺服器上執行相應命令

import paramiko
import sys
port=22
username="root"
command = " ".join(sys.argv[1:])
file=open("ip.list")
for line in file:
    hostname=str(line.split("t")[1])
    password=str(line.split("t")[4]).strip()
    print "##################",hostname,"######################"
    s=paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname,port,username,password)
    stdin,stdout,sterr=s.exec_command(command)
    print stdout.read()
    s.close()
file.close()

下面是通過ssh的dsa或rsa公鑰驗證批次登入伺服器執行命令:
import paramiko
import sys, os
port = 22
username = "root"
key_file = "~/.ssh/authorized_keys"
know_host = "/home/larry/.ssh/known_hosts"
command = " ".join(sys.argv[1:]) ####獲取命令列引數
file = open("ip.list")
for line in file:
    hostname = str(line.split(" ")[1]) ####擷取ip欄位
    print "#####################################",hostname,"###############################################"
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.load_system_host_keys(know_host)
    s.connect(hostname, port, username, key_file)
    stdin, stdout, sterr = s.exec_command(command)
    print stdout.read().strip()
    s.close()
file.close()

零基礎如何入門Python http://www.linuxidc.com/Linux/2016-10/136485.htm

Ubuntu 14.04安裝Python 3.3.5  http://www.linuxidc.com/Linux/2014-05/101481.htm

CentOS上原始碼安裝Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

Ubuntu 14.04下Python資料處理環境搭建  http://www.linuxidc.com/Linux/2017-01/139568.htm

《Python核心程式設計 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視訊+程式碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

在CentOS 6.5上安裝Python2.7  http://www.linuxidc.com/Linux/2016-10/136206.htm

在Ubuntu下用Python搭建桌面演算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-01/139974.htm


IT145.com E-mail:sddin#qq.com