python snmpwalk
时间: 2023-11-14 07:11:57 浏览: 49
Python中的snmpwalk是一个用于通过SNMP协议获取网络设备信息的工具。它可以通过指定IP地址和SNMP community字符串来获取设备的各种信息,例如CPU利用率、内存占用、磁盘使用情况等。在Python中,我们可以使用os模块来执行snmpwalk命令,并使用re模块来解析命令输出的结果。通过这些工具,我们可以轻松地获取网络设备的各种信息,并进行分析和处理。
相关问题
python snmp采集交换机信息
您好!要使用Python进行SNMP采集交换机信息,可以使用PySNMP库来实现。以下是一个简单的示例代码,演示如何使用PySNMP库采集交换机的信息:
```python
from pysnmp.hlapi import *
def snmp_walk(ip, community, oid):
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData(community),
UdpTransportTarget((ip, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)),
lexicographicMode=False)
)
if errorIndication:
print(f"SNMP error: {errorIndication}")
elif errorStatus:
print(f"SNMP error: {errorStatus.prettyPrint()} at {errorIndex and varBinds[int(errorIndex) - 1][0] or '?'}")
else:
for varBind in varBinds:
print(f"{varBind[0]} = {varBind[1]}")
# 交换机IP地址
ip = "交换机IP地址"
# SNMP团体字
community = "SNMP团体字"
# OID
oid = "要采集的OID"
snmp_walk(ip, community, oid)
```
请将代码中的"交换机IP地址"替换为实际的交换机IP地址,将"SNMP团体字"替换为实际的SNMP团体字,将"要采集的OID"替换为实际要采集的OID。
这个示例代码使用了pysnmp.hlapi模块中的getCmd函数来实现SNMP的GET操作,并使用了snmp_walk函数来进行SNMP的WALK操作。您可以根据需要修改代码以适应您的具体要求。
希望这可以帮助到您!如果有任何问题,请随时提问。
python 正则匹配~ # snmpwalk -v 2c -c zte_public 190.254.0.3:1611.3.6.1.4.1.3902.6053.19.1.3.2.7\ ... $da 8:15 0 477G 0 disk\n\ ... ~ # snmpwalk -v 2c -c zte_public 190.254.0.3:161 1.3.6.1.4.1.3902.6053.19.1.3.2.7\ ... SNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.7.1.0 = Gauge32: 0\ ... $db 8:15 0 477G 0 disk\n\ ... SNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.14.0.0 = Gauge32: 0\ ... SNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.14.1.0 = Gauge32: 0\ ... SNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.15.1.0 = Gauge32: 0\ ... $dc 8:15 0 477G 0 disk\n\ ... SNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.20.1.0 = Gauge32: 0\ ... SNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.26.0.0 = Gauge32: 0\ ... $dd 8:15 0 477G 0 disk\n\ ... SNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.26.1.0 = Gauge32: 0"串中以$d开头,disk结尾的字符串
可以使用以下正则表达式来匹配以$d开头,disk结尾的字符串:
```python
import re
string = "# snmpwalk -v 2c -c zte_public 190.254.0.3:1611.3.6.1.4.1.3902.6053.19.1.3.2.7\ ...\n$da 8:15 0 477G 0 disk\n\ ... ~ # snmpwalk -v 2c -c zte_public 190.254.0.3:161 1.3.6.1.4.1.3902.6053.19.1.3.2.7\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.7.1.0 = Gauge32: 0\ ...\n$db 8:15 0 477G 0 disk\n\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.14.0.0 = Gauge32: 0\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.14.1.0 = Gauge32: 0\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.15.1.0 = Gauge32: 0\ ...\n$dc 8:15 0 477G 0 disk\n\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.20.1.0 = Gauge32: 0\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.26.0.0 = Gauge32: 0\ ...\n$dd 8:15 0 477G 0 disk\n\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.26.1.0 = Gauge32: 0"
pattern = r'\$d.*?disk\\n\\s*\\\.\\.\\..*?'
result = re.findall(pattern, string, re.DOTALL)
print(result)
# 输出结果:['$da\t\t\t\t\t8:15\t0\t477G\t0\tdisk\n\\ ...', '$db\t\t\t\t\t8:15\t0\t477G\t0\tdisk\n\\ ...', '$dc\t\t\t\t\t8:15\t0\t477G\t0\tdisk\n\\ ...', '$dd\t\t\t\t\t8:15\t0\t477G\t0\tdisk\n\\ ...']
```
解析一下正则表达式:
- `\` :转义字符,表示后面的字符是特殊字符,需要转义处理;
- `\$d` :匹配以$d开头的字符串;
- `.*?` :非贪婪匹配任意字符,直到下一个字符能够匹配成功;
- `disk\\n\\s*` :匹配disk和换行符,并且后面可能有多个空白字符;
- `\\\.\\\.\\.` :匹配三个连续的点(因为点是特殊字符,需要转义);
- `.*?` :非贪婪匹配任意字符,直到下一个字符能够匹配成功;
- `.*?` 和 `\\\.\\\.\\.` 重复出现多次,因为要匹配多个符合条件的字符串;
- `re.DOTALL` :表示`.`可以匹配任意字符,包括换行符。