-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomtelnet.py
69 lines (59 loc) · 1.77 KB
/
randomtelnet.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python3
# copyright 2017 Peter Dordal
# licensed under the Apache 2.0 license
# generates exponentially random traffic
# sleep...send...sleep...send...
from socket import *
from sys import argv
import os
import time
import threading
import math
import random
default_host = "localhost"
portnum = 5433
density = 0.01 # fraction of bottleneck link used by this traffic
packetsize = 210 # consider larger than real telnet
BottleneckBW = 10 # mbps
# BottleneckBW = 0.2 # should yield mean spacing of 1.0 sec
def talk():
global default_host, portnum, density, packetsize
rhost = default_host
if len(argv) > 1:
rhost = argv[1]
if len(argv) > 2:
portnum = int(argv[2])
print("Looking up address of " + rhost + "...", end="")
try:
dest = gethostbyname(rhost)
except gaierror as mesg:
errno,errstr=mesg.args
print("\n ", errstr);
return;
print("got it: " + dest)
addr=(dest, portnum)
s = socket()
try:
s.connect_ex(addr)
except:
print("connect to port ", portnum, " failed")
return
buf = bytearray(os.urandom(packetsize))
meanspacing = spacing(BottleneckBW, density)
while True:
rt = rtime(meanspacing)
time.sleep(rt)
try: s.send(buf)
except: return
def spacing(mbps, density):
global packetsize
# megabit/sec = kbit/ms
# time to send 1:
sizeKB = (packetsize+40.0)/1000
timetosend = sizeKB*8.0/mbps # in ms
return timetosend/density/1000.0
# ms is the mean packet spacing in units of ms
def rtime(ms):
x = random.random()
return -math.log(x)*ms
talk()