Re: OS specific command in Python



diffuser78@xxxxxxxxx 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@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


.



Relevant Pages

  • Re: [SLE] Remote Desktop Connection
    ... > I alreadytried the Remote Desktop Connection from my Linux box. ... > I can open a window showing the remote Windows screen. ... SCP requires an ssh server at the other end... ...
    (SuSE)
  • Re: Problem serving up java gui
    ... > The problem is that I need to run it on my personal cygwin SSH server. ... I guess that the problem is that java on your Windows box does not ... with Cygwin do use X-protocol. ... other "remote GUI" protocol which is supported by windows, e.g., vnc. ...
    (SSH)
  • Re: ssh to telnet
    ... >> I set up a Linux box inside my 2k3 domain as a ssh server. ... How bad am I limiting myself in remote administration ... your question is about "Windows Admin via ...
    (comp.os.linux.networking)
  • SecurityFocus Microsoft Newsletter #228
    ... RaidenHTTPD Remote File Disclosure Vulnerability ... Microsoft Outlook Web Access Login Form Remote URI Redirecti... ... Microsoft Windows Hyperlink Object Library Buffer Overflow V... ...
    (Focus-Microsoft)
  • SecurityFocus Microsoft Newsletter #212
    ... MICROSOFT VULNERABILITY SUMMARY ... ARJ Software UNARJ Remote Directory Traversal Vulnerability ... Microsoft Windows XP WAV File Handler Denial Of Service Vuln... ...
    (Focus-Microsoft)