Skip to content

Instantly share code, notes, and snippets.

@ffund
Last active August 29, 2015 14:05
Show Gist options
  • Save ffund/c9a1c1fadda1838d8831 to your computer and use it in GitHub Desktop.
Save ffund/c9a1c1fadda1838d8831 to your computer and use it in GitHub Desktop.
Ping script for GENI MOOC routing experiment
import fileinput
import re
import sys
import socket
import time
from oml4py import OMLBase
# Open File and Get List with lines
config = open('CONFIG.txt', 'r')
DATA=open('DATA.txt', 'w+')
contents = config.readlines()
numblines=len(contents)
currenthost=socket.gethostname()
if (currenthost == 'Stanford.SZFinalTopology.ch-geni-net.instageni.stanford.edu'):
linenumb=1
node_name='Stanford'
elif (currenthost == 'KANSAS.SZFinalTopology.ch-geni-net.instageni.ku.gpeni.net'):
linenumb=2
node_name='Kansas'
elif (currenthost == 'Wisconsin.SZFinalTopology.ch-geni-net.instageni.wisc.edu'):
linenumb=3
node_name='Wisconsin'
elif (currenthost == 'Miss.SZFinalTopology.ch-geni-net.instageni.rnet.missouri.edu'):
linenumb=4
node_name='Missouri'
elif (currenthost == 'Clemson.SZFinalTopology.ch-geni-net.instageni.clemson.edu'):
linenumb=5
node_name='Clemson University'
elif (currenthost == 'NW.SZFinalTopology.ch-geni-net.instageni.northwestern.edu'):
linenumb=6
node_name='Northwestern'
elif currenthost == 'FIU':
linenumb=7
node_name='FIU'
elif (currenthost == 'UKY.SZFinalTopology.ch-geni-net.lan.sdn.uky.edu'):
linenumb=8
node_name='Kentucky'
elif (currenthost == 'NYU.SZFinalTopology.ch-geni-net.genirack.nyu.edu'):
linenumb=9
node_name='New York University'
else:
print >> sys.stderr, "ERROR"
sys.exit(0)
print >> sys.stderr, 'Currently in node : ' + node_name
contents[linenumb-1].rstrip('\r\n')
neighbors=contents[linenumb-1]
argList=re.split('\s+', neighbors)
numb_args=len(argList)
pingdata=OMLBase("Ping")
for i in range(0,numb_args/4):
host = argList[i*4]
port = int(argList[i*4+1])
# type => ["tcp" or "udp"]
type = argList[i*4+2]
pings = int(argList[i*4+3])
numbpings=pings
pingdata.addmp("PingMeasure", "source:str destination:str port:int time:double pingnumber:int ")
pingdata.start()
pingnumb=1
ping_list=[]
foo= True
while pings > 0:
#print "Ping attempt "+ str(numbpings-pings+1)
if type == "udp":
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
try:
if type == "udp":
s.sendto("--TEST LINE--", (host, port))
recv, svr = s.recvfrom(255)
s.shutdown(2)
print >> sys.stderr, "Success connecting to " + host + " on UDP port: " + str(port)
else:
start_time=time.time()
#print start_time
s.connect((host, port))
s.shutdown(2)
run_time=time.time()-start_time
run_time_ms=run_time*1000
ping_list.append(run_time_ms)
pingdata.inject("PingMeasure", (socket.gethostname(),argList[i*4] ,port,run_time_ms, pingnumb))
#print "Run time is " + str(run_time_ms) + "ms"
if foo:
print >> sys.stderr, "Success connecting to " + host + " on TCP port: " + str(port)
foo=False
except Exception, e:
try:
errno, errtxt = e
except ValueError:
print >> sys.stderr, "Cannot connect to " + host + " on port: " + str(port)
else:
if errno == 107:
print >> sys.stderr, "Success connecting to " + host + " on UDP port: " + str(port)
else:
print >> sys.stderr, "Cannot connect to " + host + " on port: " + str(port)
print >> sys.stderr, e
pings = pings -1
pingnumb=pingnumb+1
s.close
time.sleep(1)
print >> sys.stderr, 'Ping Times: '
print >> sys.stderr, ping_list
#pingdata.close()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment