Tuesday, July 31, 2012

My Sin

The following Windows batch files will enable you to to get a port name (description). This could be done much much better in powershell or any other real scripting language, but I really like doing things the hard way.

awk.exe and grep.exe are from gnuwin32.sourceforge.net
snmpget.exe from http://www.net-snmp.org/
change $COMMUNITYSTRING$ to your read-only snmp community string.
change $SITENAME$ to a string that will be matched in all your routers. For example, if your company is called Apeture Technologix and your routers are called aptech1, aptech2, etc, then use aptech. If you don't have this, then you will have to change these batch files.
---
getport.bat
---
@echo off
IF X%1 == X GOTO getinfo
echo %1
set communitystring=$COMMUNITYSTRING$
set dot1dTpFdbPort=1.3.6.1.2.1.17.4.3.1.2
set dot1dBasePortIfIndex=1.3.6.1.2.1.17.1.4.1.2.
set mibif=1.3.6.1.2.1.2.2.1.

cmd.exe /c dmac.bat %1>dmac.t
cmd.exe /c routername.bat %1>rname.t
cmd.exe /c vlan.bat %1>vlan.t


for /F %%R in ('type rname.t') do set rname=%%R
for /F %%R in ('type dmac.t') do set dmac=%%R
for /F %%R in ('type vlan.t') do set vlan=%%R
@echo on
snmpget -v 2c -c %communitystring%@%vlan% %rname% %dot1dTpFdbPort%%dmac%|grep -o [0-9]*$>idx1.t
@echo off
for /F %%R in ('type idx1.t') do set idx1=%%R
@echo on
snmpget -v 2c -c %communitystring%@%vlan% %rname% %dot1dBasePortIfIndex%%idx1%|grep -o [0-9]*$>idx2.t
@echo off
for /F %%R in ('type idx2.t') do set idx2=%%R

echo %rname%
rem for /L %%J in (1,1,22) do
@echo on
snmpget -v 2c -c %communitystring% %rname% %mibif%2.%idx2%
@echo off

del *.t
GOTO end

:getinfo
set /p hostnametoget="hostname?> "
start getport.bat %hostnametoget%
exit

:end

---
dmac.bat
---
@echo off
del tempmac.t 2>nul

del tempdmac.t 2>nul

nbtstat.exe /a %1|grep -o ..-..-..-..-..-..|grep -o [0-9A-F].|awk "/[0-9A-F]./ { print \"0x\"$1 }" >tempmac.t

for /F %%I in ('type tempmac.t') do cmd /c @h2d.bat %%I>>tempdmac.t

SETLOCAL ENABLEDELAYEDEXPANSION
set MACSTRING=
for /F %%Q in ('type tempdmac.t') do set MACSTRING=!MACSTRING!.%%Q
echo !MACSTRING!>tempdmac.t
type tempdmac.t

---
h2d.bat
---
@echo off
(set /a mytempresult = %1)
set mytempresult | grep.exe -o [0-9]*$
set mytempresult=

---
routername.bat
---
@echo off
setlocal ENABLEEXTENSIONS
cmd.exe /c @router %1>rtr.t
for /F %%Q in ('type rtr.t') do set rtr2=%%Q
ping.exe -a -n 1 %rtr2%|grep -i $SITENAME$|grep -o $SITENAME$.*com
set rtr2=
del rtr.t

---
router.bat
---
@ping.exe -n 1 %1|grep Reply |grep -o 120.*:|grep -o ^[0-9]*\.[0-9]*\.[0-9]*\.|awk "{print $0 1}"

---
vlan.bat
---
@echo off
setlocal ENABLEEXTENSIONS
cmd /c @router.bat %1>rtr.t
for /F %%Q in ('type rtr.t') do set rtr=%%Q
ping.exe -a -n 1 %rtr%|grep -i $SITENAME$|grep -o $SITENAME$.*com|grep -i -o Vlan[0-9]*|grep -o [0-9]*$
set rtr=

0 Comments:

Post a Comment

<< Home