paramiko(一)使用
paramiko(1)使用
ssh连接
>>> import paramiko >>> paramiko.util.log_to_file('paramiko.log') >>> ssh=paramiko.SSHClient() >>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) >>> ssh.connect(hostname='192.168.0.88',username='root',password='123456',timeout=5) >>> stdin,stdout,stderr=ssh.exec_command('echo "OK"') >>> out=stdout.read(),stderr.read() >>> print out ('OK\n', '') >>> ssh.close()
传文件
>>> t=paramiko.Transport(('192.168.0.88',22)) >>> t.connect(username='root',password='123456') >>> sftp=paramiko.SFTPClient.from_transport(t) >>> sftp.get('/etc/passwd','passwd') >>> sftp.put('/root/passwd','/root/passwd1') <SFTPAttributes: [ size=926 uid=0 gid=0 mode=0100644 atime=1472969310 mtime=1472969310 ]>
传文件为短连接