多次打印到文件

时间:2018-04-29 07:15:49

标签: python-3.x ssh

我是python 3.6的新手,我对使用ssh命令多次打印到文件有疑问。

我正在尝试在创建ssh会话后打印多个show命令。 建立ssh会话并且第一个show命令正常工作(打印到创建的文件)。 任何其他命令都不能作为第一个命令。

我的代码:

import paramiko 
host = '192.168.100.1'
user = 'MyUser'
secret = 'MyPass'
port = 22
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Set policy to use when connecting to servers without a known host key
ssh.connect(hostname=host, username=user, password=secret, port=port)
file = open("output/" + host + ".txt", "w")
stdin, stdout, stderr = ssh.exec_command('sh ver')
output = stdout.readlines()
file.write(''.join(output))
stdin.flush()
stdin, stdout, stderr = ssh.exec_command('sh arp')
output = stdout.readlines()
file.write(''.join(output))
file.close()

我想得到帮助, 感谢。

0 个答案:

没有答案