python ssh

python 远程执行 ssh 命令


@staticmethod
def execute_full(ip, user, password, cmd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
tout = 30
try:
ssh.connect(hostname=ip, username=user, password=password, timeout=tout, compress=True, look_for_keys=False, allow_agent=False)
except (socket.error, paramiko.AuthenticationException, paramiko.SSHException) as message:
print "ERROR: SSH connection to "+ip+" failed: " + str(message)
sys.exit(1)

stdin, stdout, ssh_stderr = ssh.exec_command(cmd)
stdout.flush()
out = stdout.read()
print out
ssh.close()
return out

posted on 2017-01-11 10:06  三分钟热血  阅读(241)  评论(0)    收藏  举报

导航