Statistics
| Branch: | Tag: | Revision:

mininet / examples / sshd.py @ 18aab5b7

History | View | Annotate | Download (2.99 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
import sys
20

    
21
from mininet.net import Mininet
22
from mininet.cli import CLI
23
from mininet.log import lg
24
from mininet.node import Node
25
from mininet.topolib import TreeTopo
26
from mininet.link import Link
27
from mininet.util import waitListening
28

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

    
34
def connectToRootNS( network, switch, ip, routes ):
35
    """Connect hosts to root namespace via switch. Starts network.
36
      network: Mininet() network object
37
      switch: switch to connect to root namespace
38
      ip: IP address for root namespace node
39
      routes: host networks to route to"""
40
    # Create a node in root namespace and link to switch 0
41
    root = Node( 'root', inNamespace=False )
42
    intf = Link( root, switch ).intf1
43
    root.setIP( ip, intf=intf )
44
    # 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
        root.cmd( 'route add -net ' + route + ' dev ' + str( intf ) )
49

    
50
def sshd( network, cmd='/usr/sbin/sshd', opts='-D',
51
          ip='10.123.123.1/32', routes=None, switch=None ):
52
    """Start a network, connect it to root ns, and run sshd on all hosts.
53
       ip: root-eth0 IP address in root namespace (10.123.123.1/32)
54
       routes: Mininet host networks to route to (10.0/24)
55
       switch: Mininet switch to connect to root namespace (s1)"""
56
    if not switch:
57
        switch = network[ 's1' ]  # switch to use
58
    if not routes:
59
        routes = [ '10.0.0.0/24' ]
60
    connectToRootNS( network, switch, ip, routes )
61
    for host in network.hosts:
62
        host.cmd( cmd + ' ' + opts + '&' )
63
    print "*** Waiting for ssh daemons to start"
64
    for server in network.hosts:
65
        waitListening( server=server, port=22, timeout=5 )
66

    
67
    print
68
    print "*** Hosts are running sshd at the following addresses:"
69
    print
70
    for host in network.hosts:
71
        print host.name, host.IP()
72
    print
73
    print "*** Type 'exit' or control-D to shut down network"
74
    CLI( network )
75
    for host in network.hosts:
76
        host.cmd( 'kill %' + cmd )
77
    network.stop()
78

    
79
if __name__ == '__main__':
80
    lg.setLogLevel( 'info')
81
    net = TreeNet( depth=1, fanout=4 )
82
    # get sshd args from the command line or use default args
83
    # useDNS=no -u0 to avoid reverse DNS lookup timeout
84
    argvopts = ' '.join( sys.argv[ 1: ] ) if len( sys.argv ) > 1 else (
85
        '-D -o UseDNS=no -u0' )
86
    sshd( net, opts=argvopts )