[Tutor] Trouble with passing commands / variables to os.system()
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@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with passing commands / variables to os.system()
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 -i %s %s" %(username, host, private_key, command) On Tue, Jun 23, 2009 at 2:01 PM, Charlie Reddington > 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@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with passing commands / variables to os.system()
On Jun 23, 2009, at 4:50 PM, Alan Gauld wrote: "Charlie Reddington" wrote Your code works as expected! Can you tell me what your code is doing different than mine? os.system needs the command to be a string so you have to build up the string by passing in your variables using the string format operator(%) or building it bit by bit outside the call to system. Beware that the return value from system is just the exit code which is not very useful, hencehthe recommendation to oook at subprocess... for host in servers: os.system("ssh %...@%s -i %s %s" %(username, host, private_key, command) Also while this might seem a good approach to start you will likely find that most of the things you are doing via sysyem will be possible directly from Python which will be more efficient in resources. Take a look at the Using the OS topic in my tutorioal for just a few examples of the kinds of things you can do directly - as well as how to use subprocess. HTH, Thanks for all the replies, I'll definitely look into it all. Charlie -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor