carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 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 | from mininet.net import Mininet |
| 20 | from mininet.cli import CLI |
| 21 | from mininet.log import lg |
| 22 | from mininet.node import Node, OVSKernelSwitch |
| 23 | from mininet.topolib import TreeTopo |
| 24 | from mininet.link import Link |
| 25 | |
| 26 | 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 | |
| 31 | def connectToRootNS( network, switch, ip, prefixLen, routes ): |
| 32 | """Connect hosts to root namespace via switch. Starts network. |
| 33 | 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 | # Create a node in root namespace and link to switch 0 |
| 39 | root = Node( 'root', inNamespace=False ) |
| 40 | intf = Link( root, switch ).intf1 |
| 41 | root.setIP( ip, prefixLen, intf ) |
| 42 | # 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 ' + str( intf ) ) |
| 47 | |
| 48 | def sshd( network, cmd='/usr/sbin/sshd', opts='-D' ): |
| 49 | "Start a network, connect it to root ns, and run sshd on all hosts." |
| 50 | 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 | 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 | if __name__ == '__main__': |
| 69 | lg.setLogLevel( 'info') |
| 70 | net = TreeNet( depth=1, fanout=4, switch=OVSKernelSwitch ) |
| 71 | sshd( net ) |