File: test_sshd.py

package info (click to toggle)
mininet 2.3.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,588 kB
  • sloc: python: 11,181; ansic: 1,332; sh: 967; makefile: 87
file content (60 lines) | stat: -rwxr-xr-x 1,916 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python

"""
Test for sshd.py
"""

import unittest
from mininet.util import pexpect
from mininet.clean import sh

class testSSHD( unittest.TestCase ):

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

    def connected( self, ip ):
        "Log into ssh server, check banner, then exit"
        # Note: this test will fail if "Welcome" is not in the sshd banner
        # and '#'' or '$'' are not in the prompt
        ssh = 'ssh -o StrictHostKeyChecking=no -i /tmp/ssh/test_rsa ' + ip
        p = pexpect.spawn( ssh, timeout=5 )
        while True:
            index = p.expect( self.opts )
            if index == 0:
                print( p.match.group(0) )
                p.sendline( 'yes' )
            elif index == 1:
                return False
            elif index == 2:
                p.sendline( 'exit' )
                p.wait()
                return True
            else:
                return False

    def setUp( self ):
        # create public key pair for testing
        sh( 'rm -rf /tmp/ssh' )
        sh( 'mkdir /tmp/ssh' )
        sh( "ssh-keygen -t rsa -P '' -f /tmp/ssh/test_rsa" )
        sh( 'cat /tmp/ssh/test_rsa.pub >> /tmp/ssh/authorized_keys' )
        cmd = ( 'python -m mininet.examples.sshd -D '
                '-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
                '-o StrictModes=no -o UseDNS=no -u0' )
        # run example with custom sshd args
        self.net = pexpect.spawn( cmd )
        self.net.expect( 'mininet>' )

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

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

if __name__ == '__main__':
    unittest.main()