sh 测试网段在线主机

yum install nmap

nmap -sP  192.168.21.1/24 查看网段在线主机

grep -vwf file1 file2 文件内容比较

#!/bin/bash
#
day=`date +%F`
oldlog=/root/showlongin/amazon-login2016.txt
touch /root/showlongin/$day
nmap -sP 192.168.21.1/24 |grep -o "192.*" > /root/showlongin/$day

if grep -vwf $day $oldlog &> /dev/null;then
    echo -e "\033[31m`grep -vwf $day $oldlog`\033[0m, IP is shutdown!!"
else
    echo -e " \033[32mIt Perfect!!\033[0m"
fi
[root@beary showlongin]# ./showlongin.sh
192.168.21.52, IP is shutdown!!

 

测试在线主机 II 

适用于多台监控多台windows服务器多网卡配置

windows 运行一个ping 外网脚本,判断是否断网。正常则在本地log日志新增OK,已断开则向监控服务器发送断开日志,IP,down

Linux 安装环境:

smb共享 http://blog.chinaunix.net/uid-23069658-id-3142052.html

linux -msmtp +mutt 邮件通告: http://blog.slogra.com/post-418.html

                 http://storysky.blog.51cto.com/628458/631267/

 

windows ping脚本:

 1 @echo off
 2 :abc             
 3 if "%1"=="hide" goto CmdBegin
 4 start mshta vbscript:createobject("wscript.shell").run("""%~0"" hide",0)(window.close)&&exit
 5 :CmdBegin                                         ##转后台运行
 6 
 7 for /f "tokens=16" %%i in ('ipconfig ^| find /i "192.168.1"') do set ip=%%i                                ## 获取内网IP
 8 @ ping -n 10 www.baidu.com >nul
 9 @ if %errorlevel%==1 echo %ip%_ADSL,down_%date:~0,10%_%time:~0,5% >>\\192.168.1.10\sabc\showlogin.txt  ##判定,断网则向共享发送日志
10 @ if %errorlevel%==0 echo %ip%_network,OK_%date:~0,10%_%time:~0,5% >>c:\netlog.txt        ##成功,本地生成日志
11 
12 choice /t 1800 /d y /n >nul        #休眠时间  
13 goto abc                           #循环测试

 

Linux 监控脚本

 cat showlogin.sh

1
#!/bin/bash 2 # 3 SAMPLE=/sabc/samplelogin.txt 4 NOWLOG=/sabc/showlogin.txt 5 while :;do 6 if grep -vwf $SAMPLE $NOWLOG &> /dev/null;then #文件内容对比判断 7 DOWN=`grep -vwf $SAMPLE $NOWLOG` 8 IPA=`grep -vwf $SAMPLE $NOWLOG | awk -F_ '{print $1}'` 9 echo "$DOWN" | mutt -s "warning $IPA wan-down!!" abc@abc.cn ##有生成新内容则截取,发送邮件 10 sleep 5 11 grep -vwf $SAMPLE $NOWLOG >> $SAMPLE #将新增内容添加到样本文件,以做下次判断 12 fi 13 sleep 300 14 done

linux: 脚本后台运行  

# ./showlogin.sh &

加入开机启动项:

[root@shlogin share]# cat /etc/rc.d/rc.local 
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/share/showlogin.sh &     ##直接新增一条记录

 

posted @ 2016-11-30 17:53  信子2016  阅读(310)  评论(0编辑  收藏  举报