如何使用python使ssh输出整洁

2024-04-25 09:01:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些ssh的输出。我想用python使它整洁

ip_address = "xxxx"
username = "xxx"
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username)
remote_connection = ssh_client.invoke_shell() 
remote_connection.send("show run | i _20.20.20.20\n")               
time.sleep(5)
readoutput = remote_connection.recv(65000)
ssh_client.close()

print(readoutput)

现在我得到:

RouterA#show run | i _20.20.20.20
 neighbor 20.20.20.20 remote-as 202025
 neighbor 20.20.20.20 password password
  neighbor 20.20.20.20 activate
crs1.ams1#

也在Html我得到这个

b'\r\nRouterA#show run | i _10.10.10.10\r\n neighbor 10.10.10.10 remote-as 1010\r\n neighbor 10.10.10.10 password password1010\r\n neighbor 10.10.10.10 activate

我想要的是:(输出中没有\n)

""""
RouterA#show run | i _20.20.20.20
neighbor 20.20.20.20 remote-as 202025
neighbor 20.20.20.20 password password
neighbor 20.20.20.20 activate
RouterA#

"""

Tags: runipclientparamikoremoteaddressasshow
1条回答
网友
1楼 · 发布于 2024-04-25 09:01:05

关于:

for line in readoutput.split('\n'):
    print(line.strip())

对于html输出,它是bytestring:

html = b'\r\nRouterA#show run | i _10.10.10.10\r\n neighbor 10.10.10.10 remote-as 1010\r\n neighbor 10.10.10.10 password password1010\r\n neighbor 10.10.10.10 activate'
for line in html.decode().split('\r\n'):
    print(line.strip())

输出:

RouterA#show run | i _10.10.10.10
neighbor 10.10.10.10 remote-as 1010
neighbor 10.10.10.10 password password1010
neighbor 10.10.10.10 activate

相关问题 更多 >