前提・実現したいこと
pythonのtelnetlibを使用してCiscoのCSR1000vからshow runを取得しようとしているのですが、windows10で実行すると下記出力結果のように改行が一つ余分にできてしまいます。余分な改行をなくすにはどうすればよいでしょうか。
改行が余分に含まれる出力ファイルの一部
以下、出力されたファイルの一部。
show running-config
Building configuration...
Current configuration : 1108 bytes
!
! Last configuration change at 23:03:32 UTC Sat Oct 17 2020
!
version 15.4
service timestamps debug datetime msec
service timestamps log datetime msec
no platform punt-keepalive disable-kernel-core
platform console virtual
!
使用したソースコード
python
1# coding:utf-8 2 3import telnetlib 4 5def main(): 6 result = telnet("x.x.x.x") 7 writeFile("x.x.x.x", result) 8 9def writeFile(client, text): 10 with open("test" + '.txt', mode='w') as wf: 11 wf.writelines(text) 12 wf.close() 13 14def telnet(clientIp): 15 tn = telnetlib.Telnet(clientIp) 16 17 tn.read_until(b'Username:') 18 tn.write(b'cisco' + b'\n') 19 20 tn.read_until(b'Password:') 21 tn.write(b'cisco' + b'\n') 22 tn.read_until(b'>') 23 24 tn.write(b'enable' + b'\n') 25 26 tn.read_until(b'Password:') 27 tn.write(b'cisco' + b'\n') 28 29 tn.read_until(b'#') 30 tn.write(b'terminal length 0' + b'\n') 31 tn.write(b'show running-config' + b'\n') 32 33 tn.read_until(b'#') 34 35 result = tn.read_until(b"FIN\n", timeout = 1).decode('ascii', errors="replace") 36 return result 37 38 tn.close() 39 40if __name__ == '__main__': 41 main()
試したこと
WindowsのWSL(Ubuntu)で上記コードを実行し、出力されたファイルの改行はwindows側でみても、WSL上でcatでみても正常な改行のファイルが出力されます。
利用環境
windows 10
python 3.6.3
csr1000v IOS XE 03.11.04.S
回答2件
あなたの回答
tips
プレビュー