简体   繁体   中英

Netmiko error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

I received after attempting to essentially take a configuration file, cut it into lines, append it to a list, and attempt to push it to a router via SSH. Code snippets below.

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()

The output for the file contents after appending to the list looks like this:

['!\\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']

I have ran this in the past and don't remember encountering this issue, so I'm not sure what I'm missing. This may help as well (File type shown from Linux)

file IOSV4.txt
IOSV4.txt: ASCII text, with CRLF, LF line terminators

Alright, here is how I fixed it.

I recall there being a rule where files edited in Linux/Unix environments get a '\\r\\n' at the end of line, whereas Windows uses '\\n', so it had to have been edited in multiple environments. Here is how I dealt with the above error knowing this:

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()

And the desired result:

['!\\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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM