Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1733920
  • 博文数量: 163
  • 博客积分: 10591
  • 博客等级: 上将
  • 技术积分: 1980
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-08 18:17
文章分类

全部博文(163)

文章存档

2018年(1)

2012年(1)

2011年(47)

2010年(58)

2009年(21)

2008年(35)

分类: WINDOWS

2010-08-10 09:52:18

自己写的一个批处理作为nagios的插件来应用,用来检查业务是否正常运行(通过查看本机端口来判断),此脚本过滤了外部链接里的端口,记录下来,方便以后查找,:)
 
 

@echo off
setlocal enabledelayedexpansion
rem        This script is finished by HongRui Wang at 2010-7-5
rem        It check port 8022/7546/12345/9001/12346/9002 on server
rem You should use check_nrpe to call this script through Windows Agent Nsclient++

set crit_num=0

rem        ### Check Port 8022 ###
rem        首先找出含有":8022"的所有条目,包括本机监听和外部连接,然后循环进行下面的检测.
netstat -na | findstr /C:":8022" > port.txt
for /F "tokens=2 usebackq" %%j in (port.txt) do (
rem        检查本机的监听端口中含有:80端口
echo %%j | findstr /C:":8022" >null
rem        如果有80端口,则跳转到ok_8022
IF !ERRORLEVEL!==0 goto ok_8022
)
rem        如果循环完成,仍然没有8022端口,则执行下面的跳转
goto bad_8022

:ok_8022
echo "Port 8022 is exsit"
goto port_7546

:bad_8022
echo "Port 8022 is not exsit"
set crit_num=1


rem        ### Check Port 7546 ###
:port_7546
netstat -na | findstr /C:":7546" > port.txt
for /F "tokens=2 usebackq" %%j in (port.txt) do (
echo %%j | findstr /C:":7546" >null
IF !ERRORLEVEL!==0 goto ok_7546
)

goto bad_7546

:ok_7546
echo "Port 7546 is exsit"
goto port_12345

:bad_7546
echo "Port 7546 is not exsit"
set crit_num=1


rem        ### Check Port 12345 ###
:port_12345
netstat -na | findstr /C:":12345" > port.txt
for /F "tokens=2 usebackq" %%j in (port.txt) do (
echo %%j | findstr /C:":12345" >null
IF !ERRORLEVEL!==0 goto ok_12345
)

goto :bad_12345

:ok_12345
echo "Port 12345 is exsit"
goto port_9001

:bad_12345
echo "Port 12345 is not exsit"
set crit_num=1
    
    
    
rem        ### Check Port 9001 ###
:port_9001
netstat -na | findstr /C:":9001" > port.txt
for /F "tokens=2 usebackq" %%j in (port.txt) do (
echo %%j | findstr /C:":9001" >null
IF ! 0 goto ok_9001
)

goto bad_9001
    
:ok_9001
echo "Port 9001 is exsit"
goto port_12346

:bad_9001
echo "Port 9001 is not exsit"
set crit_num=1


rem        ### Check Port 12346 ###
:port_12346
netstat -na | findstr /C:":12346" > port.txt
for /F "tokens=2 usebackq" %%j in (port.txt) do (
echo %%j | findstr /C:":12346" >null
IF !ERRORLEVEL!==0 goto ok_12346
)

goto bad_12346

:ok_12346
echo "Port 12346 is exsit"
goto port_9022

:bad_12346
echo "Port 12346 is not exsit"
set crit_num=1
    
    
    
rem        ### Check Port 9022 ###
:port_9022
netstat -na | findstr /C:":9022" > port.txt
for /F "tokens=2 usebackq" %%j in (port.txt) do (
echo %%j | findstr /C:":9022" >null
IF ! == 0 goto ok_9022
)

goto bad_9022

:ok_9022
echo "Port 9022 is exsit"
goto end

:bad_9022
echo "Port 9022 is not exsit"
set crit_num=1


:end
if %crit_num%==0 exit 0
if %crit_num%==1 exit 2


阅读(1741) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~