def main(): logging.basicConfig(level=logging.CRITICAL) argsDic = parseArgs(sys.argv[1:]) if '-i' in argsDic and argsDic['-i']: iface = argsDic['-i'] else: iface = utils.getDefInterface() fs = fileStorage(iface) loadingRes = fs.load() if (iface != None): netInfo = utils.getInterfaceConnectionInfo(iface) toArping = netInfo['inet'] + '/' + str(netInfo['nm']) print('Pinging....') addresses = arping.arping(toArping) _match = _different = _missed = _added = _ignored = 0 if loadingRes: for mac, ip in addresses: checkRes = fs.check(mac, ip) if checkRes == 'DIFFERENT': handleDifferent(fs, mac, ip) _different += 1 if checkRes == 'MISSED': added = handleMissed(fs, mac, ip) _missed += 1 if added: _added += 1 else: _ignored += 1 if checkRes == 'MATCH': _match += 1 else: print('first time') for mac, ip in addresses: fs.add(mac, ip) print('{} machines detected.'.format(len(addresses))) if loadingRes: print('\t{} Matched.'.format(_match)) print('\t{} Missed ({} added, {} ignored).'.format( _missed, _added, _ignored)) print('\t{} Different IP.'.format(_different)) fs.dump() else: logging.error('No up interfase')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--interface', '-i', default= '', help = 'You can select which interfaces directly.', required = False) parser.add_argument('--mask', '-m', default = '24', help = '(1-32)You can set mask range directly with this parameter.', required = False) parser.add_argument('--log', '-l', help = 'You can see log file.', action = 'store_true', required = False) args = parser.parse_args() ''' Check your LAN for unknown devices.''' if args.log: f = open("macList.txt", "a+") output = f.read() f.close() userhome = os.path.expanduser('~') username = os.path.split(userhome)[-1] os.popen('chown {0} macList.txt'.format(username)) os.popen('chgrp {0} macList.txt'.format(username)) print("------------------------------------------------------------") print(" MAC IP LOG DATE") print("------------------------------------------------------------") print(output) else: interfaces = gi.getInterfaces() if args.interface in interfaces: while (int(args.mask) >= 33 or int(args.mask) <= 0): args.mask = raw_input('Please enter valid mask(1-32): ') else: print("--- Your available interfaces ---") for i in range(len(interfaces)): print("----- " + list(interfaces.keys())[i] + ": " + list(interfaces.values())[i]) while (args.interface not in interfaces): args.interface = raw_input("Please enter valid interface name: ") while (int(args.mask) >= 33 or int(args.mask) <= 0): args.mask = raw_input('Please enter valid mask(1-32): ') onlineMacs = arping.arping(interfaces[args.interface]+'/'+args.mask, args.interface) checkList.check(onlineMacs)
def check_devices(): allowed_devices = {} options = config.options("allowed_devices") for option in options: allowed_devices[option] = config.get("allowed_devices", option) users = {} options = config.options("watch_users") for option in options: users[allowed_devices[config.get("watch_users", option)]] = option allDevices = [] device = {} conn = connect(db) c = conn.cursor() c.execute('CREATE TABLE IF NOT EXISTS not_allowed (mac TEXT NOT NULL, advised INTEGER NOT NULL, number INTEGER NOT NULL)') c.execute('CREATE TABLE IF NOT EXISTS users (user TEXT NOT NULL, present INTEGER NOT NULL)') if not(users=={}): for user in users.values(): t = (user,) c.execute('SELECT * FROM users WHERE user=?', t) if c.fetchone() == None: c.execute("INSERT INTO users VALUES (?,0)", t) conn.commit() conn.close() try: # Get devices connected to net allDevices = arping(config.get("defaults","NET"),10,1) # Open db connection conn = connect(db) c = conn.cursor() # Check if any watched user is connected if not(users=={}): for user_mac in users.keys(): found = 0 for device in allDevices: if device["MAC"] == user_mac: found = 1 break t = (users[user_mac],) if found: c.execute('SELECT present FROM users WHERE user=?', t) if not(c.fetchone()[0]): logger.info(lang.get("check_devices","msg_arrivehouse") % users[user_mac]) c.execute("UPDATE users SET present=1 WHERE user=?", t) conn.commit() notif(lang.get("check_devices","msg_arrivehouse") % users[user_mac]) else: c.execute('SELECT present FROM users WHERE user=?', t) if c.fetchone()[0]: logger.info(lang.get("check_devices","msg_lefthouse") % users[user_mac]) c.execute("UPDATE users SET present=0 WHERE user=?", t) conn.commit() notif(lang.get("check_devices","msg_lefthouse") % users[user_mac]) # Check if unallowed device is connected if not(allowed_devices=={}): for device in allDevices: if not(device.get('MAC') in allowed_devices.values()) and (device.get('MAC') != '<incomplete>'): t = (device.get('MAC'),) c.execute('SELECT * FROM not_allowed WHERE mac=?', t) dbdevice = c.fetchone() if dbdevice == None: logger.info(lang.get("check_devices","msg_notallowed") % (device.get('IP'), device.get('MAC'))) c.execute("INSERT INTO not_allowed VALUES (?,0,1)", t) conn.commit() notif(lang.get("check_devices","msg_notallowed") % (device.get('IP'), device.get('MAC'))) conn.close() except arpingerror as msg: logger.error("Error msg: %s"%str(msg))
from config_parser import config_parser from arping import arping, device from dispatcher import dispatcher config_file = "pipaalarm.ini" config = config_parser(config_file) arping = arping(ip_range = config.getScanRange()) arping.monitored_devices |= ( set([device("", d[1]["mac"]) for d in config.getClients()]) ) dispatcher = dispatcher(config, arping.warnings)
def check_devices(): allowed_devices = {} options = config.options("allowed_devices") for option in options: allowed_devices[option] = config.get("allowed_devices", option) users = {} options = config.options("watch_users") for option in options: users[allowed_devices[config.get("watch_users", option)]] = option allDevices = [] device = {} conn = connect(db) c = conn.cursor() c.execute( 'CREATE TABLE IF NOT EXISTS not_allowed (mac TEXT NOT NULL, advised INTEGER NOT NULL, number INTEGER NOT NULL)' ) c.execute( 'CREATE TABLE IF NOT EXISTS users (user TEXT NOT NULL, present INTEGER NOT NULL)' ) if not (users == {}): for user in users.values(): t = (user, ) c.execute('SELECT * FROM users WHERE user=?', t) if c.fetchone() == None: c.execute("INSERT INTO users VALUES (?,0)", t) conn.commit() conn.close() try: # Get devices connected to net allDevices = arping(config.get("defaults", "NET"), 10, 1) # Open db connection conn = connect(db) c = conn.cursor() # Check if any watched user is connected if not (users == {}): for user_mac in users.keys(): found = 0 for device in allDevices: if device["MAC"] == user_mac: found = 1 break t = (users[user_mac], ) if found: c.execute('SELECT present FROM users WHERE user=?', t) if not (c.fetchone()[0]): logger.info( lang.get("check_devices", "msg_arrivehouse") % users[user_mac]) c.execute("UPDATE users SET present=1 WHERE user=?", t) conn.commit() notif( lang.get("check_devices", "msg_arrivehouse") % users[user_mac]) else: c.execute('SELECT present FROM users WHERE user=?', t) if c.fetchone()[0]: logger.info( lang.get("check_devices", "msg_lefthouse") % users[user_mac]) c.execute("UPDATE users SET present=0 WHERE user=?", t) conn.commit() notif( lang.get("check_devices", "msg_lefthouse") % users[user_mac]) # Check if unallowed device is connected if not (allowed_devices == {}): for device in allDevices: if not (device.get('MAC') in allowed_devices.values()) and ( device.get('MAC') != '<incomplete>'): t = (device.get('MAC'), ) c.execute('SELECT * FROM not_allowed WHERE mac=?', t) dbdevice = c.fetchone() if dbdevice == None: logger.info( lang.get("check_devices", "msg_notallowed") % (device.get('IP'), device.get('MAC'))) c.execute("INSERT INTO not_allowed VALUES (?,0,1)", t) conn.commit() notif( lang.get("check_devices", "msg_notallowed") % (device.get('IP'), device.get('MAC'))) conn.close() except arpingerror as msg: logger.error("Error msg: %s" % str(msg))