天天看點

SNMP--常用指令介紹snmptrapsnmpinfosnmptrapdsnmptranslatesnmpwalk和snmpbulkwalksnmpget和snmpbulkget

snmptrap

snmptrap和snmpinfo一樣,都是發消息給管理者,差別在snmptrap預設使用SNMP TRAP發送消息給管理者,而snmpinform預設使用INFORM-PDU發送消息給管理者,INFORM-PDU需要接收者在收到trap消息時給出應答響應,表明已接收到消息。

snmptrap指令使用-Ci參數時,也可以發送INFORM-PDU消息。

指令格式

snmptrap -v [2c|3] [COMMON OPTIONS] [-Ci] AGENT uptime trap-oid [OID TYPE VALUE]...
           

TYPE是一個單字元,表示變量類型,支援的類型如下:

  • i INTEGER
  • u UNSIGNED
  • c COUNTER32
  • s STRING
  • x HEX STRING
  • d DECIMAL STRING
  • n NULLOBJ
  • o OBJID
  • t TIMETICKS
  • a IPADDRESS
  • b BITS

使用舉例

SNMPv2使用舉例

snmptrap -v 2c -c public 192.168.100.101:162 0 1.3.6.1.4.1.2345 1.3.6.1.4.1.2345.1 s "hello!"
           
參數 含義
-v 2c snmp協定版本
-c public 共同體
192.168.100.101:162 發往的IP和端口,端口也可以不指定,預設為162
uptime
1.3.6.1.4.1.2345 Trap OID
1.3.6.1.4.1.2345.1 資料OID
s 資料類型
hello! 資料值

SNMPv3使用舉例

snmptrap -v 3 -a MD5 -A mypassword -x AES -X mypassword -l authPriv -u myuser -e 800000020109840301 192.168.100.101:162 0 1.3.6.1.4.1.2345 1.3.6.1.4.1.2345.1 s "hello!"
           

服務端可以使用下面的指令檢查是否收到了消息

snmptrapd -C -c /etc/snmp/snmptrapd.conf -df -Lo
           

snmpinfo

snmpinform -v [2c|3] [COMMON OPTIONS] AGENT uptime trap-oid [OID TYPE VALUE]...
           

可以模拟snmp agent發送一個inform請求到snmp管理端(Trap是發送給SNMP管理者的通知網絡狀況等警告消息,而Inform是需要SNMP管理者确認接收的Trap消息。與Inform 相比較,Trap通知方式為不可靠傳輸,因為snmp管理端在收到一條Trap通知後無需回複任何确認資訊,是以snmp agent無法知道Trap消息是否已經被管理端正确接收)。

snmpinfo的使用方法和snmptrap一樣。

snmptrapd

snmptrapd:一個模拟snmp管理端接收trap和inform消息的程式;

ubuntu上安裝指令:

apt install snmptrapd
           

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 -C -c /etc/snmp/snmptrapd.conf -df -Lo

上面指令中的選項表示:

  • -C : 表示不使用net-snmp預設路徑下的配置檔案snmptrapd.conf
  • -c : 指定snmptrapd.conf檔案
  • -d : 顯示收到和發送的資料報,通過這個選項可以看到資料封包
  • -f : 預設情況下,snmptrapd是在背景中運作的,加上這個選項,表示在前台運作
  • -L : 指定日志記錄在哪裡,後面的o表示直接輸出到螢幕上,如果是跟着f表示日志記錄到指定的檔案中

可通過snmptrapd -h檢視指令幫助了解該指令的使用。

snmptranslate

在數字和文本之間轉換MIB的OID的名字

$ snmptranslate -On IP-MIB::ipAdEntAddr
.1.3.6.1.2.1.4.20.1.1
           
$ snmptranslate -Of IP-MIB::ipAdEntAddr
.iso.org.dod.internet.mgmt.mib-2.ip.ipAddrTable.ipAddrEntry.ipAdEntAddr
           
$ snmptranslate -Td IP-MIB::ipAdEntAddr
IP-MIB::ipAdEntAddr
ipAdEntAddr OBJECT-TYPE
  -- FROM	IP-MIB
  SYNTAX	IpAddress
  MAX-ACCESS	read-only
  STATUS	deprecated
  DESCRIPTION	"The IPv4 address to which this entry's addressing
            information pertains."
::= { iso(1) org(3) dod(6) internet(1) mgmt(2) mib-2(1) ip(4) ipAddrTable(20) ipAddrEntry(1) 1 }
           
$ snmptranslate -Ts | head
.iso.org
.iso.org.dod
.iso.org.dod.internet
.iso.org.dod.internet.directory
.iso.org.dod.internet.mgmt
.iso.org.dod.internet.mgmt.mib-2
.iso.org.dod.internet.mgmt.mib-2.system
.iso.org.dod.internet.mgmt.mib-2.system.sysDescr
.iso.org.dod.internet.mgmt.mib-2.system.sysObjectID
.iso.org.dod.internet.mgmt.mib-2.system.sysUpTime
           
$ snmptranslate -Tl | head
.iso(1).org(3)
.iso(1).org(3).dod(6)
.iso(1).org(3).dod(6).internet(1)
.iso(1).org(3).dod(6).internet(1).directory(1)
.iso(1).org(3).dod(6).internet(1).mgmt(2)
.iso(1).org(3).dod(6).internet(1).mgmt(2).mib-2(1)
.iso(1).org(3).dod(6).internet(1).mgmt(2).mib-2(1).system(1)
.iso(1).org(3).dod(6).internet(1).mgmt(2).mib-2(1).system(1).sysDescr(1)
.iso(1).org(3).dod(6).internet(1).mgmt(2).mib-2(1).system(1).sysObjectID(2)
.iso(1).org(3).dod(6).internet(1).mgmt(2).mib-2(1).system(1).sysUpTime(3)
           

snmpwalk和snmpbulkwalk

相同點:

  • 都是對一個子樹值的周遊
  • 用法基本相同

不同點:

  • snmpwalk用的是SNMP GETNEXT請求,查詢到的值是一個一個傳回給agent;
  • snmpbulkwalk用的是SNMP GETBULK請求,查詢的結果會一次性傳回給agent,這在查詢的結果很多時效率會比snmpwalk高。

使用舉例:

snmpbulkwalk -v2c -Os -c public localhost hrStorageSize
           

輸出

hrStorageSize.1 = INTEGER: 2017124
hrStorageSize.3 = INTEGER: 4114272
hrStorageSize.6 = INTEGER: 2017124
hrStorageSize.7 = INTEGER: 769436
hrStorageSize.8 = INTEGER: 1616
hrStorageSize.10 = INTEGER: 2097148
hrStorageSize.31 = INTEGER: 12835448
hrStorageSize.37 = INTEGER: 50429
hrStorageSize.39 = INTEGER: 252140
hrStorageSize.40 = INTEGER: 1280
hrStorageSize.41 = INTEGER: 252140
hrStorageSize.85 = INTEGER: 50428
hrStorageSize.88 = INTEGER: 50428
           
snmpwalk -v 2c -c public localhost IP-MIB::ipAdEntAddr
           

輸出

IP-MIB::ipAdEntAddr.127.0.0.1 = IpAddress: 127.0.0.1
IP-MIB::ipAdEntAddr.192.168.100.181 = IpAddress: 192.168.100.181
           

snmpget和snmpbulkget

snmpget隻适用于OID值是葉子節點的情況,可以一次查詢多個OID

snmpget -v2c -c public localhost sysDescr.0 ifIndex.1
           

輸出

sysDescr.0 = STRING: Linux ubuntu 4.18.0-16-generic #17~18.04.1-Ubuntu SMP Tue Feb 12 13:35:51 UTC 2019 x86_64
ifIndex.1 = INTEGER: 1
           

snmpbulkget查詢的OID可以是非葉子節點,也可以一次查詢多個OID

snmpbulkget -v2c -c public localhost hrStorageSize ipAdEntAddr
           

輸出

HOST-RESOURCES-MIB::hrStorageSize.1 = INTEGER: 2017124
IP-MIB::ipAdEntAddr.127.0.0.1 = IpAddress: 127.0.0.1
HOST-RESOURCES-MIB::hrStorageSize.3 = INTEGER: 4114272
IP-MIB::ipAdEntAddr.192.168.100.181 = IpAddress: 192.168.100.181
HOST-RESOURCES-MIB::hrStorageSize.6 = INTEGER: 2017124
IP-MIB::ipAdEntIfIndex.127.0.0.1 = INTEGER: 1
HOST-RESOURCES-MIB::hrStorageSize.7 = INTEGER: 769484
IP-MIB::ipAdEntIfIndex.192.168.100.181 = INTEGER: 2
HOST-RESOURCES-MIB::hrStorageSize.8 = INTEGER: 1616
IP-MIB::ipAdEntNetMask.127.0.0.1 = IpAddress: 255.0.0.0
HOST-RESOURCES-MIB::hrStorageSize.10 = INTEGER: 2097148
IP-MIB::ipAdEntNetMask.192.168.100.181 = IpAddress: 255.255.255.0
HOST-RESOURCES-MIB::hrStorageSize.31 = INTEGER: 12835448
IP-MIB::ipAdEntBcastAddr.127.0.0.1 = INTEGER: 0
HOST-RESOURCES-MIB::hrStorageSize.37 = INTEGER: 50429
IP-MIB::ipAdEntBcastAddr.192.168.100.181 = INTEGER: 1
HOST-RESOURCES-MIB::hrStorageSize.39 = INTEGER: 252140
IP-MIB::ip.21.1.1.0.0.0.0 = IpAddress: 0.0.0.0
HOST-RESOURCES-MIB::hrStorageSize.40 = INTEGER: 1280
IP-MIB::ip.21.1.1.169.254.0.0 = IpAddress: 169.254.0.0
           

繼續閱讀