8 from mininet.util import pexpect
9 from mininet.clean import sh
11 class testSSHD( unittest.TestCase ):
13 opts = [ '\(yes/no\)\?', 'refused', 'Welcome|\$|#', pexpect.EOF, pexpect.TIMEOUT ]
15 def connected( self, ip ):
16 "Log into ssh server, check banner, then exit"
17 # Note: this test will fail if "Welcome" is not in the sshd banner
18 # and '#'' or '$'' are not in the prompt
19 p = pexpect.spawn( 'ssh -i /tmp/ssh/test_rsa %s' % ip, timeout=10 )
21 index = p.expect( self.opts )
23 print( p.match.group(0) )
35 # create public key pair for testing
36 sh( 'rm -rf /tmp/ssh' )
37 sh( 'mkdir /tmp/ssh' )
38 sh( "ssh-keygen -t rsa -P '' -f /tmp/ssh/test_rsa" )
39 sh( 'cat /tmp/ssh/test_rsa.pub >> /tmp/ssh/authorized_keys' )
40 cmd = ( 'python -m mininet.examples.sshd -D '
41 '-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
42 '-o StrictModes=no -o UseDNS=no -u0' )
43 # run example with custom sshd args
44 self.net = pexpect.spawn( cmd )
45 self.net.expect( 'mininet>' )
48 "Verify that we can ssh into all hosts (h1 to h4)"
49 for h in range( 1, 5 ):
50 self.assertTrue( self.connected( '10.0.0.%d' % h ) )
53 self.net.sendline( 'exit' )
55 # remove public key pair
56 sh( 'rm -rf /tmp/ssh' )
58 if __name__ == '__main__':