4 Create a network and start sshd(8) on each host.
6 While something like rshd(8) would be lighter and faster,
7 (and perfectly adequate on an in-machine network)
8 the advantage of running sshd is that scripts can work
9 unchanged on mininet and hardware.
11 In addition to providing ssh access to hosts, this example
14 - creating a convenience function to construct networks
15 - connecting the host network to the root namespace
16 - running server processes (sshd in this case) on hosts
21 from mininet.net import Mininet
22 from mininet.cli import CLI
23 from mininet.log import lg, info
24 from mininet.node import Node
25 from mininet.topolib import TreeTopo
26 from mininet.util import waitListening
29 def TreeNet( depth=1, fanout=2, **kwargs ):
30 "Convenience function for creating tree networks."
31 topo = TreeTopo( depth, fanout )
32 return Mininet( topo, **kwargs )
34 def connectToRootNS( network, switch, ip, routes ):
35 """Connect hosts to root namespace via switch. Starts network.
36 network: Mininet() network object
37 switch: switch to connect to root namespace
38 ip: IP address for root namespace node
39 routes: host networks to route to"""
40 # Create a node in root namespace and link to switch 0
41 root = Node( 'root', inNamespace=False )
42 intf = network.addLink( root, switch ).intf1
43 root.setIP( ip, intf=intf )
44 # Start network that now includes link to root namespace
46 # Add routes from root ns to hosts
48 root.cmd( 'route add -net ' + route + ' dev ' + str( intf ) )
50 def sshd( network, cmd='/usr/sbin/sshd', opts='-D',
51 ip='10.123.123.1/32', routes=None, switch=None ):
52 """Start a network, connect it to root ns, and run sshd on all hosts.
53 ip: root-eth0 IP address in root namespace (10.123.123.1/32)
54 routes: Mininet host networks to route to (10.0/24)
55 switch: Mininet switch to connect to root namespace (s1)"""
57 switch = network[ 's1' ] # switch to use
59 routes = [ '10.0.0.0/24' ]
60 connectToRootNS( network, switch, ip, routes )
61 for host in network.hosts:
62 host.cmd( cmd + ' ' + opts + '&' )
63 info( "*** Waiting for ssh daemons to start\n" )
64 for server in network.hosts:
65 waitListening( server=server, port=22, timeout=5 )
67 info( "\n*** Hosts are running sshd at the following addresses:\n" )
68 for host in network.hosts:
69 info( host.name, host.IP(), '\n' )
70 info( "\n*** Type 'exit' or control-D to shut down network\n" )
72 for host in network.hosts:
73 host.cmd( 'kill %' + cmd )
76 if __name__ == '__main__':
77 lg.setLogLevel( 'info')
78 net = TreeNet( depth=1, fanout=4 )
79 # get sshd args from the command line or use default args
80 # useDNS=no -u0 to avoid reverse DNS lookup timeout
81 argvopts = ' '.join( sys.argv[ 1: ] ) if len( sys.argv ) > 1 else (
82 '-D -o UseDNS=no -u0' )
83 sshd( net, opts=argvopts )