Statistics
| Branch: | Tag: | Revision:

mininet / examples / sshd.py @ 98332cb6

History | View | Annotate | Download (1.71 KB)

1
#!/usr/bin/python
2

    
3
"""
4
Create a network and start sshd(8) on each host.
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 sshd( network ):
40
   "Start a network, connect it to root ns, and run sshd on all hosts."
41
   connectToRootNS( network, network.switches[ 0 ] )
42
   for host in network.hosts: host.cmd( 'sshd -D &' )
43
   print
44
   print "*** Hosts are running sshd at the following addresses:"
45
   print
46
   for host in network.hosts: print host.name, host.IP()
47
   print
48
   print "*** Press return to shut down network: ",
49
   readline()
50
   for host in network.hosts: host.cmd( 'kill %sshd')
51
   network.stop()
52
   
53
if __name__ == '__main__':
54
   init()
55
   network = TreeNet( depth=1, fanout=4, kernel=True )
56
   sshd( network )