-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor_outbound_network.py
executable file
·63 lines (48 loc) · 1.87 KB
/
monitor_outbound_network.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
#!/usr/bin/python3
#filename: monitor_outbound_netowork.py
#description: monitor the outbound connection.
import subprocess
import sys,os
import re
import datetime
destination="google.com"
def get_ip_of_target_hop(destination,hops=3):
last_line=""
traceroute_cmd=("traceroute -m %s %s| awk {'print $2'}" % (hops,destination)) #get the ip
proc = subprocess.Popen(traceroute_cmd, shell=True,stdout=subprocess.PIPE)
traceroute = proc.stdout.readlines()
for line in traceroute:
#print(line)
last_line=line.decode("utf-8").rstrip()
return last_line
def ping_target(ip,packets=3):
ping_cmd=("ping -c %s %s" % (packets,ip)) #ping ip
proc = subprocess.Popen(ping_cmd, shell=True,stdout=subprocess.PIPE)
ping = proc.stdout.readlines()
regex= re.compile('.*packets trans.*',re.IGNORECASE)
for line in ping:
#print(line.decode("utf-8").rstrip())
if regex.match(line.decode("utf-8").rstrip()):
return line.decode("utf-8").rstrip()
def write_line_to_log(logfile,input_line):
f=open(logfile,'a')
f.write(input_line + "\n")
f.close
my_ip=get_ip_of_target_hop(destination) #get the target to ping
print ("target ip is '%s'" % my_ip)
output=ping_target(my_ip) #get ping output summary
t=datetime.datetime.now()
mytime = (t.strftime("%a %x %X")) # logging style
mylog_date=(t.strftime("%m-%d-%y")) # filename style
myline = ("%s | %s" % (mytime,output) ) #log entry
print(myline)
log_filename=("/var/tmp/net_monitor_%s.log" % mylog_date) #log file
write_line_to_log(log_filename,myline) #write to log file
#notify us verbally of network packets dropping
regex = re.compile('.*0% packet.*',re.IGNORECASE)
if not regex.match:
if os.path.exists("/usr/bin/espeak"):
message=("Warning We are dropping packets.")
cmd_talk=("espeak -a 500 -p 1 ")
cmd_echo=("echo " + message)
status = subprocess.call(cmd_echo + "|" + cmd_talk , shell=True)