auto

This tutorial will cover some Python techniques to automate OS commands.  We will show two ways to execute commands using Python (os, subprocess).

As you begin to create Python scripts you will likely find yourself leveraging os.system and subprocess.Popen because they let you run OS commands.  The main difference between os.system and subprocess.Popen is that subprocess allows you to redirect STDOUT to a variable in Python.  This is often quite useful if you want to fiddle with STDOUT further prior to printing it to the screen.

Okay enough talk, let’s code:

>>>
>>> import os
>>> os.system('uname -a')
Linux cell 3.11.0-20-generic #35~precise1-Ubuntu SMP Fri May 2 21:32:55 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
0
>>> os.system('id')
uid=1000(cell) gid=1000(cell) groups=1000(cell),0(root)
0
>>> os.system('ping -c 1 127.0.0.1')
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.043 ms

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.043/0.043/0.043/0.000 ms
0
>>>

As you can see there isn’t much to the running a system command with the os module.  Check out the some additional functions offered by the os function “dir(os)”.

Lets run some of these same commands with the subprocess module:

>>> import subprocess
>>>
>>> com_str = 'uname -a'
>>> command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
>>> (output, error) = command.communicate()
>>> print output
Linux cell 3.11.0-20-generic #35~precise1-Ubuntu SMP Fri May 2 21:32:55 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

>>> com_str = 'id'
>>> command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
>>> (output, error) = command.communicate()
>>> print output
uid=1000(cell) gid=1000(cell) groups=1000(cell),0(root)
>>>

As you can see the syntax for subprocess is more complex, but the ability redirect from STDOUT to a variable is extremely valuable.  This allows you to manipulate the output, or write output to a file:

>>> com_str = 'id'
>>> command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
>>> (output, error) = command.communicate()
>>> output
'uid=1000(cell) gid=1000(cell) groups=1000(cell),0(root)n'
>>> f = open('file.txt', 'w')
>>> f.write(output)
>>> f.close()
>>> for line in open('file.txt', 'r'):
...   print line
...
uid=1000(cell) gid=1000(cell) groups=1000(cell),0(root)

>>>

The techniques shown in this tutorial are very powerful for automating OS commands.  As you being to slam out CLI Kung Fu, remember it can always be thrown into a Python script.

Try it out for yourself, write a script that runs a few commands and writes the output to a file, or only prints a portion of the output.