diff options
Diffstat (limited to '')
-rw-r--r-- | Makefile | 16 | ||||
-rw-r--r-- | examples/plugins/ipaddress | 29 | ||||
-rw-r--r-- | src/plugins/ipaddress.py | 207 |
3 files changed, 244 insertions, 8 deletions
@@ -16,14 +16,14 @@ PLUGIN_PATH = $(DATADIR)/$(PKGNAME) SRC = __main__ util x -PLUGINS = chase clock cpuifo cpuonline cpu df discstats leapsec \ - linereader loadavg lunar mem moc network pacman snmp \ - snmp6 softirqs solar uname uptime users vmstat weather \ - xdisplay xkb - -PLUGIN_EXAMPLES = chase clock cpu cpuinfo cpuonline df discstats \ - loadavg lunar mem moc network pacman uname \ - uptime users xdisplay xkb +PLUGINS = chase clock cpuifo cpuonline cpu df discstats ipaddress \ + leapsec linereader loadavg lunar mem moc network pacman \ + snmp snmp6 softirqs solar uname uptime users vmstat \ + weather xdisplay xkb + +PLUGIN_EXAMPLES = chase clock cpu cpuinfo cpuonline df discstats \ + ipaddress loadavg lunar mem moc network pacman \ + uname uptime users xdisplay xkb EXAMPLES = clock mixed moderate plugin-test test xmonad diff --git a/examples/plugins/ipaddress b/examples/plugins/ipaddress new file mode 100644 index 0000000..f29fdb4 --- /dev/null +++ b/examples/plugins/ipaddress @@ -0,0 +1,29 @@ +# -*- python -*- + +# A xpybar configuration example testing the features of plugins.ipaddress + +import time +import threading + +from plugins.ipaddress import IPAddress +from plugins.clock import Clock + + +OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True + + +clock = Clock(sync_to = 30 * Clock.MINUTES) + +start_ = start +def start(): + start_() + async(lambda : clock.continuous_sync(lambda : bar.invalidate())) + + +def redraw(): + addr = IPAddress('lo') + text = 'Addr: %s' % ', '.join(addr.public().keys()) + + bar.clear() + bar.draw_coloured_text(0, 10, 0, 2, text) + diff --git a/src/plugins/ipaddress.py b/src/plugins/ipaddress.py new file mode 100644 index 0000000..e150ecc --- /dev/null +++ b/src/plugins/ipaddress.py @@ -0,0 +1,207 @@ +# -*- python -*- +''' +xpybar – xmobar replacement written in python +Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + +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/>. +''' + +import os + +from util import * + + +class IPAddress: + ''' + Multi-home aware retrieval of public and private IP addresses + + @variable nics:dict<str, The state and IP address for a network interface card + (state:int Either of: `IPAddress.{DOWN,UNKOWN,ISOLATED,UP}` + private:str? The interface's private IPv4 address + private6:str? The interface's private IPv6 address + public:str?)> The interface's public IP address on the Internet + ''' + + + DOWN = 0 + ''' + The network interface is down + ''' + + UNKNOWN = 1 + ''' + The network interface's public IP address is unknown, + but it is connected to the Internet + ''' + + ISOLATED = 2 + ''' + The network interface's public IP address is unknown + because it does not appear to be connected to the Internet + ''' + + UP = 3 + ''' + The network interface is up and the public IP address is known + ''' + + + def __init__(self, *exclude): + ''' + Constructor + + @param exclude:*str Devices to exclude + ''' + nics = [d for d in os.listdir('/sys/class/net') if d not in exclude] + infos = [i for i in spawn_read('ifconfig').split('\n\n') if not i == ''] + self.nics = {} + for nic in nics: + state, private, private6, public = IPAddress.DOWN, None, None, None + 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) + 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() + if public is not None: + state = IPAddress.UP + elif not self.__isolated: + state = IPAddress.UNKNOWN + self.nics[nic] = (state, private, private6, public) + + + def public(self): + ''' + Get all unique public IP address + + @return :dist<address:str?, nics:list<str>> The IP addresses and the interfaces with those addresses + ''' + rc = {} + for nic in self.nics.keys(): + if self.nics[nic][3] in rc: + rc[self.nics[nic][3]].append(nic) + else: + rc[self.nics[nic][3]] = [nic] + return rc + + + def __site_0(self): + try: + data = spawn_read('wget', 'http://checkip.dyndns.org', '-O', '-') + if not data == '': + self.__isolated = False + return data.split('<body>')[1].split('</body>')[0].split(': ')[1] + except: + return None + + def __site_1(self): + try: + data = spawn_read('wget', 'http://ipecho.net/plain', '-O', '-') + if not data == '': + self.__isolated = False + data = data.strip() + return data if not data == '' else None + except: + return None + + def __site_2(self): + try: + data = spawn_read('wget', 'http://www.checkmyipaddress.org', '-O', '-') + if not data == '': + self.__isolated = False + data = [line.strip() for line in data.replace('\r\n', '\n').split('\n') if '</h3>' in line] + data = [line.split('>')[1].split('<')[0] for line in data] + data = [line for line in data if ' ' not in line] + return data[0] if not len(data) == 0 else None + except: + return None + + def __site_3(self): + try: + data = spawn_read('wget', 'http://www.ip-address.org', '-O', '-') + if not data == '': + self.__isolated = False + data = [line.strip(' \t') for line in data.replace('\r\n', '\n').split('\n') if 'ip += ' in line] + data = [line.split('"')[1] for line in data if len(line) - len(line.replace('"', '')) == 2] + data = [line.split('<')[0] for line in data] + data_ = [] + for line in data: + line_ = line + for c in '0123456789abcdefABCDEF.:': + line_ = line_.replace(c, '') + if not line_ == '': + data_.append(line_) + return data_[0] if not len(data_) == 0 else None + except: + return None + + def __site_4(self): + try: + data = spawn_read('wget', 'http://www.myipnumber.com/my-ip-address.asp', '-O', '-') + if not data == '': + self.__isolated = False + data = data.replace('\r\n', '\n').split('\nThe IP Address of this machine is:\n')[1] + return data.lower().split('\n')[0].split('<b>\n')[1].split('\n</b>')[0] + except: + return None + + def __site_5(self): + try: + data = spawn_read('wget', 'http://www.findipinfo.com', '-O', '-') + if not data == '': + self.__isolated = False + return data.split('Your IP Address Is: ')[1].split('<')[0] + except: + return None + + def __site_6(self): + try: + data = spawn_read('wget', 'http://what-ip.net', '-O', '-') + if not data == '': + self.__isolated = False + return data.split('Your IP Address is : ')[1].split('<b>')[1].split('</b>')[0] + except: + return None + + def __site_7(self): + try: + data = spawn_read('wget', 'http://my-ip-address.com', '-O', '-') + if not data == '': + self.__isolated = False + return data.split('<input ')[1].split('>')[0].split('value=')[1].split('"')[1] + except: + return None + + def __site_8(self): + try: + data = spawn_read('wget', 'https://duckduckgo.com?q=what is my ip address', '-O', '-') + if not data == '': + self.__isolated = False + return data.split('"Answer":"Your IP address is ')[1].split(' ')[0] + except: + return None + |