[分享] SNMP主機偵測程式
這是我在學習分析snmpwalk所產生的大量資料時,邊try資料邊寫支程式
把它們撈出來。因為網路上非常少人有寫介紹這些資料意涵的文件,所以
只好自己目測。
因為Linux跟Unix吐出資料還是有差,所以部份有價值的資料並沒有寫入
程式去撈。(例如Unix/BSD連底下裝什麼套件都看的到,雖然snmpd偵測出
來的資料有些會誤判(例如FAT32檔案系統空間用量很怪),但能夠偵測出
這麼多東西已經夠恐怖了。)
並且比對三種作業系統的樣本:Solaris 10、FreeBSD 7.2、Debian 5,
扣除掉部份系統會產生的異常結構資料,才寫出來一部分。由於自己對
於這些資料還在學習階段,如果程式有寫錯也希望大家多多指點。
謝謝......
不過如果要用這支程式,必須在要偵測的主機先開啟snmpd服務,
並且在/etc/snmpd/snmpd.conf中將access MyROSystem中的read權限
打開成all,並且定義聆聽的網路介面:
interface [ethX]
agentaddress [主機IP]:161
由於這支程式是tcl寫成的,所以以後有空會改寫成視窗介面版的。
===============================================================
#!/usr/bin/tclsh8.5
puts "請輸入要偵測的主機IP"
gets stdin IP
set resultat [exec snmpwalk -v 2c -c public $IP]
set splitre [split $resultat '\n']
set lrres [llength $splitre]
puts "SNMP擷取總資料量:$lrres 筆"
set f [open tmp w+]
set cpun 0
set nonhd 0
for { set i 0 } { $i<$lrres } {incr i } {
set line [lindex $splitre $i]
puts $f $line
#0.清除奇怪資料
if {[lindex $line 0]=="HOST-RESOURCES-MIB::hrSystemInitialLoadDevice.0"} {
#這行的引號很討厭會斷兩行,讓程式判斷錯誤。
incr i 3 set line [lindex $splitre $i]
#puts $line
}
#1.測試系統
if { [lindex $line 0]=="SNMPv2-MIB::sysDescr.0" } {
set sys [lindex $line 3]
set kernel [lindex $line 5]
set sysname [lindex $line 4]
puts "作業系統:$sys $kernel"
puts "主機名稱:$sysname"
}
#2.網路介面
if { [string range $line 0 14]=="IF-MIB::ifDescr" } {
lappend netin [lindex $line 3]
}
if { [string range $line 0 20]=="IF-MIB::ifPhysAddress" } {
lappend macaddr [lindex $line 3]
}
if { [string range $line 0 19]=="IF-MIB::ifOperStatus" } {
lappend instat [lindex $line 3]
}
if { [string range $line 0 21]=="IP-MIB::ipAdEntIfIndex" } {
set linaddr [lindex $line 0]
set indexaddr [lindex $line 3]
set inaddr [string range $linaddr 23 end]
lappend laddr "$indexaddr $inaddr"
}
if { [string range $line 0 17]=="IF-MIB::ifInOctets" } {
set inoct [lindex $line 3]
lappend inoctmb [format %.2f [expr $inoct/1024.0/1024.0]]
}
if { [string range $line 0 18]=="IF-MIB::ifOutOctets" } {
set outoct [lindex $line 3]
lappend outoctmb [format %.2f [expr $outoct/1024.0/1024.0]]
}
if { [string range $line 0 28]=="TCP-MIB::tcpConnState.0.0.0.0" } {
if {[lindex $line 3]=="listen(2)"} {
lappend listenp [lindex [split [string range [lindex $line 0] 30 end] . ] 0]
}
}
#3.處理器
if { [string range $line end-16 end]=="hrDeviceProcessor" } {
incr cpun
lappend node_cpu [string range [lindex $line 0] 33 end]
}
if { [string range $line 0 32]=="HOST-RESOURCES-MIB::hrDeviceDescr" } {
foreach var $node_cpu {
if {[string range [lindex $line 0] 34 end]==$var} {
lappend cputype [lrange $line 3 end]
}
}
}
#3.系統資源
if { [string range $line 0 33]=="HOST-RESOURCES-MIB::hrFSMountPoint" } {
lappend mountp1 [lindex $line 3]
}
if {[string range [lindex $line 0] 0 27]=="HOST-RESOURCES-MIB::hrFSType"} {
set orifs [lindex $line 3]
if {[string range $orifs 24 end]=="FSSys5FS"} { lappend fstype "UFS" }
if {[string range $orifs 24 end]=="FSNFS"} { lappend fstype "NFS" }
if {[string range $orifs 24 end]=="FSFat"} { lappend fstype "FAT(Windows)" }
if {[string range $orifs 24 end]=="FSLinuxExt2"} {
lappend fstype "Ext2(Linux)"
}
if {[string range $orifs 24 end]=="FSOther"} {
lappend fstype "unknown/devfs"
}
}
if { [string range $line 0 31]=="HOST-RESOURCES-MIB::hrMemorySize" } {
set memsize [lindex $line 3]
lappend memsizem [expr $memsize/1024]
}
if { [string range $line 0 32]=="HOST-RESOURCES-MIB::hrStorageSize" } {
lappend node_storsize [string range [lindex $line 0] 34 end]
set storsize [lindex $line 3]
lappend storsizemb [expr $storsize/1024 ]
}
if { [string range $line 0 33]=="HOST-RESOURCES-MIB::hrStorageDescr" } {
lappend node_mount [string range [lindex $line 0] 35 end]
lappend mountp [lindex $line 3]
}
if { [string range $line 0 32]=="HOST-RESOURCES-MIB::hrStorageUsed" } {
lappend node_storuse [string range [lindex $line 0] 34 end]
set storuse [lindex $line 3]
lappend storusemb [expr $storuse/1024]
}
}
set stori [llength $node_storsize]
set storj [llength $node_storuse]
for { set i 0 } {$i<$stori} {incr i} {
set stor1 [lindex $storsizemb $i]
set stor3 [lindex $mountp $i]
set index_stor1 [lindex $node_storsize $i]
for {set j 0} {$j<$storj } { incr j} {
set stor2 [lindex $storusemb $j]
set index_stor2 [lindex $node_storuse $j]
if { $index_stor1==$index_stor2 } {
lappend storage "$stor3 $stor1 $stor2"
}
}
}
set addr [lsort -index 0 $laddr]
puts "網路介面:(裝置: IP位址 MAC位址)"
set lnet [llength $netin]
for { set i 0 } { $i<$lnet } {incr i} {
set netin1 [lindex $netin $i]
set macaddr1 [lindex $macaddr $i]
set addr1 [lindex $addr $i]
puts "$netin1: $addr1 $macaddr1"
}
puts "連線狀態:$instat"
puts "網路總下載流量:$inoctmb"
puts "網路總上傳流量:$outoctmb"
puts "Listen Port:$listenp"
puts "處理器數目:$cpun"
puts "CPU類型:"
set lcputype [llength $cputype]
for {set i 0} {$i<$lcputype} { incr i} {
puts [lindex $cputype $i]
}
puts "磁碟空間用量:(全部/已使用)"
set lstorage [llength $storage]
for { set i 0 } { $i<$lstorage } { incr i} {
set storage1 [lindex $storage $i]
puts "$storage1 MB "
}
puts "檔案系統:"
set lfstype [llength $fstype]
for {set i 0} { $i<$lfstype } {incr i} {
set fstype1 [lindex $fstype $i]
set mountp11 [lindex $mountp1 $i]
puts "$fstype1 $mountp11"
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.69.67.252
推
01/11 19:07, , 1F
01/11 19:07, 1F
推
01/11 19:18, , 2F
01/11 19:18, 2F
→
01/11 19:19, , 3F
01/11 19:19, 3F