Revision 80be5642 examples/sshd.py

View differences:

examples/sshd.py
27 27
    "Convenience function for creating tree networks."
28 28
    topo = TreeTopo( depth, fanout )
29 29
    return Mininet( topo, **kwargs )
30
   
30

  
31 31
def connectToRootNS( network, switch, ip, prefixLen, routes ):
32
   """Connect hosts to root namespace via switch. Starts network.
32
    """Connect hosts to root namespace via switch. Starts network.
33 33
      network: Mininet() network object
34 34
      switch: switch to connect to root namespace
35 35
      ip: IP address for root namespace node
36 36
      prefixLen: IP address prefix length (e.g. 8, 16, 24)
37 37
      routes: host networks to route to"""
38
   # Create a node in root namespace and link to switch 0
39
   root = Node( 'root', inNamespace=False )
40
   port = max( switch.ports.values() ) + 1
41
   createLink( root, 0, switch, port )
42
   root.setIP( root.intfs[ 0 ], ip, prefixLen )
43
   # Start network that now includes link to root namespace
44
   network.start()
45
   intf = root.intfs[ 0 ]
46
   # Add routes from root ns to hosts
47
   for net in routes:
48
      root.cmd( 'route add -net ' + net + ' dev ' + intf )
38
    # Create a node in root namespace and link to switch 0
39
    root = Node( 'root', inNamespace=False )
40
    port = max( switch.ports.values() ) + 1
41
    createLink( root, 0, switch, port )
42
    root.setIP( root.intfs[ 0 ], ip, prefixLen )
43
    # Start network that now includes link to root namespace
44
    network.start()
45
    intf = root.intfs[ 0 ]
46
    # Add routes from root ns to hosts
47
    for route in routes:
48
        root.cmd( 'route add -net ' + route + ' dev ' + intf )
49 49

  
50 50
def sshd( network, cmd='/usr/sbin/sshd', opts='-D' ):
51
   "Start a network, connect it to root ns, and run sshd on all hosts."
52
   switch = network.switches[ 0 ] # switch to use
53
   ip = '10.123.123.1' # our IP address on host network
54
   routes = [ '10.0.0.0/8' ] # host networks to route to
55
   connectToRootNS( network, switch, ip, 8, routes )
56
   for host in network.hosts: host.cmd( cmd + ' ' + opts + '&' )
57
   print
58
   print "*** Hosts are running sshd at the following addresses:"
59
   print
60
   for host in network.hosts: print host.name, host.IP()
61
   print
62
   print "*** Type 'exit' or control-D to shut down network"
63
   CLI( network )
64
   for host in network.hosts: host.cmd( 'kill %' + cmd )
65
   network.stop()
66
   
51
    "Start a network, connect it to root ns, and run sshd on all hosts."
52
    switch = network.switches[ 0 ] # switch to use
53
    ip = '10.123.123.1' # our IP address on host network
54
    routes = [ '10.0.0.0/8' ] # host networks to route to
55
    connectToRootNS( network, switch, ip, 8, routes )
56
    for host in network.hosts:
57
        host.cmd( cmd + ' ' + opts + '&' )
58
    print
59
    print "*** Hosts are running sshd at the following addresses:"
60
    print
61
    for host in network.hosts:
62
        print host.name, host.IP()
63
    print
64
    print "*** Type 'exit' or control-D to shut down network"
65
    CLI( network )
66
    for host in network.hosts:
67
        host.cmd( 'kill %' + cmd )
68
    network.stop()
69

  
67 70
if __name__ == '__main__':
68
   lg.setLogLevel( 'info')
69
   init()
70
   network = TreeNet( depth=1, fanout=4, switch=KernelSwitch )
71
   sshd( network )
71
    lg.setLogLevel( 'info')
72
    init()
73
    net = TreeNet( depth=1, fanout=4, switch=KernelSwitch )
74
    sshd( net )

Also available in: Unified diff