Connecting Tech Pros Worldwide Help | Site Map

OS specific command in Python

diffuser78@gmail.com
Guest
 
Posts: n/a
#1: Jun 20 '06
I want to write a python program and call OS specific commands in it.
So basically, instead of typing in on the command line argument I want
to have it in a python program and let it do the action.

for example. in my program I would want to call the ssh feature like
one does on the command line

ssh Admin@192.168.2.10 .....etc

How can I do this easily ?

Every help is appreciated.

Thanks

stephanearnold@yahoo.fr
Guest
 
Posts: n/a
#2: Jun 20 '06

re: OS specific command in Python


diffuser78@gmail.com a écrit :
[color=blue]
> So basically, instead of typing in on the command line argument I want
> to have it in a python program and let it do the action.[/color]

Try exec() and execfile() from the standard library (IIRC)
[color=blue]
>
> for example. in my program I would want to call the ssh feature like
> one does on the command line
>
> ssh Admin@192.168.2.10 .....etc
>[/color]

When you connect (via ssh or telnet) to a remote machine, you need to
type (manually)
your username and your password. Programming that is never easy.

If what you need is simulating console interaction, you will need to
get Expect.
Expect allows you to program/emulate user interaction in a text
console, based on the Tool Command Language (a.k.a. Tcl).

But there are alternatives : start a SSH daemon written as a shell
script. (you just have to enter your login/pass once, then call the
daemon to execute some commands)

IMHO, Expect is by far the cleanest way.

Regards,
Stéphane

Robert Kern
Guest
 
Posts: n/a
#3: Jun 20 '06

re: OS specific command in Python


diffuser78@gmail.com wrote:[color=blue]
> I want to write a python program and call OS specific commands in it.
> So basically, instead of typing in on the command line argument I want
> to have it in a python program and let it do the action.
>
> for example. in my program I would want to call the ssh feature like
> one does on the command line
>
> ssh Admin@192.168.2.10 .....etc
>
> How can I do this easily ?[/color]

http://www.python.org/doc/current/li...ubprocess.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Avell Diroll
Guest
 
Posts: n/a
#4: Jun 20 '06

re: OS specific command in Python


diffuser78@gmail.com wrote:[color=blue]
> I want to write a python program and call OS specific commands in it.
> So basically, instead of typing in on the command line argument I want
> to have it in a python program and let it do the action.[/color]

There are several ways to do so :
* os.system() if you just want to launch a command (kind of fire-and-forget)
* the subprocess module if you want to access stdin, stdout and stderr
of the launched command
[color=blue]
>
> for example. in my program I would want to call the ssh feature like
> one does on the command line
>
> ssh Admin@192.168.2.10 .....etc[/color]

To interact with an ssh process , there is a great module called pexpect
(featuring some examples of how to handle ssh process in python)
http://pexpect.sourceforge.net/


OT : paramiko is a module to handle the SSH2 protocol in python
http://www.lag.net/paramiko/

Cameron Laird
Guest
 
Posts: n/a
#5: Jun 20 '06

re: OS specific command in Python


In article <1150783324.258644.65770@u72g2000cwu.googlegroups. com>,
<stephanearnold@yahoo.fr> wrote:[color=blue]
>diffuser78@gmail.com a écrit :
>[color=green]
>> So basically, instead of typing in on the command line argument I want
>> to have it in a python program and let it do the action.[/color]
>
>Try exec() and execfile() from the standard library (IIRC)
>[color=green]
>>
>> for example. in my program I would want to call the ssh feature like
>> one does on the command line
>>
>> ssh Admin@192.168.2.10 .....etc
>>[/color]
>
>When you connect (via ssh or telnet) to a remote machine, you need to
>type (manually)
>your username and your password. Programming that is never easy.
>
>If what you need is simulating console interaction, you will need to
>get Expect.
>Expect allows you to program/emulate user interaction in a text
>console, based on the Tool Command Language (a.k.a. Tcl).
>
>But there are alternatives : start a SSH daemon written as a shell
>script. (you just have to enter your login/pass once, then call the
>daemon to execute some commands)
>
>IMHO, Expect is by far the cleanest way.[/color]
diffuser78@gmail.com
Guest
 
Posts: n/a
#6: Jun 20 '06

