Last active
August 7, 2017 20:14
-
-
Save lean0708/31889f30b661041758e241b3975d35ab to your computer and use it in GitHub Desktop.
Script para correr el puresnmp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from puresnmp import get, exc | |
from companies import get_companies # companies.py es un archivo de paleo-assets que se encuentra aca http://mercurial.invgate.com/Assets/Assets-Main/files/9fa39faf5653fc5457741f2cb05461dd6bf7816f/NetworkDiscovery/python/companies.py | |
import logging | |
OIDS_SCAN = { | |
"macs_mib": "1.3.6.1.2.1.2.2.1.6.2", # pOid_ifPhysAddress" | |
"iface_descr": "1.3.6.1.2.1.2.2.1.2", | |
"iface_type": "1.3.6.1.2.1.2.2.1.3", | |
"iface_name": "1.3.6.1.2.1.31.1.1.1.1", | |
"arp_mib": "1.3.6.1.2.1.4.22", | |
"printers_description_oid": "1.3.6.1.2.1.25.3.2.1.3.1", # pOid_hrDeviceDescr | |
"description_oid": "1.3.6.1.2.1.1.1.0", # pOid_sysDescr | |
"name_oid": "1.3.6.1.2.1.1.5.0", | |
"object_id_oid": "1.3.6.1.2.1.1.2.0", # pOid_sysObjectID | |
"mac_to_port": "1.3.6.1.2.1.17.4.3.1", | |
"printer_consumables": "1.3.6.1.2.1.43.12.1.1.4.1.1", | |
} | |
logging.basicConfig(filename='snmp_debug.log', level=logging.DEBUG) | |
LOG = logging.getLogger("netdisc") | |
def main(argv): | |
ip = argv[0] | |
community = argv[1] | |
for attr, oid in OIDS_SCAN.items(): | |
output = "" | |
try: | |
output = _scan_oid(ip=ip, community=community, oid=oid) | |
if attr == "macs_mib": | |
output = ":".join(hex(c).lstrip("0x").zfill(2) for c in output) | |
if attr == "object_id_oid": | |
output = get_companies(output) | |
except exc.EmptyMessage as e: | |
log(ip, attr, oid, "Empty Message: ", e) | |
except exc.NoSuchOID as e: | |
log(ip, attr, oid, "No such OID, ", e) | |
except exc.TooManyVarbinds as e: | |
log(ip, attr, oid, "TooManyVarbinds, {}", e) | |
except exc.Timeout: | |
log(ip, attr, oid, "Timed out conection") | |
except exc.SnmpError as e: | |
log(ip, attr, oid, "Snmp Error: {}", e) | |
except ConnectionResetError: # maybe there is a computer | |
log(ip, attr, oid, "Connection was forcibly closed by the remote host") | |
except Exception as e: | |
log(ip, attr, oid, "Error: {}", e) | |
print("{}: {}".format(attr, output)) | |
def _scan_oid(ip, community, oid): | |
return get(ip=ip, community=community, oid=oid) | |
def _scan_mac(ip, community, oid): | |
mac_output = get(ip=ip, community=community, oid=oid) | |
return ":".join(hex(c).lstrip("0x").zfill(2) for c in mac_output) | |
def log(ip, attr, oid, error_type, msg): | |
LOG.error("IP: {}, ATTR: {}, OID: {}, MSG: {}, {}".format(ip, attr, oid, error_type, msg)) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment