Statistics
| Branch: | Tag: | Revision:

mininet / examples / sshd.py @ 55dd9368

History | View | Annotate | Download (1.67 KB)

1
#!/usr/bin/python
2

    
3
"""
4
Create a network and start sshd(8) on the hosts.
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
13
from mininet import init, Node, createLink, TreeNet, Cli
14

    
15
def nets( hosts ):
16
   "Return list of networks (/24) for hosts."
17
   nets = {}
18
   for host in hosts:
19
      net = host.IP().split( '.' )[ : -1 ]
20
      net = '.'.join( net ) + '.0/24'
21
      nets[ net ] = True
22
   return nets.keys()
23
   
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
27
   root = Node( 'root', inNamespace=False )
28
   createLink( root, switch )
29
   ip = '10.0.123.1'
30
   root.setIP( root.intfs[ 0 ], ip, '/24' )
31
   # Start network that now includes link to root namespace
32
   network.start()
33
   # Add routes
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' )
48
   print
49
   print "*** Hosts are running sshd at the following addresses:"
50
   print
51
   for host in network.hosts: print host.name, host.IP()
52
   print
53
   print "*** Press return to shut down network: ",
54
   readline()
55
   network.stop()