[Tutor] Trouble with passing commands / variables to os.system()
vinces1979 at gmail.com
Tue Jun 23 22:06:35 CEST 2009
os.system is not the best way to handle this you may want to look into
the subprocess module
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:
> 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
> ssh: Could not resolve hostname i: nodename nor servname provided, or not
> 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.
> Tutor maillist - Tutor at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor