Solution to garbled characters in data parsing when Python3 uses easysnmp to read the device

When using easysnmpthe read device, sometimes garbled characters appear in data parsing. For example, if you want to obtain VRIDthe number of the device, you will encounter the following garbled characters:

# !/usr/bin/env python3
# -*- coding: UTF-8 -*-

from easysnmp import snmp_walk


def get_vrid():
    vrid_data = snmp_walk(
        oids='1.3.6.1.2.1.68.1.3.1.2',
        hostname='you ip address',
        community='you device community',
        version=2
    )
    for vrid in vrid_data:
        print(f'vrid: {vrid} \t value:{vrid.value}')

insert image description here
The solution is also very simple, just snmpadd use_sprint_value=Truethe parameter when setting .

	# !/usr/bin/env python3
	# -*- coding: UTF-8 -*-
	
	from easysnmp import snmp_walk
	
	
def get_vrid():
    vrid_data = snmp_walk(
        oids='1.3.6.1.2.1.68.1.3.1.2',
        hostname='you ip address',
        community='you device community',
        version=2,
        use_sprint_value=True # 设置 use_sprint_value 为 True
    )
    for vrid in vrid_data:
        print(f'vrid: {vrid} \t value:{vrid.value}')

insert image description here
However, it should be noted that if use_sprint_valueis set to True, snmpsome data types may be returned in a non-standard format, and the data may not be sprint_valueformatted by the function library getand getnextthe return value of the method.

Guess you like

Origin blog.csdn.net/yilovexing/article/details/126061119