Revision 55dd9368 examples/sshd.py

View differences:

examples/sshd.py
1 1
#!/usr/bin/python
2 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."""
3
"""
4
Create a network and start sshd(8) on the hosts.
8 5

  
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.
10
"""
11

  
12
import sys ; readline = sys.stdin.readline
9 13
from mininet import init, Node, createLink, TreeNet, Cli
10 14

  
11 15
def nets( hosts ):
......
17 21
      nets[ net ] = True
18 22
   return nets.keys()
19 23
   
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
24
def connectToRootNS( network, switch ):
25
   "Connect hosts to root namespace via switch. Starts network."
26
   # Create a node in root namespace and link to switch 0
35 27
   root = Node( 'root', inNamespace=False )
36
   createLink( root, switches[ 0 ] )
28
   createLink( root, switch )
37 29
   ip = '10.0.123.1'
38 30
   root.setIP( root.intfs[ 0 ], ip, '/24' )
31
   # Start network that now includes link to root namespace
39 32
   network.start()
40 33
   # 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
34
   routes = nets( network.hosts )
35
   intf = root.intfs[ 0 ]
36
   for net in routes:
37
      root.cmdPrint( 'route add -net ' + net + ' dev ' + intf )
38

  
39
def startServers( network, server ):
40
   "Start network, and servers on each host."
41
   connectToRootNS( network, network.switches[ 0 ] )
42
   for host in network.hosts: host.cmdPrint( server )
43

  
44
if __name__ == '__main__':
45
   init()
46
   network = TreeNet( depth=1, fanout=4, kernel=True )
47
   startServers( network, '/usr/sbin/sshd' )
46 48
   print
47 49
   print "*** Hosts are running sshd at the following addresses:"
48
   for host in hosts: print host.name, host.IP()
49 50
   print
50
   print "*** Starting Mininet CLI - type 'exit' or ^D to exit"
51
   network.runTest( Cli )
51
   for host in network.hosts: print host.name, host.IP()
52
   print
53
   print "*** Press return to shut down network: ",
54
   readline()
52 55
   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 )

Also available in: Unified diff