re: OS specific command in Python


When you connect (via ssh or telnet) to a remote machine, you need to[color=blue]
> type (manually)
> your username and your password. Programming that is never easy.[/color]

I have setup the public keys to AUTO LOGIN so that it doesn't ask the
password.

diffuser78@gmail.com
Guest
 
Posts: n/a
#7: Jun 20 '06

re: OS specific command in Python


I tried the following and it seemed to work

import os
os.system('<system command here>')

Any comments....

Avell Diroll
Guest
 
Posts: n/a
#8: Jun 21 '06

re: OS specific command in Python


diffuser78@gmail.com wrote:[color=blue]
> I tried the following and it seemed to work
>
> import os
> os.system('<system command here>')
>
> Any comments....
>[/color]

This is an simple way to proceed if you don't need your python script to
know what happens to the launched process ...
When you need to :
* send some input to the command from the python script after it is launched
* get the output of the command in your python script
* get the pid associated to your command
* wait for your command to finish
* pipe some shell commands
* ...
you should use the subprocess module.

Here is a quick example from the Python Reference Library :
http://docs.python.org/lib/node242.html

##Shell Script :
output=`dmesg | grep hda`


##Python Script :
from subprocess import Popen
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
Avell Diroll
Guest
 
Posts: n/a
#9: Jun 21 '06

re: OS specific command in Python


stephanearnold@yahoo.fr wrote:[color=blue]
> When you connect (via ssh or telnet) to a remote machine, you need to
> type (manually)
> your username and your password. Programming that is never easy.
>[/color]

This is really eased by the module getpass (std library) :

###

import getpass

login = getpass.getuser()
password = getpass.getpass()

###

If the username is different from your system login this can be changed to :

###

import getpass

login = raw_input('login: ')
password = getpass.getpass()

###


Python definitely comes with batteries included !
diffuser78@gmail.com
Guest
 
Posts: n/a
#10: Jun 21 '06

re: OS specific command in Python


I have a question on getpass. Since I am a newbie you might find it a
little dumb.

By using the getpass, are u trying to retrieve the username and
password of remote mahcine or local ?



Avell Diroll wrote:[color=blue]
> stephanearnold@yahoo.fr wrote:[color=green]
> > When you connect (via ssh or telnet) to a remote machine, you need to
> > type (manually)
> > your username and your password. Programming that is never easy.
> >[/color]
>
> This is really eased by the module getpass (std library) :
>
> ###
>
> import getpass
>
> login = getpass.getuser()
> password = getpass.getpass()
>
> ###
>
> If the username is different from your system login this can be changed to :
>
> ###
>
> import getpass
>
> login = raw_input('login: ')
> password = getpass.getpass()
>
> ###
>
>
> Python definitely comes with batteries included ![/color]

Jon Ribbens
Guest
 
Posts: n/a
#11: Jun 21 '06

re: OS specific command in Python


In article <1150783324.258644.65770@u72g2000cwu.googlegroups. com>, stephanearnold@yahoo.fr wrote:[color=blue][color=green]
>> So basically, instead of typing in on the command line argument I want
>> to have it in a python program and let it do the action.[/color]
>
> Try exec() and execfile() from the standard library (IIRC)[/color]

Ths os.spawn...() functions are likely to be better suited to what he
wants to do.
[color=blue][color=green]
>> ssh Admin@192.168.2.10 .....etc[/color]
>
> When you connect (via ssh or telnet) to a remote machine, you need to
> type (manually) your username and your password. Programming that is
> never easy.[/color]

Indeed, so it is much easier to use public-key authentication with an
unencrypted private key, that way you don't have to type any
passwords. See the "AUTHORIZED_KEYS FILE FORMAT" section of the 'sshd'
man page, and the 'ssh-keygen' command.
diffuser78@gmail.com
Guest
 
Posts: n/a
#12: Jun 21 '06

re: OS specific command in Python


Hi Avell,

I want to communicate using subprocess module but my task is a little
different. May be you can guide me.

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.

I used

import os
os.system('ssh Admin@IP_ADDRESS shutdown -s')


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.

Any suggestions on how to do this ?

Every help is appreciated.

Thanks for your time

Avell Diroll wrote:
[color=blue]
> This is an simple way to proceed if you don't need your python script to
> know what happens to the launched process ...
> When you need to :
> * send some input to the command from the python script after it is launched
> * get the output of the command in your python script
> * get the pid associated to your command
> * wait for your command to finish
> * pipe some shell commands
> * ...
> you should use the subprocess module.
>
> Here is a quick example from the Python Reference Library :
> http://docs.python.org/lib/node242.html
>
> ##Shell Script :
> output=`dmesg | grep hda`
>
>
> ##Python Script :
> from subprocess import Popen
> p1 = Popen(["dmesg"], stdout=PIPE)
> p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
> output = p2.communicate()[0][/color]

3c273
Guest
 
Posts: n/a
#13: Jun 21 '06

re: OS specific command in Python


"Avell Diroll" <avelldiroll@yahoo.fr> wrote in message
news:4498d973$0$25496$626a54ce@news.free.fr...[color=blue]
> ##Python Script :
> from subprocess import Popen
> p1 = Popen(["dmesg"], stdout=PIPE)
> p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
> output = p2.communicate()[0][/color]

I was just trying to learn how to use .communicate() and all of the examples
I see have [0] after .communicate(). What is the significance of the [0]?
Thanks for your help.
Louis


Avell Diroll
Guest
 
Posts: n/a
#14: Jun 21 '06

re: OS specific command in Python


diffuser78@gmail.com wrote:[color=blue]
> I have a question on getpass. Since I am a newbie you might find it a
> little dumb.
>
> By using the getpass, are u trying to retrieve the username and
> password of remote mahcine or local ?
>[/color]

the module getpass contains 2 functions, getuser() and getpass() :

getuser() returns the username of the user executing the python script
on the machine where the script is executed.

getpass() prompts the user for a password and returns it in a string
whitout printing it on screen (just as the text mode login on linux or
ssh).

By the way this is better explained in the official python documentation.
http://docs.python.org/lib/module-getpass.html
Avell Diroll
Guest
 
Posts: n/a
#15: Jun 21 '06

re: OS specific command in Python


3c273 wrote:[color=blue]
> I was just trying to learn how to use .communicate() and all of the examples
> I see have [0] after .communicate(). What is the significance of the [0]?[/color]


From the Python Library Reference
(http://docs.python.org/lib/node239.html), you learn that the method
communicate() from the subprocess.Popen() class returns a tuple
containing the standard output as first item and the standard error of
the child process as second item. So the [0] in the example is for
selecting the first item of the tuple ...

####

from subprocess import *
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]

### is equivalent to :

from subprocess import *
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output_and_error = p2.communicate()
output = output_and_error[0]

### or :

from subprocess import *
p1 = Popen(["dmesg"], stdout=PIPE)
output=Popen(["grep","hda"],stdin=p1.stdout,stdout=PIPE).communicate()[0]

### or :

from subprocess import *
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
(output, stderror) = p2.communicate()


I hope it was useful ...

Avell Diroll
Guest
 
Posts: n/a
#16: Jun 21 '06

re: OS specific command in Python


diffuser78@gmail.com wrote:
(snip)[color=blue]
> 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.
>[/color]
(snip : script that fits your first need)[color=blue]
>
> 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.[/color]
(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


3c273
Guest
 
Posts: n/a
#17: Jun 21 '06

re: OS specific command in Python


"Avell Diroll" <avelldiroll@yahoo.fr> wrote in message
news:4499ad49$0$23551$626a54ce@news.free.fr...[color=blue]
> 3c273 wrote:[color=green]
> > I was just trying to learn how to use .communicate() and all of the[/color][/color]
examples[color=blue][color=green]
> > I see have [0] after .communicate(). What is the significance of the[/color][/color]
[0]?[color=blue]
>
>
> From the Python Library Reference
> (http://docs.python.org/lib/node239.html), you learn that the method
> communicate() from the subprocess.Popen() class returns a tuple
> containing the standard output as first item and the standard error of
> the child process as second item. So the [0] in the example is for
> selecting the first item of the tuple ...[/color]

Thank you for taking the time to make that clear for me. I did read the
docs, I just didn't really understand what communicate() did, but I do now.
Thanks again.
Louis


Closed Thread


Similar Python bytes