从Netmiko文档来看,我应该得到干净的输出,但是从我所做的来看,它似乎甚至显示了n来表示新的一行。是不是我的代码中缺少了什么?
import getpass
from netmiko import (
ConnectHandler,
NetmikoTimeoutException,
NetmikoAuthenticationException,
)
def send_command(device, commands):
result = {}
try:
with ConnectHandler(**device) as ssh:
ssh.enable()
for command in commands:
output = ssh.send_command(command)
result[command] = output
return result
except (NetmikoTimeoutException, NetmikoAuthenticationException) as error:
print(error)
if __name__ == "__main__":
username = input("Username: ")
passwd = getpass.getpass()
device = {
"device_type": "cisco_xr",
"host": "router1",
"username": username,
"password": passwd,
}
result = send_command(device, "show ip int brief")
print(result)
输出如下:
{'show ip int brief': 'nTue Jul 19 07:27:36.879 BSTnnInterface IP-Address Status Protocol Vrf-Name nTenGigE0/3/0/0 unassigned Down Down default nTenGigE0/3/0/1 unassigned Down Down default nTenGigE0/3/0/2 unassigned Shutdown Down default nTenGigE0/3/0/2.101 unassigned Shutdown Down default nTenGigE0/3/0/3 unassigned Down Down default nTenGigE0/3/0/4 unassigned Up Up default nTenGigE0/3/0/5 unassigned Down Down default nTenGigE0/3/0/5.1 unassigned Down Down default nTenGigE0/3/0/5.2 unassigned Down Down default nTenGigE0/3/0/5.4 unassigned Shutdown Down default nTenGigE0/3/0/5.6 unassigned Down Down default nTenGigE0/3/0/6 unassigned Up Up default nTenGigE0/3/0/7 unassigned Up Up default nTenGigE0/3/0/8 unassigned Up Up default nTenGigE0/3/0/9 unassigned Down Down default nTenGigE0/3/0/10 unassigned Shutdown Down default nTenGigE0/3/0/11 unassigned Down Down default nTenGigE0/3/0/12 unassigned Shutdown Down default nTenGigE0/3/0/12.1 unassigned Shutdown Down default '}
我认为如果没有n,它将被很好地格式化,但它不应该这样输出吗?我如何从输出中摆脱可见的换行符?
多谢
使用下面的方法,您可以运行"show ip int brief"命令同时在多个设备上执行,并正确打印输出。此方法还使用提示符特性。
import time
from multiprocessing.dummy import Pool as ThreadPool
from netmiko import Netmiko
#You should put all the ip you want to enter the config in the notepad below
f_2 = open("ip_list.txt","r")
ip_list = f_2.readlines()
f_2.close()
#To see devices with access problems, you must create the following notepad
f_3 = open("connection was not established.txt","w")
def ssh(nodeip):
try:
cisco = {
'device_type': 'cisco_ios', 'ip': nodeip, 'username':
XXXX, 'password': XXXX, }
net_connect = Netmiko(**cisco)
print(nodeip.strip() + " " + "success connection")
except Exception as e:
print (e)
f_3.write(nodeip.strip() + "n")
return
prompt_cisco_fnk = net_connect.find_prompt()
hostname_fnk = prompt_cisco_fnk.strip("<" + ">")
print(hostname_fnk) #you can see the prompt feature
net_connect.send_command_timing("enable")
output = net_connect.send_command_timing("config")
print("config mode entered")
net_connect.send_command_timing("show ip int brief")
print("config done")
net_connect.disconnect()
myPool = ThreadPool(200) #With the Threading method, you can enter devices very quickly at the same time. It is 200 variables.
result = myPool.map(ssh,ip_list) #ssh def is your function name and ip_list is the notepad where you enter the device ips.