Add interfaces listing module

This commit is contained in:
Oleksii Shevchuk 2017-03-16 22:00:05 +02:00
parent d3c8df573b
commit decb45f52b
2 changed files with 86 additions and 0 deletions

72
pupy/modules/ip.py Normal file
View File

@ -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)

View File

@ -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()