I'm trying to write a python script that can ssh into remote server and can execute simple commands like ls,cd from the python client. However, I'm not able to read the output from the pseudo-terminal after successfully ssh'ing into the server. Could anyone please help me here so that I could execute some commands on the server.
Here is the sample code:
#!/usr/bin/python2.6
import os,sys,time,thread
pid,fd = os.forkpty()
if pid == 0:os.execv('/usr/bin/ssh',['/usr/bin/ssh','user@host',])sys.exit(0)
else:output = os.read(fd,1024)print outputdata = outputos.write(fd,'password\n')time.sleep(1)output = os.read(fd,1024)print outputos.write(fd,'ls\n')output = os.read(fd,1024)print output
Sample output:
user@host's password: Last login: Wed Aug 24 03:16:57 2011 from 1x.x.x.xxxx-bash: ulimit: open files: cannot modify limit: Operation not permitted
host: /home/user>ls