Run commands from a list and check exit codes with python 3

Hi, all. I’m going to show you a pretty small python script, which was made after my friend’s request. It was needed just as a training example, but I think it’ll be useful in usual sysadmin’s life. So, welcome.

Below is a code:

import os
import subprocess as sp
import syslog

hello = "small script 0.0.0.1"
print(hello)
syslog.syslog(hello)
separator = "----------------------------"
print(separator)

#LIST OF COMMANDS
CMDS = ("df -h", 
	"ip a",
	"ping 8.8.8.8 -c 1",
	"cp /etc/no/such/file /tmp/")                #Broken command for example

for cmd in CMDS:
	c = sp.Popen(['bash','-c', cmd], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
	stream = c.communicate()[0]
	rc = c.returncode
	info1 = "INFO: Trying to run command "+cmd
	info2 = "INFO: Exit code is "+str(rc)
	info3 = "INFO: SUCCESS for "+cmd
	error1 = "ERROR: FAIL RESULT for "+cmd
	print(info1)
	syslog.syslog(info1)
	print(info2)
	syslog.syslog(info2)
	if rc == 0:
		print(info3)
		syslog.syslog(info3)
	else:
		print(error1)
		syslog.syslog(error1)
	print(separator)

As you can see, the code is pretty simple. All that it makes is:

  • runs commands one by one (stdouts are hidden)
  • checks exit codes for each command
  • prints report on terminal and also send a similar info to system journal

An example of code run:

$ python3 dosomething.py 
small script 0.0.0.1
----------------------------
INFO: Trying to run command df -h
INFO: Exit code is 0
INFO: SUCCESS for df -h
----------------------------
INFO: Trying to run command ip a
INFO: Exit code is 0
INFO: SUCCESS for ip a
----------------------------
INFO: Trying to run command ping 8.8.8.8 -c 1
INFO: Exit code is 0
INFO: SUCCESS for ping 8.8.8.8 -c 1
----------------------------
INFO: Trying to run command cp /etc/no/such/file /tmp/
INFO: Exit code is 1
ERROR: FAIL for cp /etc/no/such/file /tmp/
----------------------------

System journal at the same time:

$journalctl -f
<...>
Aug 08 10:21:40 dunterov dosomething.py[8883]: small script 0.0.0.1
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: Trying to run command df -h
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: Exit code is 0
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: SUCCESS for df -h
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: Trying to run command ip a
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: Exit code is 0
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: SUCCESS for ip a
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: Trying to run command ping 8.8.8.8 -c 1
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: Exit code is 0
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: SUCCESS for ping 8.8.8.8 -c 1
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: Trying to run command cp /etc/no/such/file /tmp/
Aug 08 10:21:40 dunterov dosomething.py[8883]: INFO: Exit code is 1
Aug 08 10:21:40 dunterov dosomething.py[8883]: ERROR: FAIL for cp /etc/no/such/file /tmp/
<...>

I think that kind of code may be implemented as part of some automating project or something, what can help you “free your hands”.

Written on August 8, 2017