From db8eb15dc846bf4865abc0c1556748894373eb13 Mon Sep 17 00:00:00 2001 From: Florian N Date: Fri, 7 Feb 2014 12:21:01 -0500 Subject: [PATCH] get interfaces --- main/views.py | 10 +++++++--- usage/views.py | 5 ++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/main/views.py b/main/views.py index 86d258d..ea85097 100755 --- a/main/views.py +++ b/main/views.py @@ -62,15 +62,19 @@ def get_ipaddress(): Get the IP Address """ try: - pipe = os.popen("/sbin/ifconfig |" + "grep -B1 'inet addr' |" + "awk '{ if ( $1 == \"inet\" ) { print $2 } else if ( $2 == \"Link\" ) { printf \"%s:\",$1 } }' |" + "awk -F: '{ print $1, $3 }'") + pipe = os.popen("/sbin/ifconfig |" + "grep -B1 'inet addr' |" + "awk '{ if ( $1 == \"inet\" ) { print $2 } else if ( $2 == \"Link\" ) { printf \"%s:\",$1 } }' |" + " awk -F: '{ print $1, $3 }'") data = pipe.read().strip().split('\n') pipe.close() data = [n for n in data if not n.startswith(('lo', '127'))] - itf = dict(zip(*[iter(data)] * 2)) data = [i.split(None, 2) for i in data] - + for e in data: + if len(e) > 2: + itf = dict(zip([iter(e[0])])) + else: + itf = [e[0]] + ips = {'interface': itf, 'itfip': data} data = ips diff --git a/usage/views.py b/usage/views.py index 5a7e715..f3a4354 100755 --- a/usage/views.py +++ b/usage/views.py @@ -251,7 +251,10 @@ def gettraffic(request): cookie_traffic = {} try: - traffic = get_traffic('eth0') + intf = get_ipaddress() + intf = intf['interface'][0] + + traffic = get_traffic(intf) except Exception: traffic = 0