Statistics
| Branch: | Tag: | Revision:

mininet / examples / sshd.py @ 08cef003

History | View | Annotate | Download (1.83 KB)

1
#!/usr/bin/python
2

    
3
"""Create a network and start sshd(8) on the hosts.
4
   While something like rshd(8) would be lighter and faster,
5
   (and perfectly adequate on an in-machine network)
6
   the advantage of running sshd is that scripts can work
7
   unchanged on mininet and hardware."""
8

    
9
from mininet import init, Node, createLink, TreeNet, Cli
10

    
11
def nets( hosts ):
12
   "Return list of networks (/24) for hosts."
13
   nets = {}
14
   for host in hosts:
15
      net = host.IP().split( '.' )[ : -1 ]
16
      net = '.'.join( net ) + '.0/24'
17
      nets[ net ] = True
18
   return nets.keys()
19
   
20
def addRoutes( node, nets, intf ):
21
   "Add routes from node to nets through intf."
22
   for net in nets:
23
      node.cmdPrint( 'route add -net ' + net + ' dev ' + intf )
24

    
25
def removeRoutes( node, nets ):
26
   "Remove routes to nets from node."
27
   for net in nets:
28
      node.cmdPrint( 'route del -net ' + net )
29
   
30
def sshd( network ):
31
   "Start sshd up on each host, routing appropriately."
32
   controllers, switches, hosts = (
33
      network.controllers, network.switches, network.hosts )
34
   # Create a node in root ns and link to switch 0
35
   root = Node( 'root', inNamespace=False )
36
   createLink( root, switches[ 0 ] )
37
   ip = '10.0.123.1'
38
   root.setIP( root.intfs[ 0 ], ip, '/24' )
39
   network.start()
40
   # Add routes
41
   routes = nets( hosts )
42
   addRoutes( root, routes, root.intfs[ 0 ] )
43
   # Start up sshd on each host
44
   for host in hosts: host.cmdPrint( '/usr/sbin/sshd' )
45
   # Dump out IP addresses and run CLI
46
   print
47
   print "*** Hosts are running sshd at the following addresses:"
48
   for host in hosts: print host.name, host.IP()
49
   print
50
   print "*** Starting Mininet CLI - type 'exit' or ^D to exit"
51
   network.runTest( Cli )
52
   network.stop()
53
   removeRoutes( root, routes )
54
   
55
if __name__ == '__main__':
56
   init()
57
   network = TreeNet( depth=1, fanout=2, kernel=True )
58
   sshd( network )