前提・実現したいこと
pythonを使用してtelnet接続を行い、コマンドの実行結果を取得するプログラムを作成しようとしています。
発生している問題・エラーメッセージ
実行結果の取得はできたのですが、改行処理がうまく取得できません。本来は下記のようなデータを取得できるはずなのですが
hogehoge hogehoge ....
下記のように改行処理が\r\nがそのまま出てきてしまいます。
hogehoge \r\n hogehoge\r\n
実行しているプログラムのソースは以下のようなものです。
python
1import telnetlib 2 3HOST = "xxx.xxx.xxx.xxx" 4user = b"test" 5password = b"test" 6 7tn = telnetlib.Telnet(HOST) 8 9tn.read_until(b"login:") 10tn.write(user + b"\n") 11if password: 12 tn.read_until(b"Password:") 13 tn.write(password + b"\n") 14 15tn.read_until(b"#") 16tn.write(b"test command" + b"\n") 17tn.write(b"logout" + b"\n") 18 19res = tn.read_all()
最終行のtn.read_all()をtn.read_all().decode("ascii")に変更してみましたが。
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbe in position 26790: ordinal not in range(128)
とエラーが出てうまくいきません。
改行を正常に認識する、またはdecodeのエラーを取り除く方法はありますでしょうか?
回答1件
あなたの回答
tips
プレビュー