diff options
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/__init__.py | 0 | ||||
-rw-r--r-- | helpers/administration.py | 0 | ||||
-rw-r--r-- | helpers/netif.py | 68 | ||||
-rw-r--r-- | helpers/sysstatus.py | 49 | ||||
-rw-r--r-- | helpers/tor.py | 0 | ||||
-rw-r--r-- | helpers/util.py | 24 | ||||
-rw-r--r-- | helpers/wireless.py | 0 |
7 files changed, 141 insertions, 0 deletions
diff --git a/helpers/__init__.py b/helpers/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/helpers/__init__.py diff --git a/helpers/administration.py b/helpers/administration.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/helpers/administration.py diff --git a/helpers/netif.py b/helpers/netif.py new file mode 100644 index 0000000..6d7a194 --- /dev/null +++ b/helpers/netif.py @@ -0,0 +1,68 @@ + +import os + +from util import * + +def parse_ip(ifname): + """ + Example ip link show string: + + 2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000 + link/ether 00:12:34:56:78:90 brd ff:ff:ff:ff:ff:ff + """ + d = dict() + ipinfo = cli_read_lines('ip addr show %s' % ifname) + if 0 == len(ipinfo): + raise KeyError('No such interface: ' + ifname) + iplinkl = ipinfo[0].split() + d['ifname'] = iplinkl[1][:-1] + val_indexes = {'mtu': 3, 'qdisc': 5, 'state': 7, 'mode': 9, 'qlen': 11} + for k, v in val_indexes.iteritems(): + if len(iplinkl) > v and iplinkl[v] == k: + d[k] = iplinkl[v+1] + d['ipv4addrs'] = list() + d['ipv6addrs'] = list() + for l in ipinfo[1:]: + # iterate through the address lines + l = l.split() + if l[0] == "link/ether": + d['mac'] = l[1] + elif l[0] == "inet": + d['ipv4addrs'].append(dict( + addr=l[1].split('/')[0], + prefix=int(l[1].split('/')[1]), + mask=prefix_to_ipv4_mask(int(l[1].split('/')[1])), + scope=l[5])) + elif l[0] == "inet6": + d['ipv6addrs'].append(dict( + addr=l[1].split('/')[0], + prefix=int(l[1].split('/')[1]), + scope=l[3])) + print d['ipv6addrs'] + return d + +def get_wan_status(ifname='eth1'): + d = dict() + try: + d.update(parse_ip(ifname)) + except KeyError: + None + return d + +def get_lan_status(ifname='eth0'): + d = dict() + try: + d.update(parse_ip(ifname)) + except KeyError: + None + return d + +def get_wireless_status(ifname='wlan0'): + d = dict() + try: + d.update(parse_ip(ifname)) + except KeyError, ke: + raise ke + return None + return d + diff --git a/helpers/sysstatus.py b/helpers/sysstatus.py new file mode 100644 index 0000000..b2eec6f --- /dev/null +++ b/helpers/sysstatus.py @@ -0,0 +1,49 @@ + +import os + +from util import * + +def get_system_status(): + d = dict() + d['hostname'] = cli_read('hostname') + d['fqdn'] = cli_read('hostname -f') + d['domain'] = cli_read('hostname -d') + d['current_time'] = cli_read('date -R') + d['uptime'] = cli_read('uptime') + return d + +def get_dmesg(): + try: + return cli_read('dmesg') + except IOError: + return None + +def get_authlog(): + try: + with open('/var/log/auth.log') as f: + return '\n'.join(f.readlines()) + except IOError: + return None + +def get_syslog(): + try: + with open('/var/log/syslog') as f: + return '\n'.join(f.readlines()) + except IOError: + return None + +def get_process_list(): + plist = list() + first = True + for l in cli_read_lines('ps aux --sort -%mem')[1:]: + l = l.split() + plist.append(dict( + user = l[0], + pid = l[1], + perc_cpu = l[2], + perc_mem = l[3], + status_code = l[7], + started = l[8], + time = l[9], + command = ' '.join(l[10:]))) + return plist diff --git a/helpers/tor.py b/helpers/tor.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/helpers/tor.py diff --git a/helpers/util.py b/helpers/util.py new file mode 100644 index 0000000..46030bf --- /dev/null +++ b/helpers/util.py @@ -0,0 +1,24 @@ + +import os + +def cli_read(cmd): + p = os.popen(cmd) + return ''.join(p.readlines()) + +def cli_read_lines(cmd): + p = os.popen(cmd) + return p.readlines() + +def fs_read(path): + with open(path, 'r') as f: + return '\n'.join(f.readlines()).strip() + +def prefix_to_ipv4_mask(prefixlen): + assert(prefixlen >= 0) + assert(prefixlen <= 32) + mask = (0xFFFFFFFF & (0xFFFFFFFF << (32 - prefixlen))) + a = (0xFF000000 & mask) >> 24 + b = (0x00FF0000 & mask) >> 16 + c = (0x0000FF00 & mask) >> 8 + d = (0x000000FF & mask) + return '%d.%d.%d.%d' % (a, b, c, d) diff --git a/helpers/wireless.py b/helpers/wireless.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/helpers/wireless.py |