"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()
例二(实现文件的上传和下载操作):
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()
为了连接安全,我们可以对上述两例稍加改变,使其使用密钥认证建立连接。
例四(以密钥认证实现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()
例五(以密钥认证实现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()