paramiko 模块 ---- python2.7

模拟远程执行命令: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import paramiko
 
#设置日志记录
paramiko.util.log_to_file('/tmp/test')
 
#建立连接
ssh=paramiko.SSHClient()
 
#缺失host_knows时的处理方法
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
#连接远程客户机器
ssh.connect('10.1.6.190',port=22,username='root',password='password',compress=True)
 
#获取远程命令执行结果
stdin, stdout, stderr = ssh.exec_command('hostname;uptime')
print stdout.read()# 其中其他各类的提示在stderr,stdin中(如错误提示)
 
#输出执行结果
ssh.close()
模拟远程文件传输:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import paramiko
 
#建立一个加密的管道
scp=paramiko.Transport(('10.1.6.190',22))
 
#建立连接
scp.connect(username='root',password='password')
 
#建立一个sftp客户端对象,通过ssh transport操作远程文件
sftp=paramiko.SFTPClient.from_transport(scp)
 
#Copy a remote file (remotepath) from the SFTP server to the local host
sftp.get('/root/debian7','/tmp/debian7')
 
#Copy a local file (localpath) to the SFTP server as remotepath
sftp.put('/root/crash-6.1.6.tar.gz','/tmp/crash-6.1.6.tar.gz')
 
scp.close()
 
 
 
未完待续……
 
 
 
 
 
原文地址:https://www.cnblogs.com/wipy/p/4094308.html