Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Netmiko错误:UnicodeDecodeError:&x27;ascii';编解码器可以';t解码位置0处的字节0xff:序号不在范围内(128)_Python - Fatal编程技术网

Python Netmiko错误:UnicodeDecodeError:&x27;ascii';编解码器可以';t解码位置0处的字节0xff:序号不在范围内(128)

Python Netmiko错误:UnicodeDecodeError:&x27;ascii';编解码器可以';t解码位置0处的字节0xff:序号不在范围内(128),python,Python,我在尝试从本质上获取一个配置文件,将其剪切成几行,将其附加到一个列表中,并尝试通过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

我在尝试从本质上获取一个配置文件,将其剪切成几行,将其附加到一个列表中,并尝试通过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'、'no ip domain lookup\r\n'、'ip routing\r\n'、'ipv6单播路由\r\n'、'!\r\n'、'cdp run\r\n'、line con 0\r\n'、'exec timeout 0\r\n'、'logging synchronous\r\n',“特权级别15\r\n',“无登录”!\r\n',“行vty 0 4\r\n',“特权级别15\r\n',“无登录”!\r\n',“!\r\n',“\r\n',”!\r\n',“\r\n',“!\n',“结束\n']

我以前运行过这个,不记得遇到过这个问题,所以我不确定我遗漏了什么。这可能也会有帮助(从Linux显示的文件类型)


好的,这是我如何修复它的

我记得有一条规则,在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'、'no ip域查找\r\n'、'ip路由\r\n'、'ipv6单播路由\r\n'、'、'!\r\n'、'r\n'、'r\n'、'r\n'、'cdp run\r\n运行\r\n'、'line con\r\n'、'0\r\r\ng同步\r\n'、“权限级别15\r\n'、“无登录\r\n'、!\r\n'、“行vty 0 4\r\n'、“权限级别15\r\n'、“无登录\r\n'、”!\r\n'、“\r\n'、”!\r\n'、“\r\n'、”结束\r\n']

'rb'?为什么要使用此方法?使用'r'和'rb'进行了尝试。结果相同。不匹配包括文本和文件编码,在linux上创建此文件,不要触摸编码系统(在windows上创建文件并在linux上使用,不要这样做,我们讨论的是相关
txt
文件的前4个字节)。
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()