linux7 snmp测试,centos7配置安装snmp

文章目录

1、安装snmp

2、设置v3

3、python实现snmp服务

4、附加

1、安装snmp

1、centos7+yum安装

yum -y install net-snmp net-snmp-utils

2、更改配置文件snmpd.conf

vi /etc/snmp/snmpd.conf

添加社区为public,名为notConfigUser

8e95cfc746c765473b775d6b7779a31f.png

8b6d75ef6bd0846102403520385af8f1.png

重启(每次修改完配置文件都需要重启)

systemctl restart snmpd

3、测试

1、查看tcp、udp端口开放

netstat -tulnp

# 查询主机信息

snmpwalk -v 2c -c public 10.0.12.59 system

出现如下内容代表成功

22ad3ba26e84cac57c03bcbb8d3fb5ad.png

2、设置v3

1、停掉服务

systemctl stop snmpd

2、设置

net-snmp-create-v3-user

2604e0b485eaf4910317a9639f09fca7.png

建议密码设置长一点,不然会设置失败

tail -n 5 /var/lib/net-snmp/snmpd.conf

5826d4953c5b1ecb21a9f5848ff2a65b.png

3、验证

snmpwalk -v3 -usuperuser -lauth -A "snmpv3@2020md5" -X "snmpv3@2020des" 10.0.12.59

999ce7aed55388b23047caee42366b2a.png

如出现如下报错,证明设置失败,原因可能是密码设置太短

6a655407c3c383076bae645f265b3257.png

需要重新设置密码

3、python实现snmp服务

# @DESC :实现snmp协议(简单网络管理协议),只有安装了snmp系统(管理信息库(MIB)、管理信息结构(SMI)及SNMP报文协议)的主机才会有响应

# 使用snmpwalk -v -2c -c public ip system 查看操作系统信息 (snmpbulkwalk代替snmpwalk)

# -v:指定snmp的版本, 1或者2c或者3

# –c:指定连接设备SNMP密码。

# snmp能够获取系统信息,内存信息,cpu信息,磁盘信息,有对应的oid

import datetime

import os

import shlex

import signal

import subprocess

from pysnmp.hlapi import *

# 获取系统信息

def run_snmp(host, public='public', v=1, timeout=3,authuser="superuser",md5pwd="snmpv3@2020md5",secpwd="snmpv3@2020des"):

""" :param host: ip :param public: 社区名 :param v: 版本1:v2,0:v1 :param timeout: 超时时间 :param authuser: v3 认证用户 :param md5pwd: md5 加密密码 :param secpwd: sec 假面描姆 :return: """

if v == 0 or v == 1:

errorIndication, errorStatus, errorIndex, varBinds = next(getCmd(

SnmpEngine(),

CommunityData(public, mpModel=v),

UdpTransportTarget((host, 161), timeout=timeout),

ContextData(),

ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))

)

if varBinds:

return ' = '.join([x.prettyPrint() for x in varBinds])

else:

# v3进行验证

command = 'snmpwalk -v3 -u{authuser} -lauth -A "{md5pwd}" -X "{secpwd}" {host} sysDescr'.format(

authuser=authuser, md5pwd=md5pwd, secpwd=secpwd, host=host)

command_split = shlex.split(command)

process = subprocess.Popen(command_split, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

start_time = datetime.datetime.now()

try:

while process.poll() is None:

now_time = datetime.datetime.now()

if (now_time-start_time).seconds > 3:

try:

os.kill(process.pid,signal.SIGTERM)

except OSError as e:

print(e,process.pid)

stdout,stderr = process.communicate()

return stdout.decode()

except:

return False

def analysis_snmp(snmp_str):

""" 解析snmp响应 :param snmp_str: SNMPv2-MIB::sysDescr.0 = Linux localhost.localdomain 3.10.0-1127.18.2.el7.x86_64 #1 SMP Sun Jul 26 15:27:06 UTC 2020 x86_64 :return: {"version":"3.10.0-1127.18.2.el7.x86_64","os":"Linux"} """

msg = {}

print('snmp_str',snmp_str)

if isinstance(snmp_str, str):

pattern = r'\d+\.(?:\w+\.)*\w+'

version_ = re.findall(pattern, snmp_str)

version = "-".join(version_)

print('版本信息为:', version)

msg['version'] = version

ospatter = r'SNMPv2-MIB::sysDescr.0 = (?:[a-zA-Z0-9]+[ |-]?[a-zA-Z0-9])*'

os_ = re.findall(ospatter, snmp_str)

os = "".join(os_).replace("SNMPv2-MIB::sysDescr.0 = ", "")

msg['os'] = os

print('系统/设备为:', os)

return msg

if __name__ == '__main__':

import re

# host_list = ['10.0.12.59','10.0.12.57','10.0.10.1','10.0.10.255','10.0.10.254']

for i in host_list:

str_msg = run_snmp(i)

analysis_snmp(str_msg)

Snmp_trap()

4、附加

设置修改(仅限v1,v2,因为v3我还不会)

在配置文件中添加

rwcommunity private default

rwcommunity6 private default

8e7bb14628087a21f0180c0a75906adb.png

验证:

snmpset -c private -v 1 ip SNMPv2-MIB::sysName.0 s ubuntu

f5a670f0ce0ab1725ee1e6e39211c727.png

linux环境下的snmp测试脚本, Installing SNMPv2C Agent Conformance Test Package 2008/04/08 IPv6 Promotion Council Terminology =========== Tester Node (TN) A tester node for the conformance tests. Node Under Test (NUT) A testee node for the conformance tests. Network Under Test The network where the conformance tests are executed. Tester Interface The network interface of TN hooked up to the Network Under Test. Interface Under Test The network interface of NUT hooked up to the Network Under Test. Prerequisites ============= Prerequisites for TN: - The package supports FreeBSD 6.0-RELEASE or higher version. - The package can also coexist with FreeBSD version of KAME. Installing the package onto TN ============================== 0. Before Starting (A) You need to install following softwares. - Perl (Required version : 5.8.7 or higher) - Net-SNMP (Required version : 5.3.1 or higher) *** You can download it from the following URL: http://net-snmp.sourceforge.net/ - v6eval (Required version : 3.0.11 or higher) Please refer to 00README.v6eval in "v6eval" for more information. *** You can download it from the following URL: http://www.tahi.org/release/ - koi (Required version : 1.1.1 or higher) Please refer to README file in "koi" package for more information. *** You can download it from the following URL: http://www.tahi.org/release/ 1. Extracting ct package % tar zxvf ct-snmpv2c-ag-X.X.tar.gz 2. Copying ct package Copy ct directory to any directory you like. % cp -pR $ORGDIR/ct-snmpv2c-ag-X.X $SOMEWHERE/ct-snmpv2c-ag-X.X % chmod -R +w $SOMEWHERE/ct-snmpv2c-ag-X.X 4. Setup ct environment % (enable TN's ipv6 capibility) % cd $SOMEWHERE/ct-snmpv2c-ag-X.X % edit config.txt % make test [End of INSTALL]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值