#!/usr/local/bin/python import socket import struct import sys import os import commands # add testconverge to listener # add responses to all listener commands # create multicast_group = ('224.3.29.71', 8989) cmdinput = '' message = 'Empty Message' basemeship = os.environ['BASEMESHIP'] wlanmesh = os.environ['WLANMESH'] mesh = 'none' timeout = 5 acksize = 40 menu = True f = '' # will hold file isopen = False #number of nodes to expect on mesh expect = 1 # change this to number of params! #error messages invalidparam = 'Invalid parameters supplied\n' # Create the datagram socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Set a timeout so the socket does not block indefinitely when trying # to receive data. sock.settimeout(timeout) # Set the time-to-live for messages to 1 so they do not go past the # local network segment. ttl = struct.pack('b', 10) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl) # Send data to the multicast group while cmdinput != 'exit': repsonse = '' #open a filename based on testname #close a filename based on testname if menu == True: print >>sys.stderr, "\tCOMMANDS: (node numbers == number on stick in machines)" print >>sys.stderr, "\tFollowing commands run against sideband network" print >>sys.stderr, "\tbatup [spc delim target node numbers]" print >>sys.stderr, "\tbatdown [spc delim target node numbers]" print >>sys.stderr, "\t802up [spc delim target node numbers]" print >>sys.stderr, "\t802down [spc delim target node numbers]" print >>sys.stderr, "\tolsrup [spc delim target node numbers]" print >>sys.stderr, "\tolsrdown [spc delim target node numbers]" print >>sys.stderr, "\theartbeatup [spc delim target node numbers]" print >>sys.stderr, "\theartbeatdown [spc delim target node numbers]" print >>sys.stderr, "\tFollowing commands run against Mesh Network" print >>sys.stderr, "\tuperf (iperf tcp bandwidth test)" print >>sys.stderr, "\ttperf (iperf udp bandwidth test)" print >>sys.stderr, "\ttraceroute " print >>sys.stderr, "\tping " print >>sys.stderr, "\ttestconverge " # add this to listener print >>sys.stderr, "\thello <# of acks expected> (every node up should acknowledge over mesh)" print >>sys.stderr, "\tstart " # add this print >>sys.stderr, "\tstop (saves results in local directory, file )" print >>sys.stderr, "\thelp (displays this menu)" print >>sys.stderr, "\texit (close console)" #perhaps a single setmesh command then an up and down command would be nicer to use.. #and maintain an upcount menu = False cmdinput = raw_input("Command: ") bypass = False response = '' sock.settimeout(timeout) cmd = cmdinput.split(' ') if cmd[0] == 'tperf': if cmd[1].isdigit and len(cmd) == 2: response = commands.getoutput( 'iperf -c '+basemeship+''.join(cmd[1])) print >>sys.stderr, response else: print >>sys.stderr, invalidparam continue expect = -1 elif cmd[0] == 'uperf': if cmd[1].isdigit and len(cmd) == 2: response = commands.getoutput( 'iperf -u -c '+basemeship+''.join(cmd[1])) print >>sys.stderr, response else: print >>sys.stderr, invalidparam continue expect = -1 elif cmd[0] == 'start': if len(cmd) == 2 and (f == '' or f.closed): # I know it really should be wrapped in try, except, finally... f = open(cmd[1], 'w') elif len(cmd) == 2 and not f.closed: print >>sys.stderr, 'file %s is open!' % f.name else: print >>sys.stderr, invalidparam continue expect = -1 elif cmd[0] == 'stop': if f == '': print >>sys.stderr, 'no test file ever started' continue elif not f.closed: f.write('End of test\n') f.close() else: print >>sys.stderr, 'test file %s is already closed\nperhaps you wanted to start a new test file?' % f.name continue expect = -1 elif cmd[0] == 'batup': sent = sock.sendto(cmdinput, multicast_group) mesh = 'batman' expect = len(cmd) - 1 elif cmd[0] == 'batdown': sent = sock.sendto(cmdinput, multicast_group) mesh = 'none' expect = len(cmd) - 1 elif cmd[0] == '802up': sent = sock.sendto(cmdinput, multicast_group) mesh = '80211s' expect = len(cmd) - 1 elif cmd[0] == '802down': sent = sock.sendto(cmdinput, multicast_group) mesh = 'none' expect = len(cmd) - 1 elif cmd[0] == 'olsrup': sent = sock.sendto(cmdinput, multicast_group) mesh = 'olsrd' expect = len(cmd) - 1 elif cmd[0] == 'olsrdown': sent = sock.sendto(cmdinput, multicast_group) mesh = 'none' expect = len(cmd) - 1 elif cmd[0] == 'heartbeatup': sent = sock.sendto(cmdinput, multicast_group) expect = len(cmd) - 1 elif cmd[0] == 'heartbeatdown': sent = sock.sendto(cmdinput, multicast_group) expect = len(cmd) - 1 elif cmd[0] == 'testconverge': sent = sock.sendto(cmdinput, multicast_group) expect = len(cmd) - 1 elif cmd[0] == 'traceroute': if len(cmd) == 2 and cmd[1].isdigit: response = commands.getoutput('meshtrace '+cmd[1]) expect = -1 print >>sys.stderr, response else: print >>sys.stderr, invalidparam continue elif cmd[0] == 'ping': if len(cmd) == 2 and cmd[1].isdigit: response = commands.getoutput('meshping '+cmd[1]) expect = -1 print >>sys.stderr, response else: print >>sys.stderr, invalidparam continue expect = -1 elif cmd[0] == 'exit': cmdinput = cmd[0] break elif cmd[0] == 'hello': if len(cmd) == 2: expect = int(cmd[1]) sock.settimeout(10) sent = sock.sendto(cmd[0], multicast_group) else: print >> sys.stderr, invalidparam expect = -1 elif cmd[0] == 'help': expect = -1 menu = True continue else: print >> sys.stderr, "Unrecognized command\n\n" continue count = 0 if f != '' and not f.closed: f.write("COMMAND ISSUED:"+cmdinput+"\n") if len(response) != 0: f.write(response+'\n') if expect != -1: print >>sys.stderr, '\n'+'wainting for repsonses' while count < expect or expect == 0: #either wait for expect # responses, or wait the time try: data, server = sock.recvfrom(acksize) except socket.timeout: print >>sys.stderr, "Responses rcvd:"+`count` break else: print >>sys.stderr, 'recevied "%s"' % data if len(data) > 0: count = count + 1 if f != '' and not f.closed: f.write(data+'\n') # here only for exampel, will removed or flag it, we dont want to write most acks to file