OS specific command in Python

Avell Diroll avelldiroll at yahoo.fr
Wed Jun 21 17:48:14 EDT 2006


diffuser78 at gmail.com wrote:
(snip)
> I have a linux box, from where I remotely execute all the commands. The
> remote machine is windows machine. I installed an OpenSSH server for
> windows to send the shutdown command. I setup the public keys in such a
> way that I could login to SSH server without using password.
> 
(snip : script that fits your first need)
> 
> I was wondering how can I interact with an application . Since you
> mentioned about subprocess module, I want a ability that my PYthon
> script can actually interact with the windows box and launch and close
> application there remotely.
(snip)

To clear up things a little, you _interact_ with the ssh process 
launched on your local machine from your python script. this ssh process 
_connect_ to an ssh server on your remote winbox. the ssh server then 
execute the requested shell command. So you are limited by what a 
windows shell script can do.
I must warn you that I never used windows much and never thought about 
running a ssh server or even a serious shell session (can you even call 
that a shell ?) on a windows box, so I won't be of much help for the 
_interacting_ with windows process from the windows shell. The only 
suggestion I could make would be to write a python script to be executed 
on the remote machine that would be executed by the ssh server ...

####local_machine_script.py

import os
os.system('ssh Admin at IP_ADDRESS python remote_machine_script.py')

###remote_machine_script.py
from subprocess import *
p = Popen(['A_WIN32_COMMAND'], stdout=PIPE)
(output, error) = p.communicate('INPUT_FOR_THE_WIN32_CMD')

This is really a _bad_ way to proceed but it gives you a little more 
interaction.

A better way to interact with the remote shell is to keep the ssh 
session interactive in the first place. To do that os.system is a 'no 
go' and using the subprocess module all the way won't be easy (if even 
possible). For an interactive ssh session you will need some non 
standard library modules. I know 2 modules that would help : pexpect and 
paramiko. Both may help but you will have to choose one or the other.

pexpect
pexpect gives you some control over a child process easing the way to 
pass argument, to get and test the output in order to choose the next 
input to send to the child process.
you can find it here :
http://pexpect.sourceforge.net/
The demo files contain some _great_ examples of how to use it with ssh

paramiko
paramiko implements the ssh2 protocol in python, that way you don't have 
to use a child process to connect to the remote ssh server. After 
establishing the connection you may open an ssh session or whatever you 
need from your local script.
you can find it here :
http://www.lag.net/paramiko/
Again the demo files contain some _great_ examples

I would advise you to download both and to play with the demo files in 
order to choose which one you prefer using.
If you are in a hurry, you should check pexpect first as one of the demo 
files is implementing nearly everything you want.



Hope this helps

-Avell





More information about the Python-list mailing list