forked from abhi005/sdn-attack-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraffic.py
49 lines (38 loc) · 1.38 KB
/
traffic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import random
from mininet.net import Mininet
import sys
import threading, time
from os.path import expanduser
import subprocess
from datetime import datetime
flush = sys.stdout.flush
# log file
LOG_FILE = 'traffic.log'
# traffic generator script - called in abilene_topo.py script
class TrafficGenerator:
def __init__(self, net):
self.net = net
th = threading.Thread(target=self.start)
th.daemon = True
th.start()
# method for background process
def start(self):
time.sleep(5)
while True:
# selecting distinct src and dst randomly from available hosts
while True:
i, j = random.randint(0, 9), random.randint(0, 9)
if i != j:
break
src, dst = self.net.hosts[i], self.net.hosts[j]
f = open(LOG_FILE, "a+")
curr_timestamp = datetime.utcnow().strftime('%s')
f.write(curr_timestamp + " src_mac: " + str(src.params['mac']) + " dst_mac: " + str(dst.params['mac']) + "\n")
# creating server at dst host
res = dst.popen('iperf -s -u')
# creating client at src host
res = src.popen("iperf -f m -c %s -u -b 200m -t 30" % (dst.IP()), stdout=subprocess.PIPE)
# sleeping for 10 seconds before another run
time.sleep(10)
f.close()
flush()