#!/usr/bin/python

"""
This program allows you to ping a number of hosts, with a given number of
packets.

Usage:
./multiping.py {# of packets} {IP}....

Example 1:
./multiping.py 5 192.168.1.1

Above command pings 192.168.1.1 with 5 packets.

Example 2:
./multiping.py 25 192.168.1.1 www.google.com 210.11.44.66

Above command pings hosts 192.168.1.1, www.google.com and 210.11.44.66 with 25
packets each.

multiping.py will respond by printing to the screen wether the host is up, 
down, or if the host was unknown.

There are some circumstances this doesn't work in yet, but I'll put a try-catch 
statement or two in, and resolve it one-day.

Written by Bradley van Ree 20040525
"""

from os import popen3
import sys

#ip = sys.argv[2]
pings = int( sys.argv[1] )
i = 2
argLen = len( sys.argv )

if ( int( argLen ) >= i ):
	
	while i < len(range( argLen )): 
		ip = sys.argv[i]

		# make the ping command
		command = "ping -c %i -i 2 %s" % ( pings, ip )
		
		
		# open a pipe to the OS, and give it the command.  Also close the Input pipe to the OS, and close the error pipe.
		comIn, comOut, comErr = popen3( command )
		
		var2 = comErr.readlines()
		var1 = comOut.readlines()
		
		comErr.close()
		comOut.close()

		if ( len( var1 )  == 0 ):
			print var2[0]
		else:
			# Get the 4th line of the ping output
			for j in range( len( var1 ) ):
				if ( j == ( pings + 3 ) ):
			      		recievedLine = var1[j][:-1]
		
			# Tokenize the above selected line
			recievedList = recievedLine.split()
		
			if (recievedList[3] != "0"):
				print "%s is up" % ip
			else:
				print "%s is down" % ip

		i = i + 1