Statistics
| Branch: | Tag: | Revision:

mininet / examples / sshd.py @ 5a646a0d

History | View | Annotate | Download (2.63 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 5a646a0d Brian O'Connor
import sys
20
21 259d7133 Bob Lantz
from mininet.net import Mininet
22 91b9e374 Bob Lantz
from mininet.cli import CLI
23
from mininet.log import lg
24 11782ae0 Bob Lantz
from mininet.node import Node, OVSKernelSwitch
25 91b9e374 Bob Lantz
from mininet.topolib import TreeTopo
26 a49c85a6 Bob Lantz
from mininet.link import Link
27 42ba5d92 Bob Lantz
28 91b9e374 Bob Lantz
def TreeNet( depth=1, fanout=2, **kwargs ):
29
    "Convenience function for creating tree networks."
30
    topo = TreeTopo( depth, fanout )
31
    return Mininet( topo, **kwargs )
32 80be5642 Bob Lantz
33 91b9e374 Bob Lantz
def connectToRootNS( network, switch, ip, prefixLen, routes ):
34 80be5642 Bob Lantz
    """Connect hosts to root namespace via switch. Starts network.
35 91b9e374 Bob Lantz
      network: Mininet() network object
36
      switch: switch to connect to root namespace
37
      ip: IP address for root namespace node
38
      prefixLen: IP address prefix length (e.g. 8, 16, 24)
39
      routes: host networks to route to"""
40 80be5642 Bob Lantz
    # Create a node in root namespace and link to switch 0
41
    root = Node( 'root', inNamespace=False )
42 a49c85a6 Bob Lantz
    intf = Link( root, switch ).intf1
43
    root.setIP( ip, prefixLen, intf )
44 80be5642 Bob Lantz
    # Start network that now includes link to root namespace
45
    network.start()
46
    # Add routes from root ns to hosts
47
    for route in routes:
48 a49c85a6 Bob Lantz
        root.cmd( 'route add -net ' + route + ' dev ' + str( intf ) )
49 55dd9368 Bob Lantz
50 a16fae67 Bob Lantz
def sshd( network, cmd='/usr/sbin/sshd', opts='-D' ):
51 80be5642 Bob Lantz
    "Start a network, connect it to root ns, and run sshd on all hosts."
52 cf6f6704 Bob Lantz
    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 80be5642 Bob Lantz
    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 98332cb6 Bob Lantz
if __name__ == '__main__':
71 80be5642 Bob Lantz
    lg.setLogLevel( 'info')
72 11782ae0 Bob Lantz
    net = TreeNet( depth=1, fanout=4, switch=OVSKernelSwitch )
73 5a646a0d Brian O'Connor
    # get sshd args from the command line; default: -D
74
    opts = ' '.join( sys.argv[ 1: ] ) if len( sys.argv ) > 1 else '-D'
75
    sshd( net, opts=opts )