4 multiping.py: monitor multiple sets of hosts using ping
6 This demonstrates how one may send a simple shell script to
7 multiple hosts and monitor their output interactively for a period=
12 from mininet.net import Mininet
13 from mininet.node import Node
14 from mininet.topo import SingleSwitchTopo
15 from mininet.log import info, setLogLevel
17 from select import poll, POLLIN
21 "Divide list l into chunks of size n - thanks Stackoverflow"
22 return [ l[ i: i + n ] for i in range( 0, len( l ), n ) ]
24 def startpings( host, targetips ):
25 "Tell host to repeatedly ping targets"
27 targetips = ' '.join( targetips )
30 cmd = ( 'while true; do '
31 ' for ip in %s; do ' % targetips +
32 ' echo -n %s "->" $ip ' % host.IP() +
33 ' `ping -c1 -w 1 $ip | grep packets` ;'
38 info( '*** Host %s (%s) will be pinging ips: %s\n' %
39 ( host.name, host.IP(), targetips ) )
43 def multiping( netsize, chunksize, seconds):
44 "Ping subsets of size chunksize in net of size netsize"
46 # Create network and identify subnets
47 topo = SingleSwitchTopo( netsize )
48 net = Mininet( topo=topo, waitConnected=True )
51 subnets = chunks( hosts, chunksize )
53 # Create polling object
54 fds = [ host.stdout.fileno() for host in hosts ]
57 poller.register( fd, POLLIN )
60 for subnet in subnets:
61 ips = [ host.IP() for host in subnet ]
62 #adding bogus to generate packet loss
63 ips.append( '10.0.0.200' )
65 startpings( host, ips )
68 endTime = time() + seconds
69 while time() < endTime:
70 readable = poller.poll(1000)
71 for fd, _mask in readable:
72 node = Node.outToNode[ fd ]
73 info( '%s:' % node.name, node.monitor().strip(), '\n' )
77 host.cmd( 'kill %while' )
82 if __name__ == '__main__':
84 multiping( netsize=20, chunksize=4, seconds=10 )