hostname R1.greenhousecreate two records for the forward zone and for the reverse zone:
domain service.prov.ru
interface FastEthernet0 / 1.24
description Line to Red House
ip address 10.24.1.1 255.255.255.254
Fa0 / 1.24.Line_to_Red_House.R1.greenhouse.service.prov.ru. IN A 10.24.1.1
')
1.1.24.10.in-addr.arpa. IN PTR Fa0 / 1.24.Line_to_Red_House.R1.greenhouse.service.prov.ru.
Fa0-1-24.Line-to-Red-House.R1.greenhouse.service.prov.ru. IN A 10.24.1.1With readability there are obvious problems. And if the interface name does not begin with a non-zero digit, and even with a colon? After bringing this to the standard, it becomes quite difficult to understand what and where in these numbers and letters. And I would like to see the traceroute output from the corner of my eye, immediately understand what, where and how, and not engage in the interpretation of output by divination. Therefore, bind was configured with an option that allows you to work with non-standard names. And the script, accordingly, makes the most readable names. Docking the DNS networkers and the global DNS is a bit of a separate conversation, but the easiest way to allocate a separate domain for network equipment, for example, network.megaproduct.org.
sudo apt-get install snmpIf you want the utilities to work with the named OID in the configuration file /etc/snmp/snmp.conf add the used mib or keyword ALL.
sudo apt-get install snmp-mibs-downloader
$ cat /etc/snmp/snmp.confCheck, snmp saw our MIB:
# As the snmp packages come without MIB files due to license reasons, loading
# of MIBs is disabled by default. If you added the MIBs you can reenable
# loading them by commenting out the following line.
mibs: SNMPv2-MIB: RFC1213-MIB: IF-MIB
#mibs: ALL
zw @ note: ~ $
host $ snmptranslate 1.3.6.1.2.1.31.1.1.1.18Make a request for a real box:
IF-MIB :: ifAlias
host $ snmptranslate -On IF-MIB :: ifAlias
.1.3.6.1.2.1.31.1.1.1.18
host $ snmpget -v 2c -c tilitili 10.239.192.2 1.3.6.1.2.1.31.1.1.1.18.1The last digit (1) that I added is ifindex. Ifndex is the interface number assigned by the snmp agent to the hardware operating system.
IF-MIB :: ifAlias.1 = STRING: Line_to_OUT
host $ snmpwalk -v 2c -c tilitili 10.239.192.2The snmpwalk output has something to read, it is very verbose, so it’s better to filter it.
RFC1213-MIB :: ifDescr.1 = STRING: "FastEthernet0 / 0"
RFC1213-MIB :: ifDescr.2 = STRING: "FastEthernet0 / 1"
RFC1213-MIB :: ifDescr.3 = STRING: "Null0"
RFC1213-MIB :: ifDescr.36 = STRING: "Loopback0"
RFC1213-MIB :: ipAdEntAddr.10.239.192.2 = IpAddress: 10.239.192.2
RFC1213-MIB :: ipAdEntAddr.10.239.192.90 = IpAddress: 10.239.192.90
RFC1213-MIB :: ipAdEntAddr.11.0.0.1 = IpAddress: 11.0.0.1
IF-MIB :: ifName.1 = STRING: Fa0 / 0
IF-MIB :: ifName.2 = STRING: Fa0 / 1
IF-MIB :: ifName.3 = STRING: Nu0
IF-MIB :: ifName.36 = STRING: Lo0
IF-MIB :: ifAlias.1 = STRING: Line_to_OUT
IF-MIB :: ifAlias.2 = STRING: Line_to_poligon
IF-MIB :: ifAlias.3 = STRING:
IF-MIB :: ifAlias.36 = STRING:
host $ snmpwalk -v 2c -c tilitili 10.239.192.2 | grep 10.239.192.2And from the output it is immediately clear that there is no OID, adding to which ifindex, we would get the interface ip addresses with this ifindex. Note:
RFC1213-MIB :: ipAdEntAddr.10.239.192.2 = IpAddress: 10.239.192.2
RFC1213-MIB :: ipAdEntIfIndex.10.239.192.2 = INTEGER: 36
RFC1213-MIB :: ipAdEntNetMask.10.239.192.2 = IpAddress: 255.255.255.255
RFC1213-MIB :: ipAdEntBcastAddr.10.239.192.2 = INTEGER: 1
RFC1213-MIB :: ipAdEntReasmMaxSize.10.239.192.2 = INTEGER: 18024
RFC1213-MIB :: ipRouteDest.10.239.192.2 = IpAddress: 10.239.192.2
RFC1213-MIB :: ipRouteIfIndex.10.239.192.2 = INTEGER: 36
and so on
host $ snmpgetnext -v 2c -c tilitili 10.239.192.2 1.3.6.1.2.1.4.20.1.1And so on. Please note that we do not know the address of the router at the first request, so the first call simply goes to the OID value 1.3.6.1.2.1.4.20.1.1
RFC1213-MIB :: ipAdEntAddr.10.239.192.2 = IpAddress: 10.239.192.2
host $ snmpgetnext -v 2c -c tilitili 10.239.192.2 1.3.6.1.2.1.4.20.1.1.10.239.192.2
RFC1213-MIB :: ipAdEntAddr.10.239.192.90 = IpAddress: 10.239.192.90
def snmp_getnextcmd(community, ip, port, OID): # type class 'generator' errorIndication, errorStatus, errorIndex, result[3] # next , next() return (nextCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((ip, port)), ContextData(), ObjectType(ObjectIdentity(OID)))) g = (snmp_getnextcmd(community_string, ip_address_host, port_snmp, OID_ipAdEntAddr)) print(g) errorIndication, errorStatus, errorIndex, varBinds = next(g) for name,val in varBinds: print(name.prettyPrint(),' ====== ',val.prettyPrint()) errorIndication, errorStatus, errorIndex, varBinds = next(g) for name,val in varBinds: print(name.prettyPrint(),' ====== ',val.prettyPrint()) errorIndication, errorStatus, errorIndex, varBinds = next(g) for name,val in varBinds: print(name.prettyPrint(),' ====== ',val.prettyPrint()) errorIndication, errorStatus, errorIndex, varBinds = next(g) for name,val in varBinds: print(name.prettyPrint(),' ====== ',val.prettyPrint()) errorIndication, errorStatus, errorIndex, varBinds = next(g) for name,val in varBinds: print(name.prettyPrint(),' ====== ',val.prettyPrint())
<generator object nextCmd at 0x7f960364f8e0>The variable g is our generator. What does python tell us right away when it says print (g).
SNMPv2-SMI :: mib-2.4.20.1.1.10.239.192.2 ====== 10.239.192.2
SNMPv2-SMI :: mib-2.4.20.1.1.10.239.192.90 ====== 10.239.192.90
SNMPv2-SMI :: mib-2.4.20.1.1.11.0.0.1 ====== 11.0.0.1
SNMPv2-SMI :: mib-2.4.20.1.2.10.239.192.2 ====== 36
SNMPv2-SMI :: mib-2.4.20.1.2.10.239.192.90 ====== 1
for name,val in varBinds: print(name.prettyPrint(),' ====== ',val.prettyPrint())
def snmp_getnextcmd_next(community, ip, port, OID, file): # class generator def snmp_getnext # OID - OID list_OID = [OID_ipAdEntAddr,OID_ipAdEntIfIndex,OID_ipAdEntNetMask], # '1.2.3.4' # , OID list_result = [] # list_result2 = [] # g = (snmp_getnextcmd(community, ip, port, OID[0])) # OID varBinds = 0 flag = True for oid in list_OID: if varBinds != 0: for name, val in varBinds: list_result2.append(list_result) list_result = [] list_result.append(val.prettyPrint()) i = 0 while i <= 0: # errorIndication, errorStatus, errorIndex, varBinds = next(g) if errors(errorIndication, errorStatus, errorIndex, ip_address_host, file): if str(varBinds).find(oid) != -1: i = 0 for name, val in varBinds: list_result.append(val.prettyPrint()) else: i = i + 1 flag = False else: file.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + 'Error snmp_getnextcmd_next ip = ' + ip + ' OID = '+ OID[0] + '\n') print('Error snmp_getnextcmd_next ', False) i = i + 1 flag = False list_result2.append(list_result) return list_result2
print (ipaddress.ip_address ('194.4.5.1'). reverse_pointer)My script names the reverse zone files based on the addresses it receives from the router, it creates a dictionary in which the key consists of the first two octets of the ip-address. Then from these keys file names are formed. To do this, you still need to parse the address by octets. I attach a script that, having received the list of addresses of routers, should produce the correct zone files. He does this for me.
1.5.4.194.in-addr.arpa
# , . # snmp sysname, , , # sysname , , ifindex, ( , ) # - traceback, testresult # desc name interface - # bind check-names master ignore # , . - named.soa. $INCLUDE #import section from pysnmp.hlapi import * from ipaddress import * from datetime import datetime import mysql.connector from mysql.connector import errorcode # mysql mysql.connector python3 # mysql, . - ip # var section #snmp community_string = 'community' # community string port_snmp = 161 OID_ipAdEntAddr = '1.3.6.1.2.1.4.20.1.1' # From SNMPv2-MIB ip OID_ifNumber = '1.3.6.1.2.1.2.1.0' # From RFC1213-MIB ifindex OID_sysName = '1.3.6.1.2.1.1.5.0' # From SNMPv2-MIB hostname/sysname OID_ipAdEntIfIndex = '1.3.6.1.2.1.4.20.1.2' # From SNMPv2-MIB ifindex interface OID_ipAdEntNetMask = '1.3.6.1.2.1.4.20.1.3' # From SNMPv2-MIB OID_ifAlias = '1.3.6.1.2.1.31.1.1.1.18' # Desc . OID ifindex OID_ifName = '1.3.6.1.2.1.31.1.1.1.1' # OID ifindex list_OID = [OID_ipAdEntAddr,OID_ipAdEntIfIndex,OID_ipAdEntNetMask] #log filename_log = 'zone_gen.log' # log_level = 'debug' # ('normal'), ('debug'), ('min') # bind zone files file_direct_zone = 'spd.esrr.rzd.hosts' # domain = 'domain.name.org' # domainname named_soa = '$INCLUDE /var/bind/named.soa' direct_zone =[] # reverse_zone = {} # name_server_record = 'dns.server.ru. IN A 10.111.33.21\n' ip_segment_ds = ['10.0.0.0/8'] # , #var section for mysql username_db = 'username' # mysql password_db = 'password' # mysql db = 'name_database' # query_from_db = 'select ip from devices where type = 2 or type = 3;' # sql ; host = '192.11.33.123' # # ip from file filename_of_ip = 'ip.txt' # Ip select_source_ip = 'file' # ('file') confstractor ('mysql') # function section def snmp_getnextcmd(community, ip, port, OID): # type class 'generator' errorIndication, errorStatus, errorIndex, result[3] # next , next() return (nextCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((ip, port)), ContextData(), ObjectType(ObjectIdentity(OID)))) def snmp_getcmd(community, ip, port, OID): # type class 'generator' errorIndication, errorStatus, errorIndex, result[3] - # get SNMP OID return (getCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((ip, port)), ContextData(), ObjectType(ObjectIdentity(OID)))) def snmp_get_next(community, ip, port, OID, file): # class generator def snmp_get # errors, class 'pysnmp.smi.rfc1902.ObjectType' OID # errorIndication, errorStatus, errorIndex, varBinds = next(snmp_getcmd(community, ip, port, OID)) # errors x = [] if errors(errorIndication, errorStatus, errorIndex, ip, file): for name, val in varBinds: return (val.prettyPrint(), True) else: file.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : Error snmp_get_next ip = ' + ip + ' OID = ' + OID + '\n') return ('Error', False) def errors(errorIndication, errorStatus, errorIndex, ip, file): # False () if errorIndication: print(errorIndication, 'ip address ', ip) file.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + str(errorIndication) + ' = ip address = ' + ip + '\n') return False elif errorStatus: print(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + '%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) file.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + '%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?' + '\n')) return False else: return True def snmp_getnextcmd_next(community, ip, port, OID, file): # class generator def snmp_getnext # OID - OID list_OID = [OID_ipAdEntAddr,OID_ipAdEntIfIndex,OID_ipAdEntNetMask], # '1.2.3.4' # , OID list_result = [] # list_result2 = [] # g = (snmp_getnextcmd(community, ip, port, OID[0])) # OID varBinds = 0 flag = True for oid in list_OID: if varBinds != 0: for name, val in varBinds: list_result2.append(list_result) list_result = [] list_result.append(val.prettyPrint()) i = 0 while i <= 0: # errorIndication, errorStatus, errorIndex, varBinds = next(g) if errors(errorIndication, errorStatus, errorIndex, ip_address_host, file): if str(varBinds).find(oid) != -1: i = 0 for name, val in varBinds: list_result.append(val.prettyPrint()) else: i = i + 1 flag = False else: file.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + 'Error snmp_getnextcmd_next ip = ' + ip + ' OID = '+ OID[0] + '\n') print('Error snmp_getnextcmd_next ', False) i = i + 1 flag = False list_result2.append(list_result) return list_result2 def check_ip(ip): # ip try: ip_address(ip) except ValueError: return False else: return True def check_ip2(ip): # ip try: ip_interface(ip) except ValueError: return False else: return True def get_from_mysql(host, user, password, db_name, query, fd): flag = True try: cnx = mysql.connector.connect(user=user, password = password, host = host ,database = db_name) if cnx.is_connected(): print('Connected to MySQL database') fd.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + 'Connected to MySQL database '+ host + '\n') cursor1 = cnx.cursor() cursor1.execute(query) query_result = [] i=0 list = [] for var in cursor1: query_result.append(str(var)[1:-2]) for var in query_result: # , ip if var.isdigit(): if check_ip(int(var)): list.append(str(ip_address(int(var)))) # query_result.append(str(ip_address(int(str(var)[1:-2])))) except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Something is wrong with your user name or password") fd.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + "Something is wrong with your user name or password" + '\n') flag = False elif err.errno == errorcode.ER_BAD_DB_ERROR: print("Database does not exist") fd.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + "Database does not exist" + '\n') flag = False else: print(err) fd(err) flag = False cursor1.close() cnx.close() return (list, flag) def get_from_file(file, filelog): # ip . - fd = open(file,'r') list_ip = [] for line in fd: line=line.rstrip('\n') if check_ip(line): list_ip.append(line) else: filed.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ': Error ip ' + line) fd.close() return list_ip # code section # filed = open(filename_log,'w') # filed.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + '\n') # if select_source_ip == 'file': ip_from_confstractor = get_from_file(filename_of_ip, filed) else: if select_source_ip == 'mysql': ip_from_confstractor, flag_mysql = get_from_mysql(host , username_db, password_db, db, query_from_db, filed) if log_level == 'debug': filed.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + 'ip address from ' + select_source_ip + '\n') for var in ip_from_confstractor: filed.write(var + '\n') # ifindex #number_ifindex = (snmp_get_next(community_string, ip_address_host, port_snmp, OID_ifNumber)) for ip_address_host in ip_from_confstractor: # sysname hostname+domainname, sysname, flag_snmp_get = (snmp_get_next(community_string, ip_address_host, port_snmp, OID_sysName, filed)) if flag_snmp_get: # , snmp if sysname == 'No Such Object currently exists at this OID' : # community . , traceback. , community, hostname, print('ERROR community', sysname , ' ' , ip_address_host) filed.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + 'ERROR community sysname = ' + sysname + ' ip = ' + ip_address_host + '\n') else: if log_level == 'debug': filed.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + ' sysname ' + sysname + ' type ' + str(type(sysname)) + ' len ' + str(len(sysname)) + ' ip ' + ip_address_host + '\n') if len(sysname) < 3: sysname = 'None_sysname' if log_level == 'debug' or log_level == 'normal': filed.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + 'Error sysname 3 = ' + sysname + ' ip = ' + ip_address_host + '\n') if sysname.find(domain) == -1: # - hostname , Huawei Catos sysname = sysname + '.' + domain # print("check domain ", sysname, " ", ip_address_host , "\n") if log_level == 'debug' or log_level == 'normal': filed.write("check domain : " + sysname + " " + ip_address_host + " " + "\n") else: # pass # , ifindex, # testresult[0] = # testresult[1] = ifindex # testresult[2] = testresult = snmp_getnextcmd_next(community_string, ip_address_host, port_snmp, list_OID, filed) i = 0 for var in testresult[0]: # ip_segment_ds flag = 'False' for net in ip_segment_ds: if (IPv4Address(var) in IPv4Network(net)): flag = 'True' if flag == 'True': pass else: testresult[0].pop(i) testresult[1].pop(i) i = i +1 # ifindex, desc , , # testresult[3] = desc desc = [] i = 0 for var in testresult[1]: x, flag_snmp_get = snmp_get_next(community_string, ip_address_host, port_snmp, OID_ifAlias+'.'+var, filed) if flag_snmp_get: x = x.replace('"', '').replace(' ', '_').replace('(', '').replace(')', '').replace( ',', '.').replace(']','').replace('[','').replace("'","").replace('-_','').replace('_-','') desc.append(x) i = i + 1 else: i = i +1 testresult[1].pop(i) # desc ifindex, , ifindex # . . testresult[0].pop(i) # , testresult.append(desc) # # ifindex, desc = [] for var in testresult[1]: x, flag_snmp_get = snmp_get_next(community_string, ip_address_host, port_snmp, OID_ifName + '.' + var, filed) if flag_snmp_get: x = x.replace('Loopback', 'Lo').replace('loopback0', 'Lo0').replace(' ', '_') desc.append(x) i = i + 1 else: i = i + 1 testresult[1].pop(i) # ifindex, , ifindex # . testresult[0].pop(i) # , tracebrake testresult[3].pop(i) # desc , tracebrake testresult.append(desc) # #testresult[4] i = 0 for var in testresult[3]: if var == 'No_Such_Object_currently_exists_at_this_OID': # APC Zyxel, OID desc testresult[3][i] = '' testresult[4][i] = 'Lo0' i = i + 1 if log_level == 'debug': for var in testresult: filed.write(str(var) + '\n') # i=0 for var in testresult[0]: rev = var.split('.') revs = rev[:] revs.reverse() revsstr='.'.join(revs) # if (rev[0]+'.'+rev[1]) in reverse_zone: if testresult[4][i] == 'Lo0': reverse_zone[rev[0]+'.'+rev[1]].append(revsstr + '.in-addr.arpa. IN PTR ' + sysname + '.') else: reverse_zone[rev[0] + '.' + rev[1]].append(revsstr + '.in-addr.arpa. IN PTR ' + testresult[4][i] + '.' + testresult[3][i] + '.' + sysname + '.') else: reverse_zone[rev[0]+'.'+rev[1]] = [] if testresult[4][i] == 'Lo0': reverse_zone[rev[0] + '.' + rev[1]].append(revsstr + '.in-addr.arpa. IN PTR ' + sysname + '.') else: reverse_zone[rev[0] + '.' + rev[1]].append( revsstr + '.in-addr.arpa. IN PTR ' + testresult[4][i] + '.' + testresult[3][ i] + '.' + sysname + '.') # if testresult[4][i] == 'Lo0': direct_zone.append(sysname + '.' + ' IN A ' + var) elif testresult[3][i] == '': direct_zone.append(testresult[4][i]+'.' + sysname + '.' +' IN A ' + var) else: direct_zone.append(testresult[4][i] + '.' + testresult[3][i] + '.' + sysname + '.' + ' IN A ' + var) i=i+1 else: print('Error ip ', ip_address_host) filed.write(datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + ' : ' + 'Error snmp, ip ' + ip_address_host + ' ' + str(flag_snmp_get) + '\n') # f=open(file_direct_zone, 'w') # f.write(named_soa + '\n'+'\n') f.write(name_server_record) for i in direct_zone: i = i.replace('..','.') f.write(i + '\n') f.close() file_list_reverse = list(reverse_zone.keys()) print(file_list_reverse) if log_level == 'debug': for i in file_list_reverse: filed.write(i + '\n') for i in list(reverse_zone.keys()): # f=open(i+'.rev','w') f.write(named_soa + '\n'+'\n') for y in reverse_zone[i]: y = y.replace('..', '.') f.write(y + '\n') f.close() # filed.write('\n' + datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") + '\n') # filed.close()
Source: https://habr.com/ru/post/340050/
All Articles