[Tutor] Trouble with passing commands / variables to os.system()

Charlie Reddington charlie.reddington at gmail.com
Tue Jun 23 22:19:16 CEST 2009


Thanks,

Your code works as expected! Can you tell me what your code is doing  
different than mine?

Charlie

On Jun 23, 2009, at 3:06 PM, vince spicer wrote:

> os.system is not the best way to handle this you may want to look into
> the subprocess module
>
> however:
>
> import os
>
> username = 'charlie'
> private_key = '/path/to/key'
> ssh = '/usr/bin/ssh'
> command = 'hostname && df -h && exit'
>
> servers = ['172.16.1.1', '172.16.12.2', '172.16.1.3']
>
> for host in servers:
>     os.system("ssh %s@%s -i %s %s" %(username, host, private_key,  
> command)
>
>
>
> On Tue, Jun 23, 2009 at 2:01 PM, Charlie Reddington <charlie.reddington at gmail.com 
> > wrote:
> Hi,
>
> I'm very very green when it comes to python. I know bash better than  
> python, so I figured a good way to learn things was covert my bash  
> stuff to python. So here goes...
>
> Here's a quick example of the code I have that is broken.
>
> import os
>
> username = 'charlie'
> private_key = '/path/to/key'
> ssh = '/usr/bin/ssh'
> command = 'hostname && df -h && exit'
>
> servers = ['172.16.1.1', '172.16.12.2', '172.16.1.3']
>
> for host in servers:
>    print(os.system(ssh -l username -i private_key host command))
>
> What I'm trying to do is is, is use ssh with a private key. That way  
> I can quickly run some remote commands on a few hundred servers in a  
> quick way to do stuff (disk usage, top, etc).
>
> When I run this, I get errors like this for every host in my list.
>
> Warning: Identity file private_key not accessible: No such file or  
> directory.
> ssh: Could not resolve hostname i: nodename nor servname provided,  
> or not known
> 65280
>
> My first thoughts are, it's not passing my variables to the function  
> the way I'd expect.
>
> So my questions are...
>
> 1.) Is it nessacary to put my IP's in quotes?
> 2.) When I call a variable in a function (like os.system() or  
> print()) I don't use $'s right?
> 3.) Am I putting variables in my functions properly? Can I put  
> variables like this in there?
>
> Thanks for any help.
>
> Charlie
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090623/12cce2a4/attachment.htm>


More information about the Tutor mailing list