-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaping
executable file
·159 lines (132 loc) · 4.36 KB
/
aping
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
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# vim: set tabstop=4 expandtab:
'''
Audio Ping.
Accepts the same arguments as the regular "ping" command.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
@author: Vincent Petry <[email protected]>
'''
import sys
import pexpect
# config
colorsEnabled = True
soundEnabled = True
# Constants
ESCAPE='\033['
SOUND_PACKET_RECEIVED='packetreceived.wav'
SOUND_PACKET_LOST='packetlost.wav'
SOUND_MANAGER="PygameSoundManager"
def printLine(row, state):
if not colorsEnabled:
print row,
return
if state:
# green
print ESCAPE + '0;32m\r',
else:
# red
print ESCAPE + '0;31m\r',
print row,
class PygameSoundManager:
def __init__(self):
self.soundPacketReceived = None
self.soundPacketLost = None
def init(self):
try:
# Playing sound in python is hard, using pygame for now
import pygame
except ImportError:
sys.stderr.write("pygame library not available for pygame.mixer\n")
return False
pygame.mixer.pre_init(44100,-16, 2, 256)
pygame.mixer.init()
self.soundPacketReceived = pygame.mixer.Sound(SOUND_PACKET_RECEIVED)
self.soundPacketLost = pygame.mixer.Sound(SOUND_PACKET_LOST)
if not self.soundPacketReceived and not self.soundPacketLost:
pygame.mixer.quit()
return False
return True
def release(self):
import pygame
pygame.mixer.quit()
def play(self, state):
if state:
self.soundPacketReceived.play()
else:
self.soundPacketLost.play()
def formatArgs(args):
s = ''
for arg in args:
if ' ' in arg:
s = '"' + arg + '" '
else:
s = s + arg + ' '
return s
def run(pingArgs):
soundManager = None
if soundEnabled:
SoundManagerClass = getattr(sys.modules[__name__], SOUND_MANAGER)
soundManager = SoundManagerClass()
if not soundManager.init():
sound = False
cmd = 'ping ' + formatArgs(pingArgs)
prevSeq = 0
try:
thread = pexpect.spawn(cmd)
cpl = thread.compile_pattern_list([
pexpect.EOF,
"^.*icmp_seq=([0-9]*).*$",
".*"
])
while True:
i = thread.expect_list(cpl, timeout=None)
if i == 0: # EOF
break
elif i == 1:
state = False
seq = int(thread.match.group(1))
if (seq == prevSeq + 1 or seq == 1):
state = True
if soundManager:
soundManager.play(True)
else:
if (prevSeq - seq > 1):
printLine('Lost packets %i to %i' % (prevSeq + 1, seq - 1), False)
else:
printLine('Lost packet %i' % (prevSeq + 1), False)
print
if soundManager:
soundManager.play(False)
printLine(thread.match.group(0), True),
prevSeq = seq
elif i == 2:
unknown_line = thread.match.group(0)
if colorsEnabled:
print ESCAPE + 'm'
print unknown_line,
except KeyboardInterrupt:
if colorsEnabled:
print ESCAPE + 'm'
thread.terminate()
l = thread.readline()
# Display post-kill output
while l:
print l,
l = thread.readline()
if soundManager:
soundManager.release()
if not sys.stdout.isatty():
colorsEnabled = False
if len(sys.argv) == 1:
print "Audio Ping: accepts the same arguments as the \"ping\" command";
args = list(sys.argv)[1:]
run(args)