Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / mininet / examples / sshd.py
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, info
24 from mininet.node import Node
25 from mininet.topolib import TreeTopo
26 from mininet.util import waitListening
27
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 = network.addLink( 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     info( "*** Waiting for ssh daemons to start\n" )
64     for server in network.hosts:
65         waitListening( server=server, port=22, timeout=5 )
66
67     info( "\n*** Hosts are running sshd at the following addresses:\n" )
68     for host in network.hosts:
69         info( host.name, host.IP(), '\n' )
70     info( "\n*** Type 'exit' or control-D to shut down network\n" )
71     CLI( network )
72     for host in network.hosts:
73         host.cmd( 'kill %' + cmd )
74     network.stop()
75
76 if __name__ == '__main__':
77     lg.setLogLevel( 'info')
78     net = TreeNet( depth=1, fanout=4 )
79     # get sshd args from the command line or use default args
80     # useDNS=no -u0 to avoid reverse DNS lookup timeout
81     argvopts = ' '.join( sys.argv[ 1: ] ) if len( sys.argv ) > 1 else (
82         '-D -o UseDNS=no -u0' )
83     sshd( net, opts=argvopts )