网络拓扑:
路由器
FastEthernet0/0
通过光电转换接入
10M
光纤
IP
:
218.25.158.16/24-
对应网关
218.25.158.1
Atm0/0
通过电话线接入
ADSL
FastEthernet0/1
内网接口连接防火墙
192.168.2.1/29
防火墙
外网接口
192.168.2.2/29
内网:
10.20.1.250/16
由于主干链路采用静态路由,同时光线与路由器外网接口之间还有光电转换设备,所以即使光线中断,路由器端的外网接口物理链路也不会报告错误。于是一些常用的方法例如:
backup interface
,
watch group
等都不启作用。最后还是想到了利用两条静态路由的不同
metric
值来定义备份链路,同时在主链路故障时,手动将
FastEthernet0/0
端口
shutdown
。要完成以上工作,靠人工监控很难做到即时有效。于是设计如下脚本,在
windows
中设置计划任务,每
15
分钟执行一次。基本思想就是每
15
分钟
ping
外网网关:
218.25.158.1
,如果正常,则在日志文件中添加一个时间纪录,如果不正常,再
ping
路由器的内网接口:
192.168.2.1
如果通,则表明骨干链路故障,执行
telnet
路由器的批处理,
shutdown FastEthernet0/0
,同时发送邮件给网管,提示故障是主链路故障;如果不通,则表明是内网自身问题,可能是防火墙或者路由器故障,则直接发送邮件给网管。
路由器的配置就不给出了,只要配置两条不同
metric
的静态路由即可。比较有借鉴意义的是通过脚本登录路由器并进行相关配置和通过脚本运行一些
windows
命令,例如
ping
,这些对我们日常网管还是非常有用的
Monitor.vbs
每
15
分钟运行一次
Set FileSys = CreateObject("Scripting.FileSystemObject")
Set WShShell = CreateObject("WScript.Shell")
RetCode = WShShell.Run("ping " &"218.25.158.1" , 0, True ) 'ping
外网网关
if RetCode <> 0 Then ' ping
测试未通过
RetCode = WShShell.Run("ping " &"192.168.2.1" , 0, True ) 'ping
路由器内网
ip
if RetCode <> 0 Then '
如果还
ping
不通
RetCode = WShShell.Run("sendmail2.vbs") '
网络内部故障,发送相关邮件
else
RetCode = WShShell.Run("temp.bat")
RetCode = WShShell.Run("sendmail.vbs") '
否则运行批处理并发送相关邮件
end if
else 'ping
外网网关通过,则每天形成一个日志文档,文档内容为每次脚本运行的时间
dim path,file,fso,ctf,str,strhtml,strnohtml
Set fso = CreateObject("Scripting.FileSystemObject")
Set CTF = FSO.OpenTextFile("network_"&date()&".txt",8,true,0)
ctf.writeline(now())
ctf.close
end if
temp.bat
@ECHO OFF
:: Create SendKeys script
ECHO.set sh=WScript.CreateObject("WScript.Shell")>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
:: Send “open” for Connect and wait 1 second
ECHO.sh.SendKeys "%%open " >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
:: Send R for sub-item Remote system
::ECHO.sh.SendKeys "R" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
:: Send Host Name
ECHO.sh.SendKeys "192.168.2.1" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
:: Send the key of “enter” to press Connect button
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "the password of vty" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "enable" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "the password of enable" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "conf t" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
:: del the static nat
ECHO.sh.SendKeys "no ip nat insi sour stat 192.168.2.3 218.25.158.28" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "no ip nat insi sour stat 192.168.2.4 218.25.158.29" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "int fa0/0" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "shut" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "exit" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "exit" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "exit" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
ECHO.WScript.Sleep 1000 >>_TEMP.VBS
ECHO.sh.SendKeys "quit" >>_TEMP.VBS
ECHO.sh.SendKeys "{enter}" >>_TEMP.VBS
:: Open a Telnet window - it will be the window with focus
start /B TELNET.EXE
:: Run the script to send keys to Telnet window
cscript//nologo _TEMP.VBS
:: Clear away workfile
del _temp.vbs
exit
sendmail.vbs
Set objMail = CreateObject("CDO.Message")
Set objConfig = CreateObject ("CDO.Configuration")
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.aaa.com" 'mail.aaa.com
为
smtp
服务器
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]" '
发送者账号
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "******" '
发送者密码
objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/languagecode") = "0x0804"
objConfig.Fields.Update()
Set objMail.Configuration = objConfig
objMail.Subject = "network error"
objMail.HTMLBody = "Now pinging 192.168.2.1 is ok,but pinging 218.25.158.1 is not ok!!!"
'objMail.AddAttachment([url]http://xxxxxx/xxxx.xxx[/url]) '
或者其他任何正确的
url,
包括
http,ftp,file
等等。
objMail.Send
sendmail2.vbs
参照
sendmail.vbs
即可,这里就不给了。