def get_ip_snmp_info(ip_list_status, snmp_oid_list, community): # 获取存活ip的snmp详细信息 ip_snmp = [] for ip in ip_list_status: print("\n正在获取{}的snmp信息...:\n{:=<126}".format(ip, "")) ip_snmp_dic = {} e = 1 try: # 初步测试该ip的snmp是否可用,若可用生成ip列表(zip用) ip_list = [ ip for i in range(int(snmpwalk(ip, "ifNumber", community)[0][1])) ] except: print("{}snmp不可用!".format(ip)) continue for snmp_oid in snmp_oid_list: # 循环读取oid信息 try: ip_snmp_dic[snmp_oid] = [ i[1] for i in snmpwalk(ip, snmp_oid, community) ] except: print("{}snmp信息获取不完整!".format(ip)) e = 0 break # print(ip_snmp_dic[snmp_oid]) if e: ip_snmp += list(zip(ip_list, *ip_snmp_dic.values())) # print(ip_snmp) return (ip_snmp)
def get_oid_mac(ip, oid, community): """ :param ip: :param oid: :param community: :return: 形式如下:'.1.3.6.1.2.1.4.22.1.2.4097.10.254.1.1' '00:1c:54:42:40:db' """ return snmpwalk(ipaddress=ip, oid=oid, community=community)
def test_snmpwalk_return_structure(): """ The snmpwalk function should give us a list of tuples, one for each OID walked. Each touple should contain the OID walked and the value of that OID on the server. """ result = snmpwalk(community='public', ipaddress=SNMP_SRV_ADDR, oid='IF-MIB::ifTable', port=SNMP_SRV_PORT) assert isinstance(result, list) assert isinstance(result[0], tuple) assert isinstance(result[0][0], str) and isinstance(result[0][1], str) assert result[0][0] == '.1.3.6.1.2.1.2.2.1.1.1' assert result[0][1] == '1'
def test_snmpwalk_return_contains_multiline_output(): """ there is an unfortunate bug / oversight in the net-snmp commands where newline characters within SNMP variables returned from a server are not escaped before printing. If you do an snmpget for 3 oids you'll get 3 lines of output printed :) But if one of those 3 variables contains, say, 2 new-line chars in it, you'll get 5 lines of output :( our snmpwalk function should detect whether a given line of output is an oid-value pair or a continuation of the value from the last pair and act accordingly """ result = snmpwalk(community='multiline-test', ipaddress=SNMP_SRV_ADDR, oid='SNMPv2-MIB::system', port=SNMP_SRV_PORT) assert isinstance(result[0], tuple) assert '\n' in result[0][1]
def post(self, request): jsonData = {} status = False try: sysUpTime = snmpwalk(ipaddress=HOST, community=COMMUNITY, port=PORT, oid=settings.SYS_UP_TIME) ssCpuUser = snmpwalk(ipaddress=HOST, community=COMMUNITY, port=PORT, oid=settings.SS_CPU_USER) ssCpuSystem = snmpwalk(ipaddress=HOST, community=COMMUNITY, port=PORT, oid=settings.SS_CPU_SYSTEM) ssCputIdle = snmpwalk(ipaddress=HOST, community=COMMUNITY, port=PORT, oid=settings.SS_CPU_IDLE) memTotalReal = snmpwalk(ipaddress=HOST, community=COMMUNITY, port=PORT, oid=settings.MEM_TOTAL_REAL) memAvailReal = snmpwalk(ipaddress=HOST, community=COMMUNITY, port=PORT, oid=settings.MEM_AVAIL_REAL) currentTime = strftime('%H:%M:%S', localtime(time())) jsonData['currentTime'] = currentTime jsonData['sysUpTime'] = sysUpTime[0][1] jsonData['ssCpuUser'] = int(ssCpuUser[0][1]) jsonData['ssCpuSystem'] = int(ssCpuSystem[0][1]) jsonData['ssCpuIdle'] = int(ssCputIdle[0][1]) jsonData['memTotalReal'] = round( int(re.findall('^[1-9][0-9]+', memTotalReal[0][1])[0]) / 1024) jsonData['memAvailReal'] = round( int(re.findall('^[1-9][0-9]+', memAvailReal[0][1])[0]) / 1024) jsonData['memTotalFree'] = jsonData['memTotalReal'] - jsonData[ 'memAvailReal'] status = True except Exception as error: pass jsonData['status'] = status return JsonResponse(jsonData)
sql = "select ip,ifindex from view_dev_info where mon " # print(sql) cur.execute(sql) ip_if_info = cur.fetchall() cur.close() conn.close() if_running_list = [] for i in ip_if_info: if_running_info = i e = 1 for ii in snmp_oid_dic["running"]: try: if_running_info += (snmpwalk(i[0], ".".join([ii, i[1]]), "xxww")[0][1], ) except: print("{}snmp信息获取不完整!{}".format( i, time.strftime("%Y-%m-%d %H:%M", time.localtime()))) e = 0 break if e: if_running_list.append(if_running_info) print("Values:{}".format(if_running_list)) field_name_list = ["ip", "ifIndex"] + snmp_oid_dic["running"] wr_db(conn_db_info, "dev_run", field_name_list, if_running_list) if mday == time.localtime().tm_mday: time.sleep(300) else:
def get_vendor(device_ip): result = snmpwalk(community='blankkey', ipaddress=device_ip, oid='iso.3.6.1.2.1.1.1.0') return result[0][1]