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

Charlie Reddington charlie.reddington at gmail.com
Tue Jun 23 22:01:46 CEST 2009


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



More information about the Tutor mailing list