Statistics
| Branch: | Tag: | Revision:

mininet / examples / sshd.py @ 8a130dea

History | View | Annotate | Download (2.49 KB)

1 42ba5d92 Bob Lantz
#!/usr/bin/python
2
3 55dd9368 Bob Lantz
"""
4 98332cb6 Bob Lantz
Create a network and start sshd(8) on each host.
5 42ba5d92 Bob Lantz

6 55dd9368 Bob Lantz
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 2f534913 Bob Lantz

11 91b9e374 Bob Lantz
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 55dd9368 Bob Lantz
"""
18
19 259d7133 Bob Lantz
from mininet.net import Mininet
20 91b9e374 Bob Lantz
from mininet.cli import CLI
21
from mininet.log import lg
22 11782ae0 Bob Lantz
from mininet.node import Node, OVSKernelSwitch
23 91b9e374 Bob Lantz
from mininet.topolib import TreeTopo
24
from mininet.util import createLink
25 42ba5d92 Bob Lantz
26 91b9e374 Bob Lantz
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 80be5642 Bob Lantz
31 91b9e374 Bob Lantz
def connectToRootNS( network, switch, ip, prefixLen, routes ):
32 80be5642 Bob Lantz
    """Connect hosts to root namespace via switch. Starts network.
33 91b9e374 Bob Lantz
      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 80be5642 Bob Lantz
    # Create a node in root namespace and link to switch 0
39
    root = Node( 'root', inNamespace=False )
40 1a9d2eac Bob Lantz
    intf = createLink( root, switch )[ 0 ]
41
    root.setIP( intf, ip, prefixLen )
42 80be5642 Bob Lantz
    # Start network that now includes link to root namespace
43
    network.start()
44
    # Add routes from root ns to hosts
45
    for route in routes:
46
        root.cmd( 'route add -net ' + route + ' dev ' + intf )
47 55dd9368 Bob Lantz
48 a16fae67 Bob Lantz
def sshd( network, cmd='/usr/sbin/sshd', opts='-D' ):
49 80be5642 Bob Lantz
    "Start a network, connect it to root ns, and run sshd on all hosts."
50 cf6f6704 Bob Lantz
    switch = network.switches[ 0 ]  # switch to use
51
    ip = '10.123.123.1'  # our IP address on host network
52
    routes = [ '10.0.0.0/8' ]  # host networks to route to
53 80be5642 Bob Lantz
    connectToRootNS( network, switch, ip, 8, routes )
54
    for host in network.hosts:
55
        host.cmd( cmd + ' ' + opts + '&' )
56
    print
57
    print "*** Hosts are running sshd at the following addresses:"
58
    print
59
    for host in network.hosts:
60
        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:
65
        host.cmd( 'kill %' + cmd )
66
    network.stop()
67
68 98332cb6 Bob Lantz
if __name__ == '__main__':
69 80be5642 Bob Lantz
    lg.setLogLevel( 'info')
70 11782ae0 Bob Lantz
    net = TreeNet( depth=1, fanout=4, switch=OVSKernelSwitch )
71 80be5642 Bob Lantz
    sshd( net )