在snmpwalk介绍及其用法一文中,介绍过net-snmp的snmpwalk的用法,殊不知,net-snmp还有trap的命令程序,可以用来测试snmp的trap方法(包括inform方法)。这些命令程序是: snmptrap、snmpinform和snmptrapd。其中:

  • snmptrap:可以模拟snmp agent发送一个trap到snmp管理端(一般称为网管,snmp manager或snmp client);
  • snmpinform:可以模拟snmp agent发送一个inform request到snmp管理端(Trap是发送给SNMP管理者的通知网络状况等的警告消息,而Inform是需要SNMP管理者确认接收的Trap。与Inform 相比较,Trap通知方式为不可靠传输,因为snmp管理端在收到一条Trap通知后无需回复任何确认信息,所以snmp agent无法知道Trap通知是否已经被snmp管理端正确接收);
  • snmptrapd:一个模拟snmp管理端接收trap/inform通知的程序;

由上可见,通过上述3个命令程序,我们可以研究snmp的trap行为及其报文,如果你正在开发snmp agent的trap功能但又没有snmp管理端,这3个程序则可帮上你的大忙。下面详细介绍这3个命令程序的具体用法。

snmptrapd的配置及其使用

snmptrapd.conf文件

跟snmp agent一样,snmptrapd也需要一个配置文件才能运行,否则会提示不能接收trap的信息。snmptrapd所需的配置文件名称为:snmptrapd.conf,安装net-snmp后,默认是没有这个配置文件的,因此需要新建此文件。snmptrapd.conf文件可放在net-snmp的安装路径下,也可放在其它路径中,但需要在启动运行时指定该文件。建立后,添加如下文件内容:

authCommunity log,execute,net public



上面语句指明以“public”为“community”请求的snmp “notification”允许的操作[见参考资料2]。上面添加的是比较简单的配置,但可使snmptrapd程序运行起来并正确接收trap包。snmptrapd.conf文件更详细的配置及其解释参考资料[1]。


运行snmptrapd程序





sudo snmptrapd -C -c Documents/snmptrapd.conf -df -Lo


上面命令中的选项表示:


  • -C : 表示不使用net-snmp默认路径下的配置文件snmptrapd.conf;
  • -c : 指定snmptrapd.conf文件;
  • -d : 显示收到和发送的数据报,通过这个选项可以看到数据报文;
  • -f  : 默认情况下,snmptrapd是在后台中运行的,加上这个选项,表示在前台运行;
  • -L : 指定日志记录在哪里,后面的o表示直接输出到屏幕上,如果是跟着f表示日志记录到指定的文件中;

可通过snmptrapd -h查看命令帮助了解该命令的使用。

 snmptrap的使用方法

 snmptrap可模拟发送不同snmp协议版本的trap包。各协议版本的snmptrap使用方法略有不同。

发送V1版本trap报文的方法




sudo snmptrap -v1 -c public 10.10.12.219 1.3.6.1.4.1.1 10.10.12.219 2 3 1000 1.3.6.1.9.9.44.1.2.1 i 12 1.3.4.1.2.3.1 s test_snmptrap


上面指令各项参数的意思分别为:


snmptrap

-v1

-c public

10.10.12.219

1.3.6.1.4.1.1

10.10.12.219

2

3

1000

命令

Snmp协议版本

共同体

Snmp管理端IP 

Enterprise-OID

Snmp代理IP

Trap类型

 Trap特征码

uptime

 

1.3.6.1.9.9.44.1.2.1

I

12

12 1.3.4.1.2.3.1

s

test_snmptrap

被发送参数的OID

数据类型

数据值

被发送参数的OID

数据类型

数据值

 

上面中,snmptrapd和snmptrap都在虚拟Ubuntu中执行的,所以snmp管理端IP和snmp代理IP一样。执行后,在执行snmptrapd的窗口中可接收到数据,如下:

04 01 01 40  04 0A 0A 0C  DB 02 01 02    .+…..@……..

0032: 02 01 03 43  02 03 E8 30  29 30 0E 06  09 2B 06 01    …C…0)0…+..

0048: 09 09 2C 01  02 01 02 01  0C 30 17 06  06 2B 04 01    ..,……0…+..

0064: 02 03 01 04  0D 74 65 73  74 5F 73 6E  6D 70 74 72    …..test_snmptr

0080: 61 70                                                 ap

2013-03-23 17:44:32 ubuntu.local [10.10.12.219] (via UDP: [10.10.12.219]:52930) TRAP, SNMP v1, community public

 SNMPv2-SMI::enterprises.1 Link Down Trap (3) Uptime: 0:00:10.00

 SNMPv2-SMI::internet.9.9.44.1.2.1 = INTEGER: 12 SNMPv2-SMI::org.4.1.2.3.1 = STRING: “test_snmptrap”

snmptrapd接收到一个trap类型为2即Link Down类型,特征码为3的trap包。

发送V2版本trap报文的方法

从上面可看到,发送V1版本的trap包有较多的参数,如uptime等,而在V 2c版本中则交给了指令程序去做。






sudo snmptrap -v 2c -c public 10.10.12.219 "aaa" 1.3.6.1.4.1.2345 SNMPv2-MIB::sysLocation.0 s "just here"


上面指令各项参数的意思分别为:


  • 10.10.12.219 “aaa”:分别是snmp代理的IP和主机名称,主机名称可以为空;
  • 1.3.6.1.4.1.2345:企业OID,Enterprise-OID;
  • SNMPv2-MIB::sysLocation.0 s “just here”:分别是:数据OID、数据类型、数据值。

使用V2c版本,是不是省了很多,不用输入很多的参数选项。执行指令后,snmptrapd收到如下数据信息:

Received 75 bytes from UDP: [10.10.12.219]:40033

0000: 30 49 02 01  01 04 06 70  75 62 6C 69  63 A7 3C 02    0I…..public.<.

0016: 04 76 90 66  76 02 01 00  02 01 00 30  2E 30 15 06    .v.fv……0.0..

0032: 0A 2B 06 01  06 03 01 01  04 01 00 06  07 2B 06 01    .+………..+..

0048: 04 01 92 29  30 15 06 08  2B 06 01 02  01 01 06 00    …)0…+…….

0064: 04 09 6A 75  73 74 20 68  65 72 65                    ..just here

2013-03-24 10:35:50 ubuntu.local [UDP: [10.10.12.219]:40033]:

SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.2345 SNMPv2-MIB::sysLocation.0 = STRING: just here

 snmpinform的使用方法

snmpinform的使用跟snmptrap的使用方法是一样的,在这里就不多介绍和说明了。最后,贴上它们的使用帮助信息:

USAGE: snmptrap [OPTIONS] AGENT TRAP-PARAMETERS

  Version:  5.3.0.1

 

OPTIONS:

  -h, –help  display this help message

  -H   display configuration file directives understood

  -v 1|2c|3  specifies SNMP version to use

  -V, –version  display package version number

SNMP Version 1 or 2c specific

  -c COMMUNITY  set the community string

SNMP Version 3 specific

  -a PROTOCOL  set authentication protocol (MD5|SHA)

  -A PASSPHRASE  set authentication protocol pass phrase

  -e ENGINE-ID  set security engine ID (e.g. 800000020109840301)

  -E ENGINE-ID  set context engine ID (e.g. 800000020109840301)

  -l LEVEL  set security level (noAuthNoPriv|authNoPriv|authPriv)

  -n CONTEXT  set context name (e.g. bridge1)

  -u USER-NAME  set security name (e.g. bert)

  -x PROTOCOL  set privacy protocol (DES|AES)

  -X PASSPHRASE  set privacy protocol pass phrase

  -Z BOOTS,TIME  set destination engine boots/time

General communication options

  -r RETRIES  set the number of retries

  -t TIMEOUT  set the request timeout (in seconds)

Debugging

  -d   dump input/output packets in hexadecimal

  -D TOKEN[,...] turn on debugging output for the specified TOKENs

      (ALL gives extremely verbose debugging output)

General options

  -m MIB[:...]  load given list of MIBs (ALL loads everything)

  -M DIR[:...]  look in given list of directories for MIBs

  -P MIBOPTS  Toggle various defaults controlling MIB parsing:

     u:  allow the use of underlines in MIB symbols

     c:  disallow the use of “–” to terminate comments

     d:  save the DESCRIPTIONs of the MIB objects

     e:  disable errors when MIB symbols conflict

     w:  enable warnings when MIB symbols conflict

     W:  enable detailed warnings when MIB symbols conflict

     R:  replace MIB symbols from latest module

  -O OUTOPTS  Toggle various defaults controlling output display:

     0:  print leading 0 for single-digit hex characters

     a:  print all strings in ascii format

     b:  do not break OID indexes down

     e:  print enums numerically

     E:  escape quotes in string indices

     f:  print full OIDs on output

     n:  print OIDs numerically

     q:  quick print for easier parsing

     Q:  quick print with equal-signs

     s:  print only last symbolic element of OID

     S:  print MIB module-id plus last element

     t:  print timeticks unparsed as numeric integers

     T:  print human-readable text along with hex strings

     u:  print OIDs using UCD-style prefix suppression

     U:  don’t print units

     v:  print values only (not OID = value)

     x:  print all strings in hex format

     X:  extended index format

  -I INOPTS  Toggle various defaults controlling input parsing:

     b:  do best/regex matching to find a MIB node

     h:  don’t apply DISPLAY-HINTs

     r:  do not check values for range/type legality

     R:  do random access to OID labels

     u:  top-level OIDs must have ‘.’ prefix (UCD-style)

     s SUFFIX:  Append all textual OIDs with SUFFIX before parsing

     S PREFIX:  Prepend all textual OIDs with PREFIX before parsing

  -L LOGOPTS  Toggle various defaults controlling logging:

     e:           log to standard error

     o:           log to standard output

     n:           don’t log at all

     f file:      log to the specified file

     s facility:  log to syslog (via the specified facility)

     (variants)

     [EON] pri:   log to standard error, output or /dev/null for level ‘pri’ and above

     [EON] p1-p2: log to standard error, output or /dev/null for levels ‘p1′ to ‘p2′

     [FS] pri token:    log to file/syslog for level ‘pri’ and above

     [FS] p1-p2 token:  log to file/syslog for levels ‘p1′ to ‘p2′

  -C APPOPTS  Set various application specific behaviour:

     i:  send an INFORM instead of a TRAP

  -v 1 TRAP-PARAMETERS:

  enterprise-oid agent trap-type specific-type uptime [OID TYPE VALUE]…

  or

  -v 2 TRAP-PARAMETERS:

  uptime trapoid [OID TYPE VALUE] …