Statistics
| Branch: | Tag: | Revision:

mininet / examples / sshd.py @ 80be5642

History | View | Annotate | Download (2.57 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
In addition to providing ssh access to hosts, this example
12
demonstrates:
13

14
- creating a convenience function to construct networks
15
- connecting the host network to the root namespace
16
- running server processes (sshd in this case) on hosts
17
"""
18

    
19
from mininet.net import init, Mininet
20
from mininet.cli import CLI
21
from mininet.log import lg
22
from mininet.node import Node, KernelSwitch
23
from mininet.topolib import TreeTopo
24
from mininet.util import createLink
25

    
26
def TreeNet( depth=1, fanout=2, **kwargs ):
27
    "Convenience function for creating tree networks."
28
    topo = TreeTopo( depth, fanout )
29
    return Mininet( topo, **kwargs )
30

    
31
def connectToRootNS( network, switch, ip, prefixLen, routes ):
32
    """Connect hosts to root namespace via switch. Starts network.
33
      network: Mininet() network object
34
      switch: switch to connect to root namespace
35
      ip: IP address for root namespace node
36
      prefixLen: IP address prefix length (e.g. 8, 16, 24)
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 route in routes:
48
        root.cmd( 'route add -net ' + route + ' dev ' + intf )
49

    
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:
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

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