-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathping.inc.py
executable file
·146 lines (130 loc) · 4.02 KB
/
ping.inc.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
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
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Pinger.py -- A ping tool that sits in your system tray
# Copyright 2013 Will Bradley
#
# Contributors: Will Bradley <[email protected]>
# AltF4 <[email protected]>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of either or both of the following licenses:
#
# 1) the GNU Lesser General Public License version 3, as published by the
# Free Software Foundation; and/or
# 2) the GNU Lesser General Public License version 2.1, as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the applicable version of the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of both the GNU Lesser General Public
# License version 3 and version 2.1 along with this program. If not, see
# <http://www.gnu.org/licenses/>
#
#
# Dependencies
#
# System Tray Icon
# Timer
# Date
import datetime
# Pinging
import subprocess
# Regex
import re
# Ctrl-c
import signal
# File paths
import os
# OS naming
import platform
# Argument parsing
import argparse
# For exit
import sys
# For graphing
# For IP addresses
import socket, struct
# For API
import json
#
# Main Class
#
class Pinger:
def ping(self, target, log=None, widget=None, data=None):
pingResult = Util.doPing(target)
ssidResult = Util.getSsid()
print json.dumps({
'target': target,
'ssid': ssidResult,
'loss': float(pingResult['loss']),
'rtt_avg': float(pingResult['rtt_avg']),
})
def __init__(self):
# Print welcome message
print "Starting Pinger..."
self.ping("4.2.2.2")
# Print started message
print "Finished."
#
# Utility class for platform-agnosticism and reusability
#
class Util:
@staticmethod
def getSsid():
if platform.system() == "Linux":
ssid = subprocess.Popen(["nmcli","-t","-f","active,ssid","dev","wifi"],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
) # | egrep '^yes(.*)' | cut -d\' -f2
out, error = ssid.communicate()
m = re.search(r'yes:(.*)', out)
result = m.group(1)
elif platform.system() == "Darwin":
ssid = subprocess.Popen(["/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport","-I"],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, error = ssid.communicate()
result = out
return result
@staticmethod
def doPing(target):
if platform.system() == "Linux":
ping = subprocess.Popen(
["ping", "-c", "5", target],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
pingOut, pingError = ping.communicate()
# Doing two regexes because Python doesn't like doing both in one
lossResult = re.search(r' ([\d\.]+)% packet loss', pingOut, re.MULTILINE)
rttAvgResult = re.search(r' = [\d\.]+/([\d\.]+)/[\d\.]+/[\d\.]+', pingOut, re.MULTILINE)
if pingError or lossResult is None or rttAvgResult is None:
output = {'loss':-1, 'rtt_avg':-1};
else:
output = {'loss':lossResult.group(1), 'rtt_avg':rttAvgResult.group(1)};
elif platform.system() == "Darwin": # Macintosh
ping = subprocess.Popen(
["ping", "-c", "5", target],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
pingOut, pingError = ping.communicate()
# print out
lossResult = re.search(r' ([\d\.]+)% packet loss', pingOut, re.MULTILINE)
rttAvgResult = re.search(r' = [\d\.]+/([\d\.]+)/[\d\.]+/[\d\.]+', pingOut, re.MULTILINE)
if pingError or lossResult is None or rttAvgResult is None:
output = {'loss':-1, 'rtt_avg':-1};
else:
output = {'loss':lossResult.group(1), 'rtt_avg':rttAvgResult.group(1)};
return output
#
# Runtime
#
if __name__ == "__main__":
pinger = Pinger()