1

i write my code in python 2 and everything going well but when i use the same code in python3 the output not so fit as i want or not look as in python 2 how i solve this issue ?

import paramiko
import time
UserName = "*****"
Password = "******"
ip = "10.226.159.1"

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, username=UserName, password=Password)
remote_connection = ssh_client.invoke_shell()
remote_connection.send("show  interfaces transceiver \n")

time.sleep(10)
output = remote_connection.recv(65535)
print (output)

the output in python2 :

 Rp#sh int tra
 If device is externally calibrated, only calibrated values are printed.
 ++ : high alarm, +  : high warning, -  : low warning, -- : low alarm.
 NA or N/A: not applicable, Tx: transmit, Rx: receive.
 mA: milliamperes, dBm: decibels (milliwatts).

              Optical   Optical
       Temperature  Voltage  Tx Power  Rx Power
     Port       (Celsius)    (Volts)  (dBm)     (dBm)
     ---------  -----------  -------  --------  --------
     Te1/1/1      24.3       3.26      -2.9      -1.8
     Te1/1/3      34.7       3.35      -1.1     -13.7
     Te1/1/4      32.6       3.20      -3.5      -1.6

the output in python 3:

     b'\r\nRp#sh int tra\r\nIf device is externally calibrated, only calibrated values are 
   printed.\r\n++ : high alarm, +  : high warning, -  : low warning, -- : low alarm.\r\nNA or   
   N/A: not applicable, Tx: transmit, Rx: receive.\r\nmA: milliamperes, dBm: decibels 
   (milliwatts).\r\n\r\n                                 Optical   Optical\r\n          Temperature  Voltage  Tx Power  Rx Power\r\nPort       (Celsius)    (Volts)  (dBm)     (dBm)\r
 \n---------  -----------  -------  --------  --------\r\nTe1/1/1      25.2       3.26      -2.9      -1.8   \r\nTe1/1/3      35.5       3.35      -1.1     -13.6   \r\nTe1/1/4      33.6       3.21      -3.5      -1.6   \r\n\r\n\r\n
  • 1
    Are you concerned about values of output or the formatting? Incase its formatting, output in python3 is in bytes. **string.decode()** will convert bytes to string. – PaxPrz Jan 03 '20 at 12:58

2 Answers2

2

To print the output correctly in python 3, you need to change the datatype from bytes to string:

>>> print(output.decode())
Rp#sh int tra
If device is externally calibrated, only calibrated values are printed.
++ : high alarm, +  : high warning, -  : low warning, -- : low alarm.
NA or   N/A: not applicable, Tx: transmit, Rx: receive.
mA: milliamperes, dBm: decibels (milliwatts).

                                 Optical   Optical
          Temperature  Voltage  Tx Power  Rx Power
Port       (Celsius)    (Volts)  (dBm)     (dBm)
---------  -----------  -------  --------  --------
Te1/1/1      25.2       3.26      -2.9      -1.8   
Te1/1/3      35.5       3.35      -1.1     -13.6   
Te1/1/4      33.6       3.21      -3.5      -1.6  
CDJB
  • 14,043
  • 5
  • 29
  • 55
2

String prefixed with b is a bytes literal (see more details here What does the 'b' character do in front of a string literal?)

To print bytes literal you need to convert it into a string first:

# Check if we have string or bytes. Convert to string, if necessary
if type(output) == bytes:
    output = output.decode() 
print(output)

This code should work both in Python 2 and Python 3

Alexander Pushkarev
  • 1,075
  • 6
  • 19