netmiko: How to sends get command output in a new line

Deadly 提交于 2020-04-30 07:13:06

问题


This is the script

from netmiko import ConnectHandler

cisco_device = {
    'device_type': 'cisco_ios',
    'ip': 'Router1',
    'username': 'u',
    'password': 'p'
}

net_connect = ConnectHandler(**cisco_device)

cmd = ['show clock', 'show version | include IOS']
output = ''
for command in cmd:
    output += net_connect.send_command(command)

print(output)

Output

As you can see, the output is displayed in a single line

user@linux:~$ python script.py 

*00:22:10.927 UTC Fri Mar 1 2002Cisco IOS Software, 3700 Software (C3725-ADVENTERPRISEK9-M), Version 12.4(15)T7, RELEASE SOFTWARE (fc3)
user@linux:~$ 

Desired Output

I would like to separate each output in a new line

*00:23:31.943 UTC Fri Mar 1 2002
Cisco IOS Software, 3700 Software (C3725-ADVENTERPRISEK9-M), Version 12.4(15)T7, RELEASE 

回答1:


output = []
for command in cmd:
    output.append(net_connect.send_command(command))
    print(output[-1])



回答2:


Other way would be appending newline "\n" on each line

output = ''
for command in cmd:
    output += net_connect.send_command(command) + "\n"

print(output)


来源:https://stackoverflow.com/questions/61222029/netmiko-how-to-sends-get-command-output-in-a-new-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!