Revision 7c5d2771

View differences:

examples/baresshd.py
4 4

  
5 5
import sys
6 6
from mininet.node import Host
7
from mininet.util import ensureRoot
7
from mininet.util import ensureRoot, waitListening
8 8

  
9 9
ensureRoot()
10
timeout = 5
10 11

  
11 12
print "*** Creating nodes"
12 13
h1 = Host( 'h1' )
......
33 34
if len( sys.argv ) > 1:
34 35
    cmd += ' ' + ' '.join( sys.argv[ 1: ] )
35 36
h1.cmd( cmd )
37
listening = waitListening( server=h1, port=22, timeout=timeout )
36 38

  
37
print "*** You may now ssh into", h1.name, "at", h1.IP()
39
if listening:
40
    print "*** You may now ssh into", h1.name, "at", h1.IP()
41
else:
42
    print ( "*** Warning: after %s seconds, %s is not listening on port 22"
43
            % ( timeout, h1.name ) )
examples/test/test_baresshd.py
6 6

  
7 7
import unittest
8 8
import pexpect
9
from time import sleep
10 9
from mininet.clean import cleanup, sh
11 10

  
12 11
class testBareSSHD( unittest.TestCase ):
13 12

  
14
    opts = [ '\(yes/no\)\?', 'Welcome to h1', 'refused', pexpect.EOF, pexpect.TIMEOUT ]
13
    opts = [ 'Welcome to h1', pexpect.EOF, pexpect.TIMEOUT ]
15 14

  
16 15
    def connected( self ):
17 16
        "Log into ssh server, check banner, then exit"
18
        p = pexpect.spawn( 'ssh 10.0.0.1 -i /tmp/ssh/test_rsa exit' )
17
        p = pexpect.spawn( 'ssh 10.0.0.1 -o StrictHostKeyChecking=no -i /tmp/ssh/test_rsa exit' )
19 18
        while True:
20 19
            index = p.expect( self.opts )
21 20
            if index == 0:
22
                p.sendline( 'yes' )
23
            elif index == 1:
24 21
                return True
25 22
            else:
26 23
                return False
......
37 34
        cmd = ( 'python -m mininet.examples.baresshd '
38 35
                '-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
39 36
                '-o StrictModes=no' )
40
        sh( cmd )
37
        p = pexpect.spawn( cmd )
38
        runOpts = [ 'You may now ssh into h1 at 10.0.0.1',
39
                    'after 5 seconds, h1 is not listening on port 22', 
40
                    pexpect.EOF, pexpect.TIMEOUT ]
41
        while True:
42
            index = p.expect( runOpts )
43
            if index == 0:
44
                break
45
            else:
46
                self.tearDown()
47
                self.fail( 'sshd failed to start in host h1' )
41 48

  
42 49
    def testSSH( self ):
43 50
        "Simple test to verify that we can ssh into h1"
44 51
        result = False
45 52
        # try to connect up to 3 times; sshd can take a while to start
46
        for _ in range( 3 ):
47
            result = self.connected()
48
            if result:
49
                break
50
            else:
51
                sleep( 1 )
53
        result = self.connected()
52 54
        self.assertTrue( result )
53 55

  
54 56
    def tearDown( self ):
mininet/util.py
543 543
    return
544 544

  
545 545
def waitListening( client=None, server='127.0.0.1', port=80, timeout=None ):
546
    "Wait until server is listening on port"
546
    """Wait until server is listening on port.
547
       returns True if server is listening"""
547 548
    run = ( client.cmd if client else
548 549
                partial( quietRun, shell=True ) )
549 550
    if not run( 'which telnet' ):
......
554 555
    time = 0
555 556
    while 'Connected' not in run( cmd ):
556 557
        if timeout:
558
            print time
557 559
            if time >= timeout:
558 560
                error( 'could not connect to %s on port %d\n'
559 561
                       % ( server, port ) )
560
                break
562
                return False
561 563
        output('waiting for', server,
562 564
               'to listen on port', port, '\n')
563 565
        sleep( .5 )
564 566
        time += .5
565

  
567
    return True

Also available in: Unified diff