client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
hostname='192.168.40.111',
port=22,
username='root',
password='yourpass'
)
stdin, stdout, stderr = client.exec_command('ssh 192.168.40.158 "df -h"', get_pty=True)
while not stdout.channel.exit_status_ready():
temp = stdout.channel.recv(1024)
result = temp.decode('utf-8').strip('\r\n')# 获取数据 if "continue connecting" in result: # 可能遇到询问是否添加指纹 stdout.channel.send("yes\n") # 自动推送yes if "password:" in result: # 可能遇到询问密码 password_cmd = "%s\n" % yourpass # 指定推送密码(第二台服务器的密码) stdout.channel.send(password_cmd) else: print(result)if stdout.channel.exit_status_ready(): # 这里需要单独再判断一次,否则读取的数据可能少一部分 temp = stdout.channel.recv(1024) result = temp.decode('utf-8').strip('\r\n') print(result)
break