2017-09-28 7 views
-1

本質的に構成ファイルを取り込み、行にカットしてリストに追加したSSH経由でルーターにプッシュしようとします。以下のコードスニペット。Netmikoエラー:UnicodeDecodeError: 'ascii'コーデックは、位置0のバイト0xffをデコードできません:序数が範囲内にありません(128)

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

は、私が過去にこれを実行していると、この問題が発生した覚えていないので、私は」:

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

リストに追加した後、ファイルの内容のための出力は、次のようになります私は何が欠けているかわからない。これも助けになるかもしれません(Linuxから見たファイルタイプ)

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

は 'rb'?なぜこの方法を使うのですか? – dsgdfg

+0

「r」と「rb」で試してみてください。同じ結果。 – OfWolfAndMan

+1

include_textとfile_encodingは一致しません。linuxでこのファイルを作成し、エンコードシステムには触れないでください(Windowsではファイルを作成し、Linuxではこれを使用しないでください、関連する 'txt'ファイルの最初の4バイトについて)。 – dsgdfg

答えて

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']

関連する問題