mininet / examples / sshd.py @ 17dbc7e0
History | View | Annotate | Download (2.8 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 |
|
28 |
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 |
|
33 |
def connectToRootNS( network, switch, ip, routes ): |
34 |
"""Connect hosts to root namespace via switch. Starts network.
|
35 |
network: Mininet() network object
|
36 |
switch: switch to connect to root namespace
|
37 |
ip: IP address for root namespace node
|
38 |
routes: host networks to route to"""
|
39 |
# Create a node in root namespace and link to switch 0
|
40 |
root = Node( 'root', inNamespace=False ) |
41 |
intf = Link( root, switch ).intf1 |
42 |
root.setIP( ip, intf=intf ) |
43 |
# Start network that now includes link to root namespace
|
44 |
network.start() |
45 |
# Add routes from root ns to hosts
|
46 |
for route in routes: |
47 |
root.cmd( 'route add -net ' + route + ' dev ' + str( intf ) ) |
48 |
|
49 |
def sshd( network, cmd='/usr/sbin/sshd', opts='-D', |
50 |
ip='10.123.123.1/32', routes=None, switch=None ): |
51 |
"""Start a network, connect it to root ns, and run sshd on all hosts.
|
52 |
ip: root-eth0 IP address in root namespace (10.123.123.1/32)
|
53 |
routes: Mininet host networks to route to (10.0/24)
|
54 |
switch: Mininet switch to connect to root namespace (s1)"""
|
55 |
if not switch: |
56 |
switch = network[ 's1' ] # switch to use |
57 |
if not routes: |
58 |
routes = [ '10.0.0.0/24' ]
|
59 |
connectToRootNS( network, switch, ip, routes ) |
60 |
for host in network.hosts: |
61 |
host.cmd( cmd + ' ' + opts + '&' ) |
62 |
print
|
63 |
print "*** Hosts are running sshd at the following addresses:" |
64 |
print
|
65 |
for host in network.hosts: |
66 |
print host.name, host.IP()
|
67 |
print
|
68 |
print "*** Type 'exit' or control-D to shut down network" |
69 |
CLI( network ) |
70 |
for host in network.hosts: |
71 |
host.cmd( 'kill %' + cmd )
|
72 |
network.stop() |
73 |
|
74 |
if __name__ == '__main__': |
75 |
lg.setLogLevel( 'info')
|
76 |
net = TreeNet( depth=1, fanout=4 ) |
77 |
# get sshd args from the command line or use default args
|
78 |
# useDNS=no -u0 to avoid reverse DNS lookup timeout
|
79 |
opts = ' '.join( sys.argv[ 1: ] ) if len( sys.argv ) > 1 else ( |
80 |
'-D -o UseDNS=no -u0' )
|
81 |
sshd( net, opts=opts ) |