From decb45f52b1baee506928658f7b12a8fe9fd9cbc Mon Sep 17 00:00:00 2001 From: Oleksii Shevchuk Date: Thu, 16 Mar 2017 22:00:05 +0200 Subject: [PATCH] Add interfaces listing module --- pupy/modules/ip.py | 72 +++++++++++++++++++++++++++++++++++++ pupy/packages/all/pupyps.py | 14 ++++++++ 2 files changed, 86 insertions(+) create mode 100644 pupy/modules/ip.py diff --git a/pupy/modules/ip.py b/pupy/modules/ip.py new file mode 100644 index 00000000..72f87eb7 --- /dev/null +++ b/pupy/modules/ip.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +from pupylib.PupyModule import * +from pupylib.PupyCmd import PupyCmd +from pupylib.utils.rpyc_utils import obtain +from pupylib.utils.term import colorize +from datetime import datetime, timedelta + +import logging +import socket + +__class_name__="IPModule" + +@config(cat="admin") +class IPModule(PupyModule): + """ list interfaces """ + + dependencies = [ 'pupyps' ] + is_module=False + + def init_argparse(self): + self.arg_parser = PupyArgumentParser(prog="ip", description=self.__doc__) + self.arg_parser.add_argument('iface', nargs='*', help='show only these interfaces') + + def run(self, args): + try: + data = obtain(self.client.conn.modules.pupyps.interfaces()) + + addrlen = max([len(x) for x in data['addrs']])+1 + + families = { + v:k[3:] for k,v in socket.__dict__.iteritems() if k.startswith('AF_') + } + + families.update({-1: 'LINK'}) + + familylen = max([len(v)-3 for v in families.itervalues()])+1 + + for addr, addresses in data['addrs'].iteritems(): + if args.iface and not addr in args.iface: + continue + + color = "" + if addr in data['stats'] and not data['stats'][addr].get('isup'): + color = 'darkgrey' + elif not any([ x.get('family') == socket.AF_INET for x in addresses ]): + color = 'grey' + + self.stdout.write(colorize(addr.ljust(addrlen), color or 'cyan')) + first = True + + for address in addresses: + if first: + first = False + else: + self.stdout.write(' '*addrlen) + + self.stdout.write(colorize(families[ + address.get('family') + ].ljust(familylen), color)) + + self.stdout.write( + colorize(address.get('address', '').split('%')[0], color or 'yellow') + ) + if address.get('netmask'): + self.stdout.write(colorize('/'+address.get('netmask'), color)) + + if address.get('broadcast'): + self.stdout.write(colorize(' brd '+address.get('broadcast'), color)) + self.stdout.write('\n') + + except Exception, e: + logging.exception(e) diff --git a/pupy/packages/all/pupyps.py b/pupy/packages/all/pupyps.py index 4a225327..581ae5cc 100644 --- a/pupy/packages/all/pupyps.py +++ b/pupy/packages/all/pupyps.py @@ -119,5 +119,19 @@ def connections(): return connections +def interfaces(): + return { + 'addrs': { + x:[ + { k:v for k,v in z.__dict__.iteritems() } for z in y + ] for x,y in psutil.net_if_addrs().iteritems() + }, + 'stats': { + x:{ + k:v for k,v in y.__dict__.iteritems() + } for x,y in psutil.net_if_stats().iteritems() + } + } + if __name__ == '__main__': print users()