我在尝试基本上获取配置文件,将其剪切成行,将其附加到列表并尝试通过SSH将其推送到路由器后收到。代码片段如下。
device_ip = "192.168.1.5"
selected_cmd_file = open('{}.txt'.format("Router1"), 'rb')
print("[+] Pushing scenario configuration for device {}.".format("Router1))
command_set = []
selected_cmd_file.seek(0)
for each_line in selected_cmd_file.readlines():
command_set.append(each_line)
net_connect = ConnectHandler(device_type = device, ip = device_ip, username = radiususer, password = radiuspass)
output = net_connect.send_config_set(command_set)
net_connect.disconnect()
附加到列表后文件内容的输出如下所示:
['!\n', 'version 15.6\n', '!\n', 'enable\r\n', 'configure terminal\r\n', 'no service timestamps debug uptime\r\n', 'no service timestamps log uptime\r\n', '!\r\n', 'hostname IOSV4\r\n', '!\r\n', 'no ip domain lookup\r\n', 'ip routing\r\n', 'ipv6 unicast-routing\r\n', '!\r\n', 'cdp run\r\n',line con 0\r\n', ' exec-timeout 0 0\r\n', ' logging synchronous\r\n', ' privilege level 15\r\n', ' no login\r\n', '!\r\n', 'line vty 0 4\r\n', ' privilege level 15\r\n', ' no login\r\n', '!\r\n', 'end\r\n', '\r\n', '\n', '!\n', 'end\n']
我过去曾经这样做,并且不记得遇到过这个问题,所以我不确定我错过了什么。这也可能有所帮助(从Linux显示的文件类型)
file IOSV4.txt
IOSV4.txt: ASCII text, with CRLF, LF line terminators
答案 0 :(得分:0)
好的,这是我修复它的方法。
我记得有一条规则,在Linux / Unix环境中编辑的文件会得到一个' \ r \ n'在行尾,而Windows使用' \ n',因此必须在多个环境中进行编辑。以下是我如何处理上述错误:
device_ip = "192.168.1.5"
selected_cmd_file = open('{}.txt'.format("Router1"), 'rb')
print("[+] Pushing scenario configuration for device {}.".format("Router1))
command_set = []
selected_cmd_file.seek(0)
for each_line in selected_cmd_file.readlines():
if '\r' not in each_line:
each_line = each_line.strip('\n')
each_line = ("{}\r\n".format(each_line))
command_set.append(each_line)
else:
command_set.append(each_line)
net_connect = ConnectHandler(device_type = device, ip = device_ip, username = radiususer, password = radiuspass)
output = net_connect.send_config_set(command_set)
net_connect.disconnect()
期望的结果:
['!\r\n', 'version 15.6\r\n', '!\r\n', 'enable\r\n', 'configure terminal\r\n', 'no service timestamps debug uptime\r\n', 'no service timestamps log uptime\r\n', '!\r\n', 'hostname IOSV4\r\n', '!\r\n', 'no ip domain lookup\r\n', 'ip routing\r\n', 'ipv6 unicast-routing\r\n', '!\r\n', 'cdp run\r\n', 'line con 0\r\n', ' exec-timeout 0 0\r\n', ' logging synchronous\r\n', ' privilege level 15\r\n', ' no login\r\n', '!\r\n', 'line vty 0 4\r\n', ' privilege level 15\r\n', ' no login\r\n', '!\r\n', 'end\r\n', '\r\n', '\r\n', '!\r\n', 'end\r\n']