Telmo
Telmo

Reputation: 127

Unexpected output in Paramiko

I'm using Paramiko to send some commands to a router but the output text is almost unreadable.

How can I remove the 'b' and \r\n in the output?

What is the correct way to break lines?

This piece of code is supposedly where it should break lines correctly:

def print_lines(self, data):
        last_line = data
        if '\n' in data:
            lines = data.splitlines()
            for i in range(0, len(lines)-1):
                print((lines[i]))
            last_line = lines[len(lines) - 1]
            if data.endswith('\n'):
                print(last_line)
                last_line = ''
        return last_line

This is the full code:

class ssh:
    shell = None
    client = None
    transport = None

    def __init__(self, address, username, password):
#        print(("Connecting to server on ip", str(address) + "."))
        self.client = paramiko.client.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
        self.client.connect(address, username=username, password=password, look_for_keys=False)
        self.transport = paramiko.Transport((address, 22))
        self.transport.connect(username=username, password=password)

        thread = threading.Thread(target=self.process)
        thread.daemon = True
        thread.start()

    def close_connection(self):
        if(self.client != None):
            self.client.close()
            self.transport.close()

    def open_shell(self):
        self.shell = self.client.invoke_shell()

    def send_shell(self, command):
        if(self.shell):
            self.shell.send(command + "\n")
        else:
            print("<h1>Shell não aberta.</h1>")

    def process(self):
        global strdata, fulldata
        while True:
            # Print data when available
            if self.shell is not None and self.shell.recv_ready():
                alldata = self.shell.recv(1024)
                while self.shell.recv_ready():
                    alldata += self.shell.recv(1024)
                strdata = strdata + str(alldata)
                fulldata = fulldata + str(alldata)
                strdata = self.print_lines(strdata) # print all received data except last line

    def print_lines(self, data):
        last_line = data
        if '\n' in data:
            lines = data.splitlines()
            for i in range(0, len(lines)-1):
                print((lines[i]))
            last_line = lines[len(lines) - 1]
            if data.endswith('\n'):
                print(last_line)
                last_line = ''
        return last_line


sshUsername = "admin"
sshPassword = "password"
sshServer = "192.168.40.165"

connection = ssh(sshServer, sshUsername, sshPassword)
connection.open_shell()
connection.send_shell('en')
connection.send_shell('conf t')
connection.send_shell('hostname R5')
time.sleep(1)
print('<h1>'+fulldata+'</h1>')   # This contains the complete data received.
connection.close_connection()

I expected this:

R5#en
R%#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R5(config)#hostname R5
R5(config)#

But this was the result:

b'\r\nR5#'b'e'b'n'b'\r\n'b'R5#'b'c'b'o'b'n'b'f'b' t'b'\r\n'b'Enter configuration commands, one per line. End with CNTL/Z.\r\nR5(config)#'b'h'b'o'b's'b't'b'n'b'a'b'm'b'e'b' 'b'R'b'5'b'\r\n'b'R5(config)#'

How can i get the line breaks correctly? Please forgive my big post.

Upvotes: 3

Views: 331

Answers (2)

Malekai
Malekai

Reputation: 5011

It looks like you're dealing with a bytes() object (more info), so you'll have to use the decode() method it to turn it into a string.

For example, lets create a bytes() object and turn it back into a string:

greeting = str("Hello World").encode("UTF-8")
print(greeting)
greeting = greeting.decode("UTF-8")
print(greeting)

Or in your case:

alldata_str = alldata.decode("UTF-8")

WARNING: Paramiko might have a different encoding for their strings so be sure to double check their documentation.

Good luck.

Upvotes: 1

Totoro
Totoro

Reputation: 887

alldata receives bytes from paramiko, and you are using str(bytes) so you are putting quotes around the b'string'. you need to use decode to decode the bytes

alldata_str = alldata.decode()
strdata = strdata + alldata_str
fulldata = fulldata + alldata_str

There are libraries that help with paramiko like for routers netmiko based on paramiko. I also have a library to do terminal interactive ssh to devices in general https://github.com/filintod/pyremotelogin (also based on paramiko). There is also napalm (https://napalm-automation.net/blog/) also for networking.

Upvotes: 1

Related Questions