From 123ecac86250f7f3dd13a3f94758ed997a48cace Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 8 Dec 2014 03:52:44 +0100 Subject: proper multi-home support in ipaddress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- examples/plugins/ipaddress | 2 +- src/plugins/ipaddress.py | 57 +++++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/examples/plugins/ipaddress b/examples/plugins/ipaddress index f29fdb4..96572ee 100644 --- a/examples/plugins/ipaddress +++ b/examples/plugins/ipaddress @@ -22,7 +22,7 @@ def start(): def redraw(): addr = IPAddress('lo') - text = 'Addr: %s' % ', '.join(addr.public().keys()) + text = 'Addr: %s' % ', '.join(addr for addr in addr.public().keys() if addr is not None) bar.clear() bar.draw_coloured_text(0, 10, 0, 2, text) diff --git a/src/plugins/ipaddress.py b/src/plugins/ipaddress.py index 7f370d6..0b53800 100644 --- a/src/plugins/ipaddress.py +++ b/src/plugins/ipaddress.py @@ -71,22 +71,23 @@ class IPAddress: for info in infos: if info.startswith(nic + ': '): info = [i.lstrip().split(' ') for i in info.split('\n')[1:]] - info = dict((i[0], i[1]) for i in info) + info = dict((i[0], i[1]) for i in info if len(i) > 1) private = info['inet'] if 'inet' in info else None private6 = info['inet6'] if 'inet6' in info else None state = IPAddress.ISOLATED break if not state == IPAddress.DOWN: self.__isolated = True - public = self.__site_0() - if public is None: public = self.__site_1() - if public is None: public = self.__site_2() - if public is None: public = self.__site_3() - if public is None: public = self.__site_4() - if public is None: public = self.__site_5() - if public is None: public = self.__site_6() - if public is None: public = self.__site_7() - if public is None: public = self.__site_8() + download = lambda page : ('curl', '--interface', nic, page) + public = self.__site_0(download) + if public is None: public = self.__site_1(download) + if public is None: public = self.__site_2(download) + if public is None: public = self.__site_3(download) + if public is None: public = self.__site_4(download) + if public is None: public = self.__site_5(download) + if public is None: public = self.__site_6(download) + if public is None: public = self.__site_7(download) + if public is None: public = self.__site_8(download) if public is not None: state = IPAddress.UP elif not self.__isolated: @@ -109,18 +110,18 @@ class IPAddress: return rc - def __site_0(self): + def __site_0(self, download): try: - data = spawn_read('curl', 'http://checkip.dyndns.org') + data = spawn_read(*download('http://checkip.dyndns.org')) if not data == '': self.__isolated = False return data.split('')[1].split('')[0].split(': ')[1] except: return None - def __site_1(self): + def __site_1(self, download): try: - data = spawn_read('curl', 'http://ipecho.net/plain') + data = spawn_read(*download('http://ipecho.net/plain')) if not data == '': self.__isolated = False data = data.strip() @@ -128,9 +129,9 @@ class IPAddress: except: return None - def __site_2(self): + def __site_2(self, download): try: - data = spawn_read('curl', 'http://www.checkmyipaddress.org') + data = spawn_read(*download('http://www.checkmyipaddress.org')) if not data == '': self.__isolated = False data = [line.strip() for line in data.replace('\r\n', '\n').split('\n') if '' in line] @@ -140,9 +141,9 @@ class IPAddress: except: return None - def __site_3(self): + def __site_3(self, download): try: - data = spawn_read('curl', 'http://www.ip-address.org') + data = spawn_read(*download('http://www.ip-address.org')) if not data == '': self.__isolated = False data = [line.strip(' \t') for line in data.replace('\r\n', '\n').split('\n') if 'ip += ' in line] @@ -159,9 +160,9 @@ class IPAddress: except: return None - def __site_4(self): + def __site_4(self, download): try: - data = spawn_read('curl', 'http://www.myipnumber.com/my-ip-address.asp') + data = spawn_read(*download('http://www.myipnumber.com/my-ip-address.asp')) if not data == '': self.__isolated = False data = data.replace('\r\n', '\n').split('\nThe IP Address of this machine is:\n')[1] @@ -169,36 +170,36 @@ class IPAddress: except: return None - def __site_5(self): + def __site_5(self, download): try: - data = spawn_read('curl', 'http://www.findipinfo.com') + data = spawn_read(*download('http://www.findipinfo.com')) if not data == '': self.__isolated = False return data.split('Your IP Address Is: ')[1].split('<')[0] except: return None - def __site_6(self): + def __site_6(self, download): try: - data = spawn_read('curl', 'http://what-ip.net') + data = spawn_read(*download('http://what-ip.net')) if not data == '': self.__isolated = False return data.split('Your IP Address is : ')[1].split('')[1].split('')[0] except: return None - def __site_7(self): + def __site_7(self, download): try: - data = spawn_read('curl', 'http://my-ip-address.com') + data = spawn_read(*download('http://my-ip-address.com')) if not data == '': self.__isolated = False return data.split('')[0].split('value=')[1].split('"')[1] except: return None - def __site_8(self): + def __site_8(self, download): try: - data = spawn_read('curl', 'https://duckduckgo.com?q=what is my ip address') + data = spawn_read(*download('https://duckduckgo.com?q=what is my ip address')) if not data == '': self.__isolated = False return data.split('"Answer":"Your IP address is ')[1].split(' ')[0] -- cgit v1.2.3-70-g09d2