第三方模块paramiko的使用

  "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

 

转载于:https://www.cnblogs.com/God-Li/p/7695832.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值