基于Mininet的网络拓扑搭建代码
1 import logging 2 import os 3 import time 4 import thread 5 import multiprocessing 6 7 from mininet.net import Mininet 8 from mininet.node import RemoteController 9 from mininet.cli import CLI 10 from mininet.log import setLogLevel 11 from mininet.link import TCLink 12 from mininet.topo import Topo 13 14 logger = logging.getLogger(__name__) 15 16 REQUEST_NUMBER = 3 17 18 class NMTree(Topo): 19 logger.debug("Class NMTree") 20 switchList = [] 21 hostList = [] 22 23 def __init__(self,level_number,node_number): 24 logger.info("Class NMTree init") 25 self.level_number = level_number 26 self.node_number = node_number 27 #init Topo 28 Topo.__init__(self) 29 30 def create_topo(self): 31 self.create_switch(self.level_number) 32 self.create_host(self.level_number,self.node_number) 33 34 def create_switch(self,number): 35 logger.debug("Create switch") 36 for i in range(1,number+1): 37 index = 0 38 prefix = "s"+str(i) 39 if i == 1: 40 self.switchList.append(self.addSwitch(prefix + str(i))) 41 else: 42 for k in range(1,2**(i-1)+1): 43 self.switchList.append(self.addSwitch(prefix+str(k))) 44 self.addLink(self.switchList[2**(i-2)+index-1],self.switchList[2**(i-1)+k-2]) 45 if k % 2 == 0: 46 index += 1 47 48 def create_host(self,level,node): 49 logger.debug("Create host") 50 for i in range(1,2**(level-1)+1): 51 prefix = "h"+str(i) 52 for j in range(1,node+1): 53 self.hostList.append(self.addHost(prefix+str(j))) 54 self.addLink(self.switchList[2**(level-1)+i-2],self.hostList[(i-1)*node+j-1]) 55 56 57 def start_log_server(net, topo): 58 time.sleep(15) 59 60 # h21 ip = 10.0.0.4 61 h21 = net.get(topo.hostList[3]) 62 # Start ITGLog server on h21 63 h21.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGLog") 64 65 66 def start_recv_server(net, topo): 67 time.sleep(10) 68 # h23 ip = 10.0.0.6 69 h23 = net.get(topo.hostList[5]) 70 # Start ITGRecv server on h23 71 h23.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGRecv") 72 73 74 def generate_traffic_by_n_itg(net, topo): 75 time.sleep(20) 76 77 pool = multiprocessing.Pool(processes=3) 78 # h13 ip = 10.0.0.3 79 h13 = net.get(topo.hostList[2]) 80 81 # Start ITGSend server on h13 and send TCP streams to h23 82 pool.apply_async(h13.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGSend -a 10.0.0.6 -rp 10001 -c 100 -C 10 -t 80000 \ 83 -T TCP -l send_tcp_log -L 10.0.0.4 TCP -x recv_tcp_log -X 10.0.0.4 TCP")) 84 logger.info("end send tcp") 85 # Start ITGSend server on h13 and send UDP streams to h23 86 pool.apply_async(h13.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGSend -a 10.0.0.6 -rp 10001 -c 100 -C 10 -t 15000 \ 87 -T UDP -l send_udp_log -L 10.0.0.4 UDP -x recv_udp_log -X 10.0.0.4 UDP")) 88 logger.info("end send udp") 89 # Start ITGSend server on h13 and send ICMP streams to h23 90 pool.apply_async(h13.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGSend -a 10.0.0.6 -rp 10001 -c 100 -C 10 -t 5000 \ 91 -T ICMP -l send_icmp_log -L 10.0.0.4 TCP -x recv_icmp_log -X 10.0.0.4 TCP")) 92 logger.info("end send icmp") 93 pool.close() 94 pass 95 96 97 def create_topo(): 98 logging.debug("Create NMTree") 99 topo = NMTree(3,3) 100 topo.create_topo() 101 logging.debug("Start Mininet") 102 controller_ip = "127.0.0.1" 103 controller_port = 6633 104 net = Mininet(topo=topo, link=TCLink, controller=None, autoSetMacs=True) 105 net.addController('controller',controller=RemoteController,ip=controller_ip, port=controller_port) 106 net.start() 107 thread.start_new_thread(generate_traffic_by_n_itg, (net, topo)) 108 thread.start_new_thread(start_log_server, (net, topo)) 109 thread.start_new_thread(start_recv_server, (net, topo)) 110 CLI(net) 111 net.stop() 112 113 if __name__ == '__main__': 114 setLogLevel('info') 115 if os.getuid() != 0: 116 logger.debug("You are NOT root") 117 elif os.getuid() == 0: 118 create_topo()
本文来自博客园,作者:Jcpeng_std,转载请注明原文链接:https://www.cnblogs.com/JCpeng/p/14990034.html