From c0d8fc0d37343264b9ee5df3eb9f38a521850ab4 Mon Sep 17 00:00:00 2001 From: cody burkard Date: Thu, 25 Sep 2014 13:55:49 -0700 Subject: [PATCH 1/3] wait until sshd has started on each host --- examples/sshd.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/examples/sshd.py b/examples/sshd.py index 27166cb..d7f45a4 100755 --- a/examples/sshd.py +++ b/examples/sshd.py @@ -59,6 +59,16 @@ def sshd( network, cmd='/usr/sbin/sshd', opts='-D', connectToRootNS( network, switch, ip, routes ) for host in network.hosts: host.cmd( cmd + ' ' + opts + '&' ) + + # wait until each host's sshd has started up + remaining = list( network.hosts ) + while True: + for host in tuple( remaining ): + if 'sshd is running' in host.cmd( 'service ssh status' ): + remaining.remove( host ) + if not remaining: + break + print print "*** Hosts are running sshd at the following addresses:" print From cf5bbd597ab5b0ba1624039bd92211c2b7745d34 Mon Sep 17 00:00:00 2001 From: cody burkard Date: Fri, 26 Sep 2014 16:52:51 -0700 Subject: [PATCH 2/3] promote waitListening to util.py --- examples/cpu.py | 14 +------------- examples/sshd.py | 12 ++++-------- mininet/util.py | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/examples/cpu.py b/examples/cpu.py index 6dfc936..0406cc9 100755 --- a/examples/cpu.py +++ b/examples/cpu.py @@ -7,23 +7,11 @@ cpu.py: test iperf bandwidth for varying cpu limits from mininet.net import Mininet from mininet.node import CPULimitedHost from mininet.topolib import TreeTopo -from mininet.util import custom +from mininet.util import custom, waitListening from mininet.log import setLogLevel, output from time import sleep -def waitListening(client, server, port): - "Wait until server is listening on port" - if not client.cmd('which telnet'): - raise Exception('Could not find telnet') - cmd = ('sh -c "echo A | telnet -e A %s %s"' % - (server.IP(), port)) - while 'Connected' not in client.cmd(cmd): - output('waiting for', server, - 'to listen on port', port, '\n') - sleep(.5) - - def bwtest( cpuLimits, period_us=100000, seconds=5 ): """Example/test of link and CPU bandwidth limits cpu: cpu limit as fraction of overall CPU time""" diff --git a/examples/sshd.py b/examples/sshd.py index d7f45a4..bd50e18 100755 --- a/examples/sshd.py +++ b/examples/sshd.py @@ -24,6 +24,7 @@ from mininet.log import lg from mininet.node import Node from mininet.topolib import TreeTopo from mininet.link import Link +from mininet.util import waitListening def TreeNet( depth=1, fanout=2, **kwargs ): "Convenience function for creating tree networks." @@ -59,15 +60,10 @@ def sshd( network, cmd='/usr/sbin/sshd', opts='-D', connectToRootNS( network, switch, ip, routes ) for host in network.hosts: host.cmd( cmd + ' ' + opts + '&' ) - + client = network.switches[ 0 ] # wait until each host's sshd has started up - remaining = list( network.hosts ) - while True: - for host in tuple( remaining ): - if 'sshd is running' in host.cmd( 'service ssh status' ): - remaining.remove( host ) - if not remaining: - break + for server in network.hosts: + waitListening( client, server, 22, timeout=5 ) print print "*** Hosts are running sshd at the following addresses:" diff --git a/mininet/util.py b/mininet/util.py index 3dbbea6..5d741ff 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -538,3 +538,22 @@ def ensureRoot(): print "*** Mininet must run as root." exit( 1 ) return + +def waitListening( client, server, port, timeout=None ): + "Wait until server is listening on port" + if not client.cmd( 'which telnet' ): + raise Exception('Could not find telnet' ) + cmd = ( 'sh -c "echo A | telnet -e A %s %s"' % + ( server.IP(), port ) ) + time = 0 + while 'Connected' not in client.cmd( cmd ): + if timeout: + if time >= timeout: + error( 'could not connect to %s on port %d\n' + % ( client, port ) ) + break + output('waiting for', server, + 'to listen on port', port, '\n') + sleep( .5 ) + time += .5 + From a565bdd57dc4d0d31525fab9a6b4a415d990ac89 Mon Sep 17 00:00:00 2001 From: cody burkard Date: Fri, 26 Sep 2014 23:33:59 -0700 Subject: [PATCH 3/3] fix popen to work with shell --- examples/sshd.py | 3 +-- mininet/util.py | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/sshd.py b/examples/sshd.py index bd50e18..ee80416 100755 --- a/examples/sshd.py +++ b/examples/sshd.py @@ -60,10 +60,9 @@ def sshd( network, cmd='/usr/sbin/sshd', opts='-D', connectToRootNS( network, switch, ip, routes ) for host in network.hosts: host.cmd( cmd + ' ' + opts + '&' ) - client = network.switches[ 0 ] # wait until each host's sshd has started up for server in network.hosts: - waitListening( client, server, 22, timeout=5 ) + waitListening( server=server, port=22, timeout=5 ) print print "*** Hosts are running sshd at the following addresses:" diff --git a/mininet/util.py b/mininet/util.py index 5d741ff..f819259 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -10,6 +10,7 @@ import re from fcntl import fcntl, F_GETFL, F_SETFL from os import O_NONBLOCK import os +from functools import partial # Command execution support @@ -61,12 +62,6 @@ def errRun( *cmd, **kwargs ): stderr: STDOUT to merge stderr with stdout shell: run command using shell echo: monitor output to console""" - # Allow passing in a list or a string - if len( cmd ) == 1: - cmd = cmd[ 0 ] - if isinstance( cmd, str ): - cmd = cmd.split( ' ' ) - cmd = [ str( arg ) for arg in cmd ] # By default we separate stderr, don't run in a shell, and don't echo stderr = kwargs.get( 'stderr', PIPE ) shell = kwargs.get( 'shell', False ) @@ -74,6 +69,14 @@ def errRun( *cmd, **kwargs ): if echo: # cmd goes to stderr, output goes to stdout info( cmd, '\n' ) + if len( cmd ) == 1: + cmd = cmd[ 0 ] + # Allow passing in a list or a string + if isinstance( cmd, str ) and not shell: + cmd = cmd.split( ' ' ) + cmd = [ str( arg ) for arg in cmd ] + elif isinstance( cmd, list ) and shell: + cmd = " ".join( arg for arg in cmd ) popen = Popen( cmd, stdout=PIPE, stderr=stderr, shell=shell ) # We use poll() because select() doesn't work with large fd numbers, # and thus communicate() doesn't work either @@ -539,18 +542,20 @@ def ensureRoot(): exit( 1 ) return -def waitListening( client, server, port, timeout=None ): +def waitListening( client=None, server='127.0.0.1', port=80, timeout=None ): "Wait until server is listening on port" - if not client.cmd( 'which telnet' ): + run = ( client.cmd if client else + partial( quietRun, shell=True ) ) + if not run( 'which telnet' ): raise Exception('Could not find telnet' ) cmd = ( 'sh -c "echo A | telnet -e A %s %s"' % ( server.IP(), port ) ) time = 0 - while 'Connected' not in client.cmd( cmd ): + while 'Connected' not in run( cmd ): if timeout: if time >= timeout: error( 'could not connect to %s on port %d\n' - % ( client, port ) ) + % ( server, port ) ) break output('waiting for', server, 'to listen on port', port, '\n')