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/ |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脚本:
@echo off
:abc
if "%1"=="hide" goto CmdBegin
start mshta vbscript:createobject("wscript.shell").run("""%~0"" hide",)(window.close)&&exit
:CmdBegin ##转后台运行 for /f "tokens=16" %%i in ('ipconfig ^| find /i "192.168.1"') do set ip=%%i ## 获取内网IP
@ ping -n www.baidu.com >nul
@ if %errorlevel%== echo %ip%_ADSL,down_%date:~,%_%time:~,% >>\\192.168.1.10\sabc\showlogin.txt ##判定,断网则向共享发送日志
@ if %errorlevel%== echo %ip%_network,OK_%date:~,%_%time:~,% >>c:\netlog.txt ##成功,本地生成日志 choice /t /d y /n >nul #休眠时间
goto abc #循环测试
Linux 监控脚本
cat showlogin.sh 1 #!/bin/bash
#
SAMPLE=/sabc/samplelogin.txt
NOWLOG=/sabc/showlogin.txt
while :;do
if grep -vwf $SAMPLE $NOWLOG &> /dev/null;then #文件内容对比判断
DOWN=`grep -vwf $SAMPLE $NOWLOG`
IPA=`grep -vwf $SAMPLE $NOWLOG | awk -F_ '{print $1}'`
echo "$DOWN" | mutt -s "warning $IPA wan-down!!" [email protected] ##有生成新内容则截取,发送邮件
sleep
grep -vwf $SAMPLE $NOWLOG >> $SAMPLE #将新增内容添加到样本文件,以做下次判断
fi
sleep
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 & ##直接新增一条记录