TEST_ping.py 2.4 KB
import sys, os, threading, time, subprocess
import utils_test

# Global variable
channel = 1

def configure_environment(config_file_path, peers_file_path):
        config_command = subprocess.Popen(["python", "setup-wlan.py" , config_file_path, peers_file_path],
                                   shell=False,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        result = config_command.stdout.readlines()
        if result == []:
                return "ERROR"
        else:
                return result

def configure_sniff(channel_to_sniff):
	cmd = "./load-ath9k.sh %s" % channel_to_sniff
	result = "ERROR"
        while result == "ERROR":
		config_command = subprocess.Popen([cmd],
	                                   shell=True,
	                                   stdout=subprocess.PIPE,
	                                   stderr=subprocess.PIPE)
	        result = config_command.stdout.readlines()
        	print "error, trying to configure fish1 one more time on %s..." % channel_to_sniff
	return result

def start_capture(channel_to_sniff):
        file_back = "test-pcap/tcpdump%s.pcap" % channel_to_sniff
	cmd = "tcpdump -v -i fish1 -w %s" % file_back
	capture_command = subprocess.Popen([cmd],
		shell=True,
		stdout=subprocess.PIPE,
		stderr=subprocess.PIPE)
	
	global channel
        channel = channel + 1
        time.sleep(1000)
        capture_command.kill()

if (len(sys.argv) != 2):
	print "Usage: python TEST_ping.py path_to_peers_file"
	sys.exit()
# Check path to config file and peers file
if (not os.path.isfile(str(sys.argv[1]))):
	print "Configuration file missing."
	sys.exit()

peers_file_path = str(sys.argv[1])

for i in range(1, 15):

	config_file_path = "config-files/ur/channel/setup-ur0-%i.json" % channel
	print "Configuring environment ..."
	configuration = configure_environment(config_file_path, peers_file_path)
	print "COnfiguring fish1 ..."
	sniff_if = configure_sniff(channel)

	if configuration == "ERROR":
		print "Can't configure configure environment: file %s" % config_file_path
		sys.exit()
	elif sniff_if == "ERROR":
		print "Can't configure fish1"

	print "start monitor on channel %s" % channel
	start_capture(channel)

	with open(peers_file_path) as peers_file_opened:
		for peer in peers_file_opened:
			peer = peer.split()
			peer_ip = peer[0]

			utils_test.check_ping(peer_ip)

	print "finished monitor on channel %s" % channel