Skip to content
Snippets Groups Projects
sshd.py 1.72 KiB
Newer Older
Bob Lantz's avatar
Bob Lantz committed
"""
Create a network and start sshd(8) on each host.
Bob Lantz's avatar
Bob Lantz committed
While something like rshd(8) would be lighter and faster,
(and perfectly adequate on an in-machine network)
the advantage of running sshd is that scripts can work
unchanged on mininet and hardware.
Bob Lantz's avatar
Bob Lantz committed
"""

import sys ; readline = sys.stdin.readline
from mininet.mininet import init, Node, createLink, TreeNet, Cli

def nets( hosts ):
   "Return list of networks (/24) for hosts."
   nets = {}
   for host in hosts:
      net = host.IP().split( '.' )[ : -1 ]
      net = '.'.join( net ) + '.0/24'
Bob Lantz's avatar
Bob Lantz committed
def connectToRootNS( network, switch ):
   "Connect hosts to root namespace via switch. Starts network."
   # Create a node in root namespace and link to switch 0
   root = Node( 'root', inNamespace=False )
Bob Lantz's avatar
Bob Lantz committed
   createLink( root, switch )
   ip = '10.0.123.1'
   root.setIP( root.intfs[ 0 ], ip, '/24' )
Bob Lantz's avatar
Bob Lantz committed
   # Start network that now includes link to root namespace
Bob Lantz's avatar
Bob Lantz committed
   routes = nets( network.hosts )
   intf = root.intfs[ 0 ]
   for net in routes:
      root.cmdPrint( 'route add -net ' + net + ' dev ' + intf )

def sshd( network ):
   "Start a network, connect it to root ns, and run sshd on all hosts."
Bob Lantz's avatar
Bob Lantz committed
   connectToRootNS( network, network.switches[ 0 ] )
   for host in network.hosts: host.cmd( 'sshd -D &' )
   print
   print "*** Hosts are running sshd at the following addresses:"
   print
Bob Lantz's avatar
Bob Lantz committed
   for host in network.hosts: print host.name, host.IP()
   print
   print "*** Press return to shut down network: ",
   readline()
   for host in network.hosts: host.cmd( 'kill %sshd')
   
if __name__ == '__main__':
   init()
   network = TreeNet( depth=1, fanout=4, kernel=True )
   sshd( network )