Remote script execution with Python ...

Steve Holden sholden at holdenweb.com
Thu Mar 8 01:01:07 CET 2001


"Robert Gahan" <gahanr at gofree.indigo.ie> wrote in message
news:Lpwp6.8059$r4.7712 at news.indigo.ie...
> Thanks Ben.
>
> os.popen2 is not available to windows programmers but
> win32pipe came to the rescue !!. win32pipe I think is available
> from the following location
> http://www.activestate.com/Products/ActivePython/win32all.html
>
> Got something like the following to work ....
>
> import win32pipe
>
> stdin, stdout = win32pipe.popen2("rsh <hostname> -l <username>
> <scriptname>", "t")
> stdin.write("<whatever you want to write to stdin !!>")
> print stdout.readlines() # This will print the result.
>
> Regards,
> Robert.
>
> Ben Hutchings <ben.hutchings at roundpoint.com> wrote in message
> news:ulmqil8qu.fsf at roundpoint.com...
> > "Robert Gahan" <gahanr at gofree.indigo.ie> writes:
> >
> > > Hi there,
> > >
> > > I have installed python on a Windows PC and I am looking to
> > > remotely execute a script on a UNIX box from a python program.
> > > I need to be able to pipe a "y" to stdin also. Is there a windows
> > > equivalent of the unix "rexec" that could help me ?. What is the
> > > most appropriate way of doing this ?.
> >
> > You might be able to do this with something like:
> >
> >     script_in, script_out = os.popen2("rsh otherhost -c myscript")
> >     print >>script_in 'y'
> >     result = script_out.readlines()
> >
> > Windows 2000 includes an rsh client but I'm not sure about other
> > versions of Windows.  You might want to use ssh instead for greater
> > security.

For portability, Luz and Ascher recommend:

import sys
if sys.platform == "win32":
    try:
        import win32pipe
        popen = win32pipe.open()
    except ImportError:
        raise ImportError, "Could not find win32pipe module"
else:
    import os
    popen = os.popen()

And then use popen() to open pipes. There is an obscure bug in the Windows
os.popen() module on Windows, but this seems to be a suitable workaround.

regards
 Steve








More information about the Python-list mailing list