Statistics
| Branch: | Tag: | Revision:

mininet / examples / sshd.py @ 51270ce4

History | View | Annotate | Download (1.72 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

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

    
16
def nets( hosts ):
17
   "Return list of networks (/24) for hosts."
18
   nets = {}
19
   for host in hosts:
20
      net = host.IP().split( '.' )[ : -1 ]
21
      net = '.'.join( net ) + '.0/24'
22
      nets[ net ] = True
23
   return nets.keys()
24
   
25
def connectToRootNS( network, switch ):
26
   "Connect hosts to root namespace via switch. Starts network."
27
   # Create a node in root namespace and link to switch 0
28
   root = Node( 'root', inNamespace=False )
29
   createLink( root, switch )
30
   ip = '10.0.123.1'
31
   root.setIP( root.intfs[ 0 ], ip, '/24' )
32
   # Start network that now includes link to root namespace
33
   network.start()
34
   # Add routes
35
   routes = nets( network.hosts )
36
   intf = root.intfs[ 0 ]
37
   for net in routes:
38
      root.cmdPrint( 'route add -net ' + net + ' dev ' + intf )
39

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