pty controlling app

Brian Sturk bsturk at news.rcn.com
Mon Dec 3 02:11:27 EST 2001


I am trying to control a shell and found a few posts with
examples of controlling ssh using the pty module and cribbed the code.
My issue is I'm unable to get it to execute commands. They just get
echo'ed back. I found a post where someone had the same issue but never
posted how he got it to work after he figured it out. :(
I'm probably missing something very obvious but I've searched
everywhere and haven't been able to figure it out. Any
help would be greatly appreciated!

#!/usr/bin/env python

import os, pty, time, signal

def remote_ex( sh, arg ):

    pid, fd = pty.fork()

    if pid == 0:
        os.execv( sh, [ sh, arg ] )
    else:
        print ">> 1 sh says %r" % os.read( fd, 1024 ).strip()
#        time.sleep(2)
        print ">> 2 sh took %d bytes." % os.write( fd, "ls\n" )
#        time.sleep(2)
        print ">> 3 sh says %r" % os.read( fd, 1024 ).strip()
#        time.sleep(2)

        os.kill( pid, signal.SIGKILL )

def main():

    sh    = "/bin/bash"
    arg   = '--version'
    remote_ex( sh, arg )

if __name__ == "__main__":
    main()

//  outputs  //

>> 1 sh says 'GNU bash, version 2.05.0(1)-release (i386-slackware-linux-gnu)
   \r\nCopyright 2000 Free Software Foundation, Inc.'
>> 2 sh took 3 bytes.
>> 3 sh says 'ls'

--
.-----------------------------------------------------,-------.  
| Brian M. Sturk - http://www.nh.ultranet.com/~bsturk \ C/C++ |  .>   )\,^a__
|--------------------------|  bsturk at nh.ultranet.com   | Java | (  _ _)/ /-." ~
| http://www.telengard.com `---------------------------`------|  `( )_ )/
| Telengard Technologies Inc. -  NT/*nix UI & device drivers  | _<_s_<_s
'-------------------------------------------------------------'



More information about the Python-list mailing list