Skip to content

Instantly share code, notes, and snippets.

@qiuchengxuan
Created December 10, 2017 03:50
Show Gist options
  • Save qiuchengxuan/fcc37fc1b7bd086d7feca4146870c8ed to your computer and use it in GitHub Desktop.
Save qiuchengxuan/fcc37fc1b7bd086d7feca4146870c8ed to your computer and use it in GitHub Desktop.
Create a lot of sshd host for testing, based on mininet
#!/usr/bin/python
import ipaddress
import argparse
from mininet.topo import Topo
from mininet.nodelib import LinuxBridge
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.util import waitListening
class SSHdTopo(Topo):
def __init__(self, bridge_name, num_hosts=4):
Topo.__init__(self)
hosts = [self.addHost('h%d' % i) for i in xrange(num_hosts)]
br = self.addSwitch(bridge_name, cls=LinuxBridge)
for host in hosts:
self.addLink(host, br)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='start ip and bridge name required.')
parser.add_argument('ip', help='Begining ip')
parser.add_argument('-n', '--num-of-hosts', dest='num_of_hosts', type=int, metavar='NUM-OF-HOSTS', default=100,
help='Amount of hosts to run, default is 100')
parser.add_argument('-b', '--bridge', dest='bridge_name', default='br0',
help='Linux bridge name, default is br0')
args = parser.parse_args()
topology = Mininet(SSHdTopo(args.bridge_name, args.num_of_hosts))
ip = ipaddress.ip_address(unicode(args.ip))
prefix = '16'
for host in topology.hosts:
host.setIP(str(ip) + '/' + prefix)
ip = ip + 1
host.cmd('/usr/sbin/sshd -D &')
br = topology[args.bridge_name]
br.start(topology['c0'])
br.cmd('brctl addif %s eth0' % args.bridge_name)
print "*** Enviroment ready, Type 'exit' or control-D to shut down network"
CLI(topology)
for host in topology.hosts:
host.cmd('kill %/usr/sbin/sshd')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment