Revision a565bdd5

View differences:

examples/sshd.py
60 60
    connectToRootNS( network, switch, ip, routes )
61 61
    for host in network.hosts:
62 62
        host.cmd( cmd + ' ' + opts + '&' )
63
    client = network.switches[ 0 ]
64 63
    # wait until each host's sshd has started up
65 64
    for server in network.hosts:
66
        waitListening( client, server, 22, timeout=5 )
65
        waitListening( server=server, port=22, timeout=5 )
67 66

  
68 67
    print
69 68
    print "*** Hosts are running sshd at the following addresses:"
mininet/util.py
10 10
from fcntl import fcntl, F_GETFL, F_SETFL
11 11
from os import O_NONBLOCK
12 12
import os
13
from functools import partial
13 14

  
14 15
# Command execution support
15 16

  
......
61 62
       stderr: STDOUT to merge stderr with stdout
62 63
       shell: run command using shell
63 64
       echo: monitor output to console"""
64
    # Allow passing in a list or a string
65
    if len( cmd ) == 1:
66
        cmd = cmd[ 0 ]
67
        if isinstance( cmd, str ):
68
            cmd = cmd.split( ' ' )
69
    cmd = [ str( arg ) for arg in cmd ]
70 65
    # By default we separate stderr, don't run in a shell, and don't echo
71 66
    stderr = kwargs.get( 'stderr', PIPE )
72 67
    shell = kwargs.get( 'shell', False )
......
74 69
    if echo:
75 70
        # cmd goes to stderr, output goes to stdout
76 71
        info( cmd, '\n' )
72
    if len( cmd ) == 1:
73
        cmd = cmd[ 0 ]
74
    # Allow passing in a list or a string
75
    if isinstance( cmd, str ) and not shell:
76
        cmd = cmd.split( ' ' )
77
        cmd = [ str( arg ) for arg in cmd ]
78
    elif isinstance( cmd, list ) and shell:
79
        cmd = " ".join( arg for arg in cmd )
77 80
    popen = Popen( cmd, stdout=PIPE, stderr=stderr, shell=shell )
78 81
    # We use poll() because select() doesn't work with large fd numbers,
79 82
    # and thus communicate() doesn't work either
......
539 542
        exit( 1 )
540 543
    return
541 544

  
542
def waitListening( client, server, port, timeout=None ):
545
def waitListening( client=None, server='127.0.0.1', port=80, timeout=None ):
543 546
    "Wait until server is listening on port"
544
    if not client.cmd( 'which telnet' ):
547
    run = ( client.cmd if client else
548
                partial( quietRun, shell=True ) )
549
    if not run( 'which telnet' ):
545 550
        raise Exception('Could not find telnet' )
546 551
    cmd = ( 'sh -c "echo A | telnet -e A %s %s"' %
547 552
           ( server.IP(), port ) )
548 553
    time = 0
549
    while 'Connected' not in client.cmd( cmd ):
554
    while 'Connected' not in run( cmd ):
550 555
        if timeout:
551 556
            if time >= timeout:
552 557
                error( 'could not connect to %s on port %d\n'
553
                       % ( client, port ) )
558
                       % ( server, port ) )
554 559
                break
555 560
        output('waiting for', server,
556 561
               'to listen on port', port, '\n')

Also available in: Unified diff