How to do an ssh -t admin@ipaddress command with paramiko?

Asked by Hachem

Hi,

I am trying to do an ssh -t admin@ipaddress command with paramiko

so i saw that in the channel class, there is the get_pty() method that replace the -t

and i use this piece of code:
import socket,paramiko

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(20)
sock.connect(("10.10.24.246",22))
_t = paramiko.Transport(sock)
_t.connect(hostkey=None ,username='root', password='pass', pkey=None)
_chan = _t.open_session()
_chan.get_pty()
_chan.exec_command("ls")

when i execute the script nothing happen, in the /var/log/messages of the server i can see that the session was opened for
user root.

Any help.

Thanks in advance.

Best Regards,
Hachem

Question information

Language:
English Edit question
Status:
Solved
For:
paramiko Edit question
Assignee:
No assignee Edit question
Solved by:
Lars Friedrichs
Solved:
Last query:
Last reply:
Revision history for this message
Best Lars Friedrichs (l-friedrichs) said :
#1

Hi,

I never programmed python before but I found this would be a nice starter. I think all it takes is reading and printing the channels output.

I added these lines:
line=_chan.recv(500)
print line
_chan.close()
_t.close()

The 500 stands for a buffer of max. 500 characters, you may choose a bigger one if your commands output is bigger...

Hope it helps,
Lars

PS: I still get an error
No handlers could be found for logger "paramiko.transport"
but this seems secondary to me, read the paramiko docs at http://www.lag.net/paramiko/docs/paramiko.Transport-class.html
They helped me a lot.

Revision history for this message
Hachem (hachem-osmani-yahoo) said :
#2

Thanks Lars Friedrichs, that solved my question.