Python 操作 ssh


Author: yifei / Created: June 22, 2018, 6:21 p.m. / Modified: June 22, 2018, 6:22 p.m. / Edit

import paramiko
ip='server ip'
port=22
username='username'
password='password'
cmd='some useful command' 
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)
stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)

有任何问题可以发邮件到 kongyifei (at) gmail.com 讨论