有人可以帮我解决执行命令后发生超时的原因。我正在尝试SSH到机器并执行命令。我想存储执行命令的结果。我提到了paramiko-timeout,但我没有得到预期的结果。
import traceback
try:
import paramiko
except ImportError:
raise Exception("Please install paramiko , pip install paramiko")
import pexpect
from paramiko_expect import SSHClientInteraction
def main():
HOSTNAME = "minion5.net"
USERNAME = "root"
PASSWORD = "help@432"
PROMPT = "root@*:~\$\s+"
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=HOSTNAME, username=USERNAME, password=PASSWORD)
with SSHClientInteraction(client, timeout=20, display=True) as interact:
interact.expect(PROMPT)
interact.send('hostname -f')
interact.expect(PROMPT)
interact.send('exit')
cmd_output = interact.current_output_clean
print cmd_output
except Exception:
traceback.print_exc()
finally:
try:
client.close()
except:
pass
if __name__ == '__main__':
main()
以下是输出
[root@btpvm0740 paramiko-tests]# python sa-test.py
Last login: Thu Jun 14 11:13:10 2018 from btpvm0740.net
[root@minion5 ~]# Traceback (most recent call last):
File "sa-test.py", line 25, in main
interact.expect(PROMPT)
File "/usr/lib/python2.7/site-packages/paramiko_expect.py", line 144, in expect
current_buffer = self.channel.recv(self.buffer_size)
File "/usr/lib/python2.7/site-packages/paramiko/channel.py", line 685, in recv
raise socket.timeout()
timeout