Skip to content
Advertisement

How to ssh.invoke_shell() work under Linux

import paramiko
import os
import sys

#Data conection
ssh_servidor = 'xxx.xx.xx.xxx'
ssh_usuario = 'yyy'
ssh_clave = 'pws'
ssh_puerto = yy

# Conect to the server
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Open a server session
ssh.connect(ssh_servidor, ssh_puerto, ssh_usuario, ssh_clave)
channel = ssh.invoke_shell()
channel.send('clear')
channel.send('n')
channel_data = str()

while True:

    if channel.recv_ready():
        enter code herechannel_data += channel.recv(9999)
        print '############ Device Ouput ##############'
        print channel_data
        print '########################################'

    else:
        continue

    if channel_data.endswith('xxxx@yyyyy~$'):
        channel.send('ls | grep mmmm')
        channel.send('n')

The problem is that in theory the var channel_data must have and print This:

############ Device Ouput ##############
Linux tito 2.6.32-5-amd64 #1 SMP Fri May 10 08:43:19 UTC 2013 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Dec 14 10:01:15 2017 from .xx.xx.xxx

xxxx@yyyyy~$ 
########################################

Inted of that, it print this:

############ Device Ouput ##############
Linux tito 2.6.32-5-amd64 #1 SMP Fri May 10 08:43:19 UTC 2013 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Dec 14 10:01:15 2017 from .xx.xx.xxx

########################################

The host “xxxx@yyyyy~$” it never appeared, so the part of the code when I asked for:

if channel_data.endswith('xxxx@yyyyy~$'):

It never happened.

The only difference from this, to the paramiko librarie example I saw, was that the code work on Windows.

So the quiestión is, what’s the difference between win and Linux at this point?

Why the S.O. do not show the host xxxx@yyyyy~$ ?

Any sugggestión?

Advertisement

Answer

I found the answer:

channel_bytes = channel.recv(9999)

channel_data = channel_bytes.decode(“utf-8”)

Otherwise, people will run into is using bytes (from a buffer) as strings.

Thanks guys !!

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement