From 0a2e9fa9f4605f11728ac1a0946041f3d0d58726 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Fri, 27 Mar 2015 22:19:36 +0000 Subject: [PATCH] Detecting router IP --- netwatch.py | 84 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 27 deletions(-) diff --git a/netwatch.py b/netwatch.py index 2bf8c34..31d5be5 100755 --- a/netwatch.py +++ b/netwatch.py @@ -12,32 +12,33 @@ import curses.wrapper from gi.repository import GLib, GObject # Pinging import subprocess +import socket, struct # Regex import re # Unicode import locale + +# Configure Unicode locale.setlocale(locale.LC_ALL, '') encoding = locale.getpreferredencoding() -# Variables -internet_host = "8.8.8.8" -router_host = "192.168.1.1" -ping_frequency = 5 - class Pinger: internet_log = [] router_log = [] def run(self): - internet_latency = self.ping(internet_host,self.internet_log) - self.draw("Internet:",self.internet_log,internet_latency,0) + self.stdscr.clear() # clear window during each run - router_latency = self.ping(router_host,self.router_log) - self.draw("Router:",self.router_log,router_latency,2) + internet_latency = self.ping(self.internet_host,self.internet_log) + self.draw("Internet:",self.internet_host,self.internet_log,internet_latency,0) + + router_latency = self.ping(self.router_host,self.router_log) + self.draw("Router:",self.router_host,self.router_log,router_latency,2) # Schedule next run GObject.timeout_add_seconds(self.timeout, self.run) + self.stdscr.refresh() # output drawing to screen def ping(self,host,log): @@ -67,61 +68,90 @@ class Pinger: return latency - def draw(self,title,log,latency,line): + def draw(self,title,subtitle,log,latency,line): # Draw internet heading on specified line - self.stdscr.addstr(line,0,title, curses.color_pair(3)) + self.stdscr.addstr(line,0,title, self.COL_DEFAULT) + self.stdscr.addstr(line,len(title)+1,subtitle, self.COL_MUTE) interpret = self.interpret_ping(latency) - self.stdscr.addstr(line,10,interpret['subjective'], interpret['color']) - right_align = self.ping_log_max_size-16 - self.stdscr.addstr(line,right_align,str(round(latency/1000,3))+" second lag", interpret['color']) + heading = str(round(latency/1000,3))+" second lag ("+interpret['subjective']+")" + right_align = self.ping_log_max_size-len(heading) + self.stdscr.addstr(line,right_align,heading, interpret['color']) # Draw graph on next line for idx,entry in enumerate(log): self.stdscr.addstr(line+1,idx,entry['graph'].encode(encoding), entry['color']) - self.stdscr.refresh() def interpret_ping(self, ping): ping = float(ping) if ping == -1: graph = "E "#u'\u2847' # Error - color = curses.color_pair(1) + color = self.COL_BAD subjective = "Down" elif ping < 30: graph = u'\u2840' - color = curses.color_pair(2) + color = self.COL_GOOD subjective = "Great" elif ping < 70: graph = u'\u2844' - color = curses.color_pair(2) + color = self.COL_GOOD subjective = "Good" elif ping < 120: graph = u'\u2846' - color = curses.color_pair(1) + color = self.COL_WARN subjective = "Fair" else: graph = u'\u2847' - color = curses.color_pair(1) + color = self.COL_BAD subjective = "Poor" return {'graph': graph, 'color': color, 'subjective': subjective, 'ping': float(ping)} +# def find_router_ip(self): +# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +# s.connect((self.internet_host,80)) +# my_ip = s.getsockname()[0] +# s.close() +# return my_ip + + def get_default_gateway_linux(self): + # Read the default gateway directly from /proc. + with open("/proc/net/route") as fh: + for line in fh: + fields = line.strip().split() + if fields[1] != '00000000' or not int(fields[3], 16) & 2: + continue + + return socket.inet_ntoa(struct.pack("