Python Pingy Dingy

A simple Python script to demonstrate running an external program and displaying the output:

# Comments
# More fun with system calls
# Author: Jeremy Pierson

from subprocess import PIPE, Popen, STDOUT

IP = ‘’
count = ‘8’

# The section below is the external system call. Parameters are passed as a list,
# which is why they are enclosed in brackets [] and seperated by commas. This is very helpful as
# it allows you to easily use seperate variables for different parameters.
# The command below executes ping -c 8

p = Popen([‘ping’,’-c ‘+count, IP], stdout=PIPE, stderr=STDOUT)

# -c specifies the number of pings on a Linux system, on windows it’s -n (default is 4 on windows)
# linux boxes will ping indefinitely

#Since the process runs silently until there is output, this message lets you know it is running
print ‘Pinging…’

#This message displays the output of the command.
print p.communicate()[0]

print ‘Complete!’

This really isn’t much more useful than running a ping directly unless you start to consider how many different ways you can use the output. The output is stored as a variable, so it can be logged, searched, sorted, truncated, concatenated, emailed, posted on a web page, etc. etc.

Posted in Python

Leave a Reply