0

I am using paramiko to get information from switch. The manul step would be:

  1. login to the switch via ssh
  2. run a simple command

here is the output:

SW10 [standalone: master] > show interfaces ethernet 1/32 status

Port                   Operational state           Speed                  Negotiation
----                   -----------------           -----                  -----------
Eth1/32                Up                          10 Gbps                No-Negotiation

You can see the output just text.

But when I run this via a script, I got the following strange characters.

import paramiko
import time

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.10.21.22', username='admin', password='pass')
channel = ssh.invoke_shell()
resp = channel.recv(9999)

show_interface_cmd = "show interfaces ethernet 1/21 status\r\n"
channel.send(show_interface_cmd)
resp = channel.recv(9999)
print(resp)

Output:

b'\rSW10 [standalone: master] > show interfaces ethernet 1/26 status\r\r\n\x1b[?1h\x1b=\r\r\nPort
            Operational state           Speed                  Negoti \x08ation              \r\n----
 -----------------           -----                  ------ \x08-----              \r\nEth1/26                Up
                  10 Gbps                No-Neg \x08otiation           \r\n\r\x1b[K\x1b[?1l\x1b>\rSW10 [standalone: master] > \r\r\n\rSW10 [standalone: master] > '

Then if I send another correct command and try to get the response with recv, the output will contains error.

My question is why I got this kind of strange output? And how to handle this in the right way? Thanks.

TangHongWan
  • 645
  • 1
  • 6
  • 18

1 Answers1

-1

Replace channel = ssh.invoke_shell() by channel = ssh.exec_command().

In addition: the "b'" in the "resp" output indicates that the literal should become a bytes literal in byte. If you decode the string you get a string (skipping here the options you can set as suggested in the comments by Martin Prikry). Depending on the decoding type you may get an error or not. For example I used the "utf-8" character-set for decoding in below sample code. This can be replaced by any other type of character-set decoding. See also https://en.wikipedia.org/wiki/Character_encoding. The python 3 documentation on this can be found at https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

resp = b'\rSW10 [standalone: master] > show interfaces ethernet 1/26 status\r\r\n\x1b[?1h\x1b=\r\r\nPort
            Operational state           Speed                  Negoti \x08ation              \r\n----
 -----------------           -----                  ------ \x08-----              \r\nEth1/26                Up
                  10 Gbps                No-Neg \x08otiation           \r\n\r\x1b[K\x1b[?1l\x1b>\rE2E-M9958-102Y-SW10 [standalone: master] > \r\r\n\rSW10 [standalone: master] > '

decoded_resp = resp.decode("utf-8")  # "utf-8" is replaceable by any other type of character-set.

print(decoded_resp )

Example:

b'\xE2\x82\xAC'.decode('UTF-8')

result : '€'

or:

b'\xc2\xb5'.decode('utf-8')

result :'µ'

ZF007
  • 3,708
  • 8
  • 29
  • 48