Statistics
| Branch: | Tag: | Revision:

mininet / examples / test / test_sshd.py @ bfb56004

History | View | Annotate | Download (1.82 KB)

1
#!/usr/bin/env python
2

    
3
"""
4
Test for sshd.py
5
"""
6

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

    
12
class testSSHD( unittest.TestCase ):
13

    
14
    opts = [ '\(yes/no\)\?', 'refused', 'Welcome|\$|#', pexpect.EOF, pexpect.TIMEOUT ]
15

    
16
    def connected( self, ip ):
17
        "Log into ssh server, check banner, then exit"
18
        # Note: this test will fail if "Welcome" is not in the sshd banner 
19
        # and '#'' or '$'' are not in the prompt
20
        p = pexpect.spawn( 'ssh -i /tmp/ssh/test_rsa %s' % ip, timeout=10 )
21
        while True:
22
            index = p.expect( self.opts )
23
            if index == 0:
24
                print p.match.group(0)
25
                p.sendline( 'yes' )
26
            elif index == 1:
27
                return False
28
            elif index == 2:
29
                p.sendline( 'exit' )
30
                p.wait()    
31
                return True
32
            else:
33
                return False
34

    
35
    def setUp( self ):
36
        # create public key pair for testing
37
        sh( 'rm -rf /tmp/ssh' )
38
        sh( 'mkdir /tmp/ssh' )
39
        sh( "ssh-keygen -t rsa -P '' -f /tmp/ssh/test_rsa" )
40
        sh( 'cat /tmp/ssh/test_rsa.pub >> /tmp/ssh/authorized_keys' )
41
        cmd = ( 'python -m mininet.examples.sshd -D '
42
                '-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
43
                '-o StrictModes=no' )
44
        # run example with custom sshd args
45
        self.net = pexpect.spawn( cmd )
46
        self.net.expect( 'mininet>' )
47

    
48
    def testSSH( self ):
49
        "Verify that we can ssh into all hosts (h1 to h4)"
50
        for h in range( 1, 5 ):
51
            self.assertTrue( self.connected( '10.0.0.%d' % h ) )
52

    
53
    def tearDown( self ):
54
        self.net.sendline( 'exit' )
55
        self.net.wait()
56
        # remove public key pair
57
        sh( 'rm -rf /tmp/ssh' )
58

    
59
if __name__ == '__main__':
60
    unittest.main()
61