Files
mininet/examples/popenpoll.py
T
lantz f0c726a42f Use /usr/bin/env python for virtualenv (#1025)
This helps with virtualenv although it can open
up another security hole if you end up using an
unexpected python interpreter.

Overall it seems to make sense to err on the side
of usability but it's good to be aware of security.

However, for the remaining utility scripts that require
python 2, we explicitly note this with #!/usr/bin/python2.
2021-02-05 07:42:50 -08:00

39 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python
"Monitor multiple hosts using popen()/pmonitor()"
from time import time
from signal import SIGINT
from mininet.net import Mininet
from mininet.topo import SingleSwitchTopo
from mininet.util import pmonitor
from mininet.log import setLogLevel, info
def pmonitorTest( N=3, seconds=10 ):
"Run pings and monitor multiple hosts using pmonitor"
topo = SingleSwitchTopo( N )
net = Mininet( topo, waitConnected=True )
net.start()
hosts = net.hosts
info( "Starting test...\n" )
server = hosts[ 0 ]
popens = {}
for h in hosts:
popens[ h ] = h.popen('ping', server.IP() )
info( "Monitoring output for", seconds, "seconds\n" )
endTime = time() + seconds
for h, line in pmonitor( popens, timeoutms=500 ):
if h:
info( '<%s>: %s' % ( h.name, line ) )
if time() >= endTime:
for p in popens.values():
p.send_signal( SIGINT )
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
pmonitorTest()