-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNetLatency.py
More file actions
136 lines (119 loc) · 5.54 KB
/
NetLatency.py
File metadata and controls
136 lines (119 loc) · 5.54 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import paramiko
import regex
import datetime
import tabulate
class SSHClientPlus:
def __init__(self, hostname, user, password):
self._sshClient = paramiko.SSHClient()
self._sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self._hostname = hostname
self._user = user
self._password = password
def connect(self):
self._sshClient.connect(self._hostname,username=self._user,password=self._password)
def disconnect(self):
self._sshClient.close()
def execute(self, command):
print command
command = 'echo $$; exec ' + command
stdin, stdout, stderr = self._sshClient.exec_command(command)
pid = int(stdout.readline())
return pid, stdin, stdout, stderr
class SSHClientLatencyTarget(SSHClientPlus):
def __init__(self, hostname, ipaddress, user, password):
SSHClientPlus.__init__(self,hostname, user, password)
self.connect()
self._IPAddress = ipaddress
def startServer(self):
# get network card name from ipaddress
command = "ifconfig -a | grep -E 'flags|inet' | sed 's/inet//g' | awk '{print $1}' | sed '$!N;s/\\n/ /' | sed 's/://' | grep %s" %(self._IPAddress)
pid,stdin,stdout,stderr = self.execute(command)
line = stdout.readline()
print line
items = line.split()
if len(items)!=2:
return None
networkCardName = items[0]
tcpdumpCommand = "tcpdump -ttt -i %s ip proto \\\\icmp and host %s" %(networkCardName,self._IPAddress)
self._pid, stdin, self._stdout, self._stderr = self.execute(tcpdumpCommand)
return networkCardName
def stopServer(self):
self.execute("kill %d" %(self._pid))
def parseLatency(self):
"""
Output sample:
00:00:00.000000 IP host1 > 192.168.0.1: ICMP echo request, id 94, seq 0, length 508
00:00:00.000027 IP 192.168.0.1 > host1: ICMP echo reply, id 94, seq 0, length 508
00:00:01.000067 IP host1 > 192.168.0.1: ICMP echo request, id 94, seq 1, length 508
00:00:00.000024 IP 192.168.0.1 > host1: ICMP echo reply, id 94, seq 1, length 508
00:00:01.000052 IP host1 > 192.168.0.1: ICMP echo request, id 94, seq 2, length 508
00:00:00.000042 IP 192.168.0.1 > host1: ICMP echo reply, id 94, seq 2, length 508
00:00:01.000026 IP host1 > 192.168.0.1: ICMP echo request, id 94, seq 3, length 508
00:00:00.000038 IP 192.168.0.1 > host1: ICMP echo reply, id 94, seq 3, length 508
"""
microsecond = 0
pingTimes = 0
for line in self._stderr.readlines():
print line
print "====================================================================="
for line in self._stdout.readlines():
print line
if line.find("ICMP echo reply")!=-1:
delta = datetime.datetime.strptime(line.split()[0],"%H:%M:%S.%f")
pingTimes = pingTimes+1
microsecond = microsecond + delta.microsecond + delta.second*1000 + delta.minute*60*1000
return microsecond/pingTimes
class SSHClientLatencySource(SSHClientPlus):
def __init__(self, hostname, user, password):
SSHClientPlus.__init__(self,hostname,user,password)
self.connect()
def getIPaddressFromHostname(self, hostname):
self._targetHostname = hostname
pid,stdin,stdout,stderr = self.execute("ping %s 500 1" %(self._targetHostname))
line = stdout.readline()
IPRegex = regex.compile("\d+\.\d+\.\d+\.\d+")
IPAddress = IPRegex.findall(line)
if len(IPAddress)!=1:
return None
return IPAddress[0]
def pingTarget(self):
pid,stdin,stdout,stderr = self.execute("ping %s 500 4" %(self._targetHostname))
stdout.readlines() # wait for the ping command to finish
def testlatency(source, target, targetInternal):
sshSource = SSHClientLatencySource(source,"root","passw0rd")
targetIP = sshSource.getIPaddressFromHostname(targetInternal)
sshTarget = SSHClientLatencyTarget(target,targetIP,"root","passw0rd")
etherName = sshTarget.startServer()
if etherName==None:
return "Error","Unknown",targetIP
sshSource.pingTarget()
sshTarget.stopServer()
return sshTarget.parseLatency(),etherName,targetIP
import ConfigParser
class NetworkCfg:
def __init__(self, filename):
self._config = ConfigParser.RawConfigParser()
self._config.read(filename)
self._jobList = []
for section in self._config.sections():
try:
srcHost = self._config.get(section,"SRC_HOST")
dstHost = self._config.get(section,"DST_HOST")
except NoOptionError:
print "Error parsing section",section
continue
try:
dstHostInternal = self._config.get(section,"DST_HOST_INTERNAL")
except NoOptionError:
dstHostInternal = dstHost
self._jobList.append((section,srcHost,dstHost,dstHostInternal))
def getJobList(self,):
return self._jobList
if __name__ == '__main__':
tableContent = []
tableHeader = ["source","target","Ethernet","IP","Latency (micro-second)"]
cfg = NetworkCfg("network.cfg")
for (section,source,target,targetInternal) in cfg.getJobList():
latency,etherName,targetIP = testlatency(source,target,targetInternal)
tableContent.append([source,target,etherName,targetIP,latency])
print tabulate.tabulate(tableContent,headers=tableHeader,tablefmt="orgtbl")