"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()