$snmpwalk -c public -v 2c hostname .1.3.6.1.2.1.17.4.3.1.1
17.4.3.1.1.0.0.12.7.172.8 = Hex: 00 00 0C 07 AC 08
17.4.3.1.1.0.1.2.27.80.145 = Hex: 00 01 02 1B 50 91
17.4.3.1.1.0.1.3.72.77.90 = Hex: 00 01 03 48 4D 5A
17.4.3.1.1.0.1.3.72.221.191 = Hex: 00 01 03 48 DD BF
...
$snmpwalk -c public -v 2c hostname .1.3.6.1.2.1.17.4.3.1.2
17.4.3.1.2.0.0.12.7.172.8 = 13
17.4.3.1.2.0.1.2.27.80.128 = 13
17.4.3.1.2.0.1.2.27.80.145 = 13
17.4.3.1.2.0.1.2.163.145.225 = 13
...
#! / usr / bin / perl
# ================================================= ===================
#
# QUERY MAC ADDRESSES FROM 3COM SWITCH FROM SELECTED PORT
# ================================================= ===================
use strict;
use Net :: SNMP qw ( snmp_dispatcher oid_lex_sort ) ;
my $ show_vendor = 1 ;
my $ script = "/usr/share/zabbix/scripts/mac.sh -s" ;
my $ debug = 0 ;
my $ file_name = "/ tmp / $ ARGV [0] -getmac.tmp" ;
my $ interval = 90 ; #refresh rate
my $ new = 0 ;
my @ list;
my $ write_secs = ( stat ( $ file_name ) ) [ 9 ] ;
if ( $ debug == 1 ) {
print "file $ file_name updated at" , scalar ( localtime ( $ write_secs ) ) , " \ n " ;
} ;
if ( $ write_secs + $ interval < time ) { #file updated less then $ interval seconds ago
if ( $ debug == 1 ) { print "generating new mac table \ n " ; }
$ new = 1 ;
open FH, "> $ file_name " or die "can't open ' $ file_name ': $!" ;
} else {
if ( $ debug == 1 ) { print "using old mac table \ n " ; }
open FR, "< $ file_name " or die "can't open ' $ file_name ': $!" ;
while ( my $ line = < FR > ) {
chomp ( $ line ) ;
my ( $ p , $ m ) = split / ; / , $ line ;
@ list [ $ p ] = "@list [ $ p ] $ m ," ;
}
}
# =====================================
my $ session ;
my $ error ;
my $ port = $ ARGV [ 1 ] ;
if ( $ new == 1 ) {
# === Setup session to remote host ===
( $ session , $ error ) = Net :: SNMP- > session (
-hostname = > $ ARGV [ 0 ] || 'localhost' ,
-community = > 'public' ,
-version = > '2c' ,
-translate = > [ -octetstring = > 0 ] ,
-port = > 161
) ;
# === Was the session created? ===
if ( ! defined ( $ session ) ) {
printf ( "ERROR:% s \ n " , $ error ) ;
exit 1 ;
}
} ;
# ==================================
# === OIDs queried to retrieve information ====
my $ TpFdbAddress = '1.3.6.1.2.1.17.4.3.1.1' ;
my $ TpFdbPort = '1.3.6.1.2.1.17.4.3.1.2' ;
# =============================================
my $ result ;
my @ tmp;
my $ x ;
if ( $ new == 1 ) {
if ( defined ( $ result = $ session - > get_table ( $ TpFdbAddress ) ) ) {
foreach ( oid_lex_sort ( keys ( % { $ result } ) ) ) {
$ x = unpack ( 'H *' , $ result - > { $ _ } ) ;
$ x = ~ s / ( .. ( ? ! \ Z ) ) / \ 1 : / g;
push ( @ tmp, $ x ) ;
}
} else {
if ( $ debug == 1 ) {
printf ( "ERROR:% s \ n \ n " , $ session - > error ( ) ) ;
}
}
# ==========================================
# === Print the returned MAC ports ===
$ result ;
if ( defined ( $ result = $ session - > get_table ( -baseoid = > $ TpFdbPort ) ) ) {
my $ i = 0 ;
my $ out = "" ;
my $ res = 0 ;
my $ tmp_port ;
my $ tmp_mac_list = "" ;
foreach ( oid_lex_sort ( keys ( % { $ result } ) ) ) {
if ( $ result - > { $ _ } == $ port ) {
$ res = 1 ;
if ( $ show_vendor == 1 ) {
$ out = ` $ script $ tmp [ $ i ] ` ;
printf ( "% s (% s)" , $ tmp [ $ i ] , $ out ) ;
} else {
printf ( "% s" , $ tmp [ $ i ] ) ;
} ;
print "," ;
} ;
if ( $ show_vendor == 1 ) {
$ out = ` $ script $ tmp [ $ i ] ` ;
printf FH ( "% s;% s (% s) \ n " , $ result - > { $ _ } , $ tmp [ $ i ] , $ out ) ;
} else {
printf FH ( "% s;% s \ n " , $ result - > { $ _ } , $ tmp [ $ i ] ) ;
} ;
$ i ++;
}
if ( $ res == 0 ) {
print "null" ;
} ;
} else {
if ( $ debug == 1 ) {
printf ( "ERROR:% s \ n \ n " , $ session - > error ( ) ) ;
} else {
print "null" ;
} ;
}
} else {
if ( @ list [ $ port ] ) {
print "@list [ $ port ]" ;
} else {
print "null" ;
} ;
}
print " \ n " ;
# =============================================
# === Close the session and exit the program ===
if ( $ new == 1 ) {
$ session - > close;
close FH;
} else {
close FR;
}
exit 0 ;
awk --assign IGNORECASE = 1 '/ hex / && /' $ mac '/ {for (x = 3; x <= NF; x ++) {printf ("% s", $ x)}}' $ filename
#! / bin / sh
#get vendor from mac-address
if [ -z "$ 1" ] ; then
echo "no args specified, exiting! Use $ 0 [-option] mac"
echo "where [-option] can be -u (update databse and exit) or -s (silent mode, just show vendor)"
exit 1
fi
# don't forget that Zabbix set $ PATH when running scripts
filename = / usr / share / zabbix / scripts / oui.txt
tmpfile = / tmp / oui.txt
link = http: // standards.ieee.org / develop / regauth / oui / oui.txt
sed = / bin / sed
awk = / bin / awk
case $ 1 in
-s )
silent = 1
mac = $ 2
if [ -z "$ 2" ] ; then
echo "no mac specified, exiting!"
exit 1
fi
;;
-u )
wget $ link -O $ tmpfile
if [ $? -gt 0 ] ; then
echo "download error, exiting"
exit 1
else
echo "Download ok!"
echo "Moving $ tmpfile to $ filename ..."
mv -f $ tmpfile $ filename
if [ $? -gt 0 ] ; then
echo "Error!"
else
echo "Success!"
fi
exit 0
fi
;;
* )
mac = $ 1
;;
esac
if [ ! -f $ filename ] ; then
if [ -z $ silent ] ; then
echo "no mac list file, dowload it? [y / n]"
else
exit 1
fi
while :
do
read INPUT_STRING
case $ INPUT_STRING in
y )
echo "Trying to download from $ link "
wget $ link -O $ filename
if [ $? -gt 0 ] ; then
echo "download error, exiting"
exit 1
else
echo "Download ok!"
fi
break
;;
n )
echo "exiting!"
exit 0
;;
* )
echo "wrong input, use [y / n]"
;;
esac
done
fi
if [ $ {# mac} -lt 8 ] ; then
mac = ` echo " $ mac " | $ sed 's / ^ \ (.. \) \ (.. \) \ (.. \) / \ 1- \ 2- \ 3 /' `
else
mac = ` echo " $ mac " | $ sed -e 's /: / - / g' `
fi
mac = $ {mac: 0: 8}
if [ -z $ silent ] ; then
echo "Searching for $ mac ..."
fi
result = ` $ awk --assign IGNORECASE = 1 '/ hex / && /' $ mac '/ {for (x = 3; x <= NF; x ++) {printf ("% s ", $ x)}}' $ filename `
if [ -z " $ result " ] ; then
result = "no info"
fi
echo -n $ result
$./mac.sh "000000"
no mac list file, dowload it? [y/n]
y
Trying to download from standards.ieee.org/develop/regauth/oui/oui.txt
--2011-10-06 14:20:16-- standards.ieee.org/develop/regauth/oui/oui.txt
proxy.organization.ltd... 192.168.0.1
proxy.organization.ltd|192.168.0.1|:3128... .
Proxy , ... 200 OK
: 2493060 (2,4M) [text/plain]
Saving to: «oui.txt»
100%[==========================================================================================================================================================================>] 2 493 060 784K/s 3,1s
2011-10-06 14:20:26 (784 KB/s) - «oui.txt» saved [2493060/2493060]
Download ok!
Searching for 00-00-00...
XEROX CORPORATION
### Option: Timeout
# Specifies how long we wait for agent, SNMP device or external check (in seconds).
# Range: 1-30
Timeout=5
### Option: ExternalScripts
# Location of external scripts
ExternalScripts=/usr/share/zabbix/scripts/
Source: https://habr.com/ru/post/128439/