8 from mininet.util import pexpect
9 from mininet.clean import cleanup, sh
10 from sys import stdout
12 class testBareSSHD( unittest.TestCase ):
14 opts = [ 'Welcome to h1', pexpect.EOF, pexpect.TIMEOUT ]
16 def connected( self ):
17 "Log into ssh server, check banner, then exit"
18 p = pexpect.spawn( 'ssh 10.0.0.1 -o ConnectTimeout=1 '
19 '-o StrictHostKeyChecking=no '
20 '-i /tmp/ssh/test_rsa exit' )
22 index = p.expect( self.opts )
30 # verify that sshd is not running
31 self.assertFalse( self.connected() )
32 # create public key pair for testing
33 sh( 'rm -rf /tmp/ssh' )
34 sh( 'mkdir /tmp/ssh' )
35 sh( "ssh-keygen -t rsa -P '' -f /tmp/ssh/test_rsa" )
36 sh( 'cat /tmp/ssh/test_rsa.pub >> /tmp/ssh/authorized_keys' )
37 # run example with custom sshd args
38 cmd = ( 'python -m mininet.examples.baresshd '
39 '-o AuthorizedKeysFile=/tmp/ssh/authorized_keys '
41 p = pexpect.spawn( cmd )
42 runOpts = [ 'You may now ssh into h1 at 10.0.0.1',
43 'after 5 seconds, h1 is not listening on port 22',
44 pexpect.EOF, pexpect.TIMEOUT ]
46 index = p.expect( runOpts )
51 self.fail( 'sshd failed to start in host h1' )
54 "Simple test to verify that we can ssh into h1"
56 # try to connect up to 3 times; sshd can take a while to start
57 result = self.connected()
58 self.assertTrue( result )
61 # kill the ssh process
62 sh( "ps aux | grep ssh |grep Banner| awk '{ print $2 }' | xargs kill" )
64 # remove public key pair
65 sh( 'rm -rf /tmp/ssh' )
67 if __name__ == '__main__':