脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python封装SNMP调用接口的示例代码

Python封装SNMP调用接口的示例代码

2022-07-24 12:23lyshark Python

PySNMP 是一个纯粹用Python实现的SNMP,用PySNMP的最抽象的API为One-line Applications。本文为大家分享了Python封装SNMP调用接口的示例代码,需要的可以参考一下

PySNMP 是一个纯粹用Python实现的SNMP,用PySNMP的最抽象的API为One-line Applications,其中有两类API:同步的和非同步的,都在模块pysnmp.entity.rfc3413.oneliner.cmdgen 中实现,如下是Get方式与Walk方式的基本实现.

首先需要在系统中安装SNMP客户端,对于Linux平台来说只需要执行如下配置过程即可.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# yum install -y net-snmp
[root@localhost ~]# cat /etc/snmp/snmpd.conf |grep -vE "^#|^$"
com2sec notConfigUser  default       public
 
group   notConfigGroup v1           notConfigUser
group   notConfigGroup v2c           notConfigUser
 
view    systemview    included   .1
view    systemview    included   .1
 
access  notConfigGroup  ""  any  noauth  exact  systemview none none
 
[root@localhost ~]# systemctl restart snmpd
[root@localhost ~]# systemctl enable snmpd

如果是Windows系统则需要在客户机服务列表,开启SNMP支持,并设置好一个团体名称,如下图。

Python封装SNMP调用接口的示例代码

当我们配置好客户端后,服务端就客户获取数据了,我们以一个OID序号为例,我们查询特定序号对应的名称,然后将其记录下来,例如下面这样。

C:\Users\admin> snmpwalk -v 2c -c public 192.168.1.101 .1.3.6.1.2.1.25.2.2
HOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 2096632 KBytes

首先我们不使用PySNMP模块直接开线程调用看看,该代码如下所示.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os,re,time
 
# 通过SNMP收集主机CPU利用率: 通过SNMP协议,收集目标主机的CPU利用率(百分比),并返回JSON字符串.
def Get_CPU_Info(addr):
    try:
        Head = ["HostName","CoreLoad","CpuUser","CpuSystem","CpuIdle"]
        CPU = []
        ret = os.popen("snmpwalk -v 2c -c nmap " + addr + " .1.3.6.1.2.1.1.5")
        CPU.append(ret.read().split(":")[3].strip())
        ret = os.popen("snmpwalk -v 2c -c nmap " + addr + " .1.3.6.1.2.1.25.3.3.1.2")
        CPU.append(ret.read().split(":")[3].strip())
 
        for i in [9,10,11]:
            ret = os.popen("snmpwalk -v 2c -c nmap " + addr + " 1.3.6.1.4.1.2021.11.{}.0".format(i))
            ret = ret.read()
            Info = ret.split(":")[3].strip()
            CPU.append(Info)
        return dict(zip(Head,CPU))
    except Exception:
        return 0
 
# 通过SNMP获取系统CPU负载信息: 分别获取到系统的1,5,15分钟的负载信息,并返回JSON格式.
def Get_Load_Info(addr):
    try:
        Head = ["HostName","Load1","Load5","Load15"]
        SysLoad = []
        ret = os.popen("snmpwalk -v 2c -c nmap " + addr + " .1.3.6.1.2.1.1.5")
        SysLoad.append(ret.read().split(":")[3].strip())
 
        ret = os.popen("snmpwalk -v 2c -c nmap " + addr + " .1.3.6.1.4.1.2021.10.1.3")
        load = list(re.sub(".*STRING: ", "", ret.read()).split("\n"))
        SysLoad.append(load[0])
        SysLoad.append(load[1])
        SysLoad.append(load[2])
        return dict(zip(Head,SysLoad))
    except Exception:
        return 0
 
# 通过SNMP获取系统内存占用: 内存利用率,获取到之后,将其转化为字典格式保存。
def Get_Mem_Info(addr):
    try:
        Head = ["HostName","memTotalSwap","memAvailSwap","memTotalReal","memTotalFree"]
        SysMem = []
        ret = os.popen("snmpwalk -v 2c -c nmap " + addr + " .1.3.6.1.2.1.1.5")
        SysMem.append(ret.read().split(":")[3].strip())
        ret = os.popen("snmpwalk -v 2c -c nmap " + addr + " .1.3.6.1.4.1.2021.4")
        mem = ret.read().split("\n")
        for i in [2,3,4,6]:
            SysMem.append(re.sub(".*INTEGER: ","",mem[i]).split(" ")[0])
        return dict(zip(Head,SysMem))
    except Exception:
        return 0
 
# 通过SNMP获取系统磁盘数据: 这个案例并不完整,我只写了一点,后面有个问题一直没有解决.
def Get_Disk_Info(addr):
    try:
        dic = {}
        list = []
        ret = os.popen("snmpwalk -v 2c -c nmap " + addr + " HOST-RESOURCES-MIB::hrStorageDescr")
        DiskName = ret.read().split("\n")
        ret =os.popen("snmpwalk -v 2c -c nmap " + addr + " HOST-RESOURCES-MIB::hrStorageUsed")
        DiskUsed = ret.read().split("\n")
        ret = os.popen("snmpwalk -v 2c -c nmap " + addr + " HOST-RESOURCES-MIB::hrStorageSize")
        DiskSize = ret.read().split("\n")
 
        for i in range(1,len(DiskName) - 7):
            dic["Name"]= DiskName[i + 5].split(":")[3]
            dic["Used"]= DiskUsed[i + 5].split(":")[3]
            dic["Size"]= DiskSize[i + 5].split(":")[3]
            list.append(dic)
        return list
    except Exception:
        return 0
 
if __name__ == '__main__':
    for i in range(100):
        dic = Get_CPU_Info("192.168.1.20")
        print(dic)
        time.sleep(1)

我们使用pysnmp模块来做,安装pysnmp很简单,执行命令pip install pysnmp 即可,安装后使用以下代码执行即可获取到目标数据,获取方式分为两种一种为Get另一种为Walk.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from pysnmp.hlapi import *
import os,sys
 
class NetSNMP():
    def __init__(self,address,region):
        self.region = region
        self.address = address
 
    # 获取指定数据的方法
    def GetNumber(self,oid,sub_oid,sub_id):
        iterator = getCmd(SnmpEngine(),
                          CommunityData(self.region),
                          UdpTransportTarget((self.address, 161)),
                          ContextData(),
                          ObjectType(ObjectIdentity(oid, sub_oid, sub_id)))
        errorIndication, errorStatus, errorIndex, varBinds = next(iterator)
 
        if errorIndication:
            return False
        else:
            if errorStatus:
                return False
            else:
                for varBind in varBinds:
                    return [x.prettyPrint() for x in varBind]
 
    # 使用Walk拉取数据
    def WalkNumber(self, oid):
        res = []
        for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(SnmpEngine(),
             CommunityData(self.region),UdpTransportTarget((self.address, 161)),ContextData(),
             ObjectType(ObjectIdentity(oid)).addMibSource(
             './site-packages/pysnmp/smi/mibs','pysnmp_mibs'),lexicographicMode=False):
            if errorIndication:
                print(errorIndication, file=sys.stderr)
                break
            elif errorStatus:
                print('%s at %s' % (errorStatus.prettyPrint(),
                                    errorIndex and varBinds[int(errorIndex) - 1][0] or '?'),
                      file=sys.stderr)
                break
            else:
                for varBind in varBinds:
                    res.append(str(varBind))
        return res
 
if __name__ == "__main__":
 
    # 初始化
    ptr = NetSNMP("192.168.81.130","public")
 
    # 使用GET方式获取OID数据
    ret = ptr.GetNumber("HOST-RESOURCES-MIB","hrMemorySize",0)
    print("类型: {} --> 返回结果: {} --> 解析: {}".format(type(ret),ret,ret[1]))
 
    # 使用Walk方式获取OID数据
    ret = ptr.WalkNumber(".1.3.6.1.2.1.2.2.1.6")
    for each in ret:
        mac = each.split("=")[1]
        if len(mac) > 1:
            print("网卡接口: {}".format(mac))

以上就是Python封装SNMP调用接口的示例代码的详细内容,更多关于Python SNMP调用接口的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/LyShark/p/16512846.html

延伸 · 阅读

精彩推荐
  • PythonPython利用Beautiful Soup模块搜索内容详解

    Python利用Beautiful Soup模块搜索内容详解

    这篇文章主要给大家介绍了python中 Beautiful Soup 模块的搜索方法函数。 方法不同类型的过滤参数能够进行不同的过滤,得到想要的结果。文中介绍的非常详...

    Glumes2992020-09-27
  • PythonPython第三方库jieba库与中文分词全面详解

    Python第三方库jieba库与中文分词全面详解

    jieba库是一款优秀的Python第三方中文分词库,jieba支持三种分词模式:精确模式、全模式和搜索引擎模式,下面这篇文章主要给大家介绍了关于Python第三方库ji...

    Argonaut_3902022-07-07
  • PythonPython深度学习理解pytorch神经网络批量归一化

    Python深度学习理解pytorch神经网络批量归一化

    这篇文章主要是Python深度学习篇,通过示例的详解让大家更好的理解pytorch神经网络批量归一化,有需要的的朋友可以借鉴参考下,希望能够有所帮助...

    Supre_yuan8702022-01-21
  • PythonPython实现信息轰炸工具(再也不怕说不过别人了)

    Python实现信息轰炸工具(再也不怕说不过别人了)

    不知道各位小伙伴有没有遇到过这样的一个故事,发现自己直接喷不过,打字速度不够给力.下面这篇文章就能解决自己喷不过的苦恼,话不多说,上才艺,需要的...

    韶光不负10912021-11-30
  • PythonPython数据持久化shelve模块用法分析

    Python数据持久化shelve模块用法分析

    这篇文章主要介绍了Python数据持久化shelve模块用法,结合实例形式较为详细的总结分析了shelve模块的功能、原理及简单使用方法,需要的朋友可以参考下...

    Bigberg10232021-03-11
  • Python如何优雅地改进Django中的模板碎片缓存详解

    如何优雅地改进Django中的模板碎片缓存详解

    这篇文章主要给大家介绍了关于如何优雅地改进Django中的模板碎片缓存的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的...

    栖迟于一丘10872021-03-13
  • PythonPython判断文本中消息重复次数的方法

    Python判断文本中消息重复次数的方法

    这篇文章主要介绍了Python判断文本中消息重复次数的方法,涉及Python针对文本文件的读取与字符串操作的相关技巧,需要的朋友可以参考下...

    阿涵-_-7472020-08-21
  • PythonPython代码实现列表分组计数

    Python代码实现列表分组计数

    这篇文章主要介绍了Python代码实现列表分组计数,利用Python代码实现了使用分组函数对列表进行分组,并计算每组的元素个数的功能,需要的朋友可以参考一...

    Felix5212022-02-28