"Paramiko" is a combination of the Esperanto words for "paranoid" and "friend". It's a module for Python 2.7/3.4+ that implements the SSH2 protocol fo ...
"Paramiko" is a combination of the Esperanto words for "paranoid" and "friend". It's a module for Python 2.7/3.4+ that implements the SSH2 protocol for secure (encrypted and authenticated) connections to remote machines. Unlike SSL (aka TLS), SSH2 protocol does not require hierarchical certificates signed by a powerful central authority. You may know SSH2 as the protocol that replaced Telnet and rsh for secure access to remote shells, but the protocol also includes the ability to open arbitrary channels to remote services across the encrypted tunnel (this is how SFTP works, for example).
paramiko是一個遠程式控制制模塊,使用它可以很容易的再python中使用SSH執行命令和使用SFTP上傳或下載文件;而且paramiko直接與遠程系統交互,無需編寫服務端。
例一(實現一個簡單的SSH客戶端):
1 import paramiko 2 3 #實例化一個ssh客戶端實例 4 ssh = paramiko.SSHClient() 5 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 6 #連接遠程 --- 填入要連接的地址,埠,用戶,密碼 7 ssh.connect(hostname="192.168.56.50", port=22, username='libin', password='123456') 8 9 while True: 10 command = input(">>>:") 11 12 #exec_command()返回三個值:the stdin, stdout, and stderr of the executing command, as a 3-tuple 13 stdin, stdout, stderr = ssh.exec_command(command) 14 15 result = stdout.read() 16 error = stderr.read() 17 18 if result: 19 print(result.decode()) 20 else: 21 print(error.decode()) 22 #關閉連接 23 ssh.close()ssh_client
例二(實現文件的上傳和下載操作):
1 import paramiko 2 3 transport = paramiko.Transport('192.168.56.50', 22) 4 transport.connect(username='libin', password='123456') 5 #實例化一個SFTP客戶端實例 6 sftp = paramiko.SFTPClient.from_transport(transport) 7 8 #put('localpath', 'remotepath')上傳本地文件至伺服器 9 #sftp.put(r'E:\tempdownload\Sau_Authentication_Client_For_Windows_V6.82.exe', '/tmp/abc.exe') 10 #get('remotepath', 'localpath')將遠程文件下載至本地 11 sftp.get('/tmp/abc.exe', r'E:\tempdownload\abc.exe') 12 13 transport.close()sftp_client
為了連接安全,我們可以對上述兩例稍加改變,使其使用密鑰認證建立連接。
例四(以密鑰認證實現SSH):
1 import paramiko 2 3 #實例化一個ssh客戶端實例 4 ssh = paramiko.SSHClient() 5 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 6 #連接遠程 --- 填入要連接的地址,埠,用戶,密碼 7 8 #導入私鑰文件 9 private_key = paramiko.RSAKey.from_private_key_file('id_rsa_2048') 10 #連接遠程 11 ssh.connect(hostname="192.168.56.50", port=22, username='libin', pkey=private_key) #無需再填入用戶密碼 12 13 while True: 14 command = input(">>>:") 15 16 #exec_command()返回三個值:the stdin, stdout, and stderr of the executing command, as a 3-tuple 17 stdin, stdout, stderr = ssh.exec_command(command) 18 19 result = stdout.read() 20 error = stderr.read() 21 22 if result: 23 print(result.decode()) 24 else: 25 print(error.decode()) 26 #關閉連接 27 ssh.close()key_ssh_client
例五(以密鑰認證實現SFTP):
1 import paramiko 2 3 transport = paramiko.Transport('192.168.56.50', 22) 4 #導入私鑰文件 5 privat_key = paramiko.RSAKey.from_private_key_file('id_rsa_2048') 6 transport.connect(username='libin', pkey=privat_key) 7 #實例化一個SFTP客戶端實例 8 sftp = paramiko.SFTPClient.from_transport(transport) 9 10 #put('localpath', 'remotepath')上傳本地文件至伺服器 11 sftp.put(r'E:\tempdownload\test.exe', '/tmp/123.exe') 12 13 # # get('remotepath', 'localpath')將遠程文件下載至本地 14 # sftp.get('/tmp/abc.exe', r'E:\tempdownload\abc.exe') 15 transport.close()key_sftp_client