diff options
author | Felix Richter <Felix.Richter@syntax-fehler.de> | 2011-01-30 05:46:39 +0100 |
---|---|---|
committer | Felix Richter <Felix.Richter@syntax-fehler.de> | 2011-01-30 05:46:39 +0100 |
commit | 5fb4705362ab694eb84b21f1389c6ca6bc5e4d8a (patch) | |
tree | 2ff9165df317ab5c4af2639c87c828d4a90d7da9 | |
parent | 43f2eb6e5492c0fd0797785b3d3b258e2a7a2169 (diff) |
updated to something actually working
-rw-r--r-- | src/arping.py | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | src/main.py | 51 | ||||
-rwxr-xr-x | src/snmp_users.py | 15 |
3 files changed, 60 insertions, 8 deletions
diff --git a/src/arping.py b/src/arping.py index 1846f474..11fb81a9 100644 --- a/src/arping.py +++ b/src/arping.py @@ -9,7 +9,7 @@ try: from scapy.all import * #might throws "no such module" def arpingy(iprange="10.42.1.0/24",iface='eth0'): - log.debug("pinging"+ str(iprange)) + log.debug("pinging "+ str(iprange)) """Arping function takes IP Address or Network, returns nested mac/ip list""" try: conf.verb=0 diff --git a/src/main.py b/src/main.py index 0e1d5dde..2d9bb206 100644..100755 --- a/src/main.py +++ b/src/main.py @@ -1 +1,50 @@ -#!/usr/bin/python2 +#!/usr/bin/env python2 + +import logging +logging.basicConfig(level=logging.INFO) +log = logging.getLogger('snmp_exchange') + +import pika +import json,argparse,hashlib,sys,time +from snmp_users import snmp_users + +SNMP_EXCHANGE='snmp_src' +PROTO_VERSION='1' + +parser = argparse.ArgumentParser(description='Generates a list of mac-addresses currently in the network via snmp ') +parser.add_argument('--host',default='141.31.8.11', help='AMQP host ip address') +parser.add_argument('--port',type=int,default=5672, help='AMQP host port') +parser.add_argument('-u','--username',default='guest', help='AMQP username') +parser.add_argument('-p','--password',default='guest', help='AMQP password') +parser.add_argument('-r','--repeat',type=int,default=20, help='SNMP_Polling Delay') +parser.add_argument('--unique-key',action='store_true', help='Unique Key') +args = parser.parse_args() +if args.unique_key: + print hashlib.sha1(PROTO_VERSION+args.host+str(args.port)).hexdigest() + sys.exit(0) + +args = parser.parse_args() + +connection = pika.AsyncoreConnection(pika.ConnectionParameters( + credentials = pika.PlainCredentials(args.username,args.password), + host=args.host,port=args.port)) +channel = connection.channel() + +channel.exchange_declare(exchange=SNMP_EXCHANGE, + type='fanout') + +log.info('Starting up snmp_users') +s = snmp_users() +print ' Sending Messages in Intervals. To exit press CTRL+C' +try: + while True: + log.info("collecting data from network") + ret = s.collect() + data = { 'type' : 'snmp', 'subtype' : 0, 'data' : ret} + log.info("writing data to queue : %s" % data) + channel.basic_publish(exchange=SNMP_EXCHANGE, + routing_key='', + body=json.dumps(data)) + time.sleep(args.repeat) +except : + print "something happened :(" diff --git a/src/snmp_users.py b/src/snmp_users.py index 1d6cf9be..1f189d78 100755 --- a/src/snmp_users.py +++ b/src/snmp_users.py @@ -8,8 +8,8 @@ log = logging.getLogger('snmp_users') DEFAULT_CONFIG= { "snmp" : { - "server" : "10.42.0.1", - "community" : "shammunity", + "server" : "127.0.0.1", + "community" : "community", "tree" : "1.3.6.1.2.1.3.1.1.2" }, "arping" : { @@ -55,8 +55,8 @@ class snmp_users(Configurable): def update_results(self,new): """ Verifies ip and mac via ARP Scan in addition it adds the correct ip to the mac_list """ - macl = self.mac_list - + macl = self.mac_list = {} + for ip,mac in new: # fill the mac_list if not macl.get(mac,None): macl[mac] = [] @@ -68,6 +68,7 @@ class snmp_users(Configurable): """ verifies retrieved data where data is an array of arrays where [0] is the ip and [1] is the mac (space-delimited)""" arp_data = self.arping_parallel(snmp_data) + print arp_data self.update_results(arp_data) def arping_parallel(self,data): @@ -76,7 +77,7 @@ class snmp_users(Configurable): p = Pool(10) tmp = [ {'iprange':dat[0],'iface':conf['dev']} for dat in data] try: - return filter(lambda x:x , p.map(arping_helper, tmp)) + return filter(lambda x:x , p.map(arping_helper, tmp)) except Exception as e: log.warning("Something happened,falling back to original data: "+ str(e)) return data @@ -85,7 +86,8 @@ class snmp_users(Configurable): output = self.call_external() data = self.parse_output(output) macs = self.verify(data) - self.print_results(self.mac_list) + #self.print_results(self.mac_list) + return self.mac_list def print_results(self,macs): log.debug('printing results:') print '\n'.join([ mac + " => %s" % @@ -96,3 +98,4 @@ if __name__ == "__main__": logging.basicConfig(level=logging.INFO) a = snmp_users() a.collect() + a.print_results(a.mac_list) |