robbosch
Posts: 43
Joined: 21.Sep.2004
From: Denver, CO
Status: offline
|
I took at stab at modifying this script to only make changes with the default route in the routing table rather than changing the NIC configuration. I'm not sure this is any more effective but I found it worked well for what I was trying to do. Thanks for the initial script for the baseline! I also created some variables at the top to manage loop time, IP addresses, and to determine if you want switchback to primary or not. The script assumes you have two NIC cards connected to different ISP's. I use this on a backup box rather than just the primary since I don't like the DNS timeouts with the round robin approach. I'm looking at using a different way to update DNS which will resolve this problem. Anyway, for anyone who finds it useful. ' ' This script assumes you are using a multi-homed box with two NIC's for two different ISP's. The ' script will check the availability of the PING_IP on the current ISP and then switch between them ' when one fails every XX seconds (managed by WaitVal). If SwitchBack is 1 then it will switch back ' to ISP1 when it is available. The script does NOT change NIC configuration values, only the ' routing table for the default route. ' strDebug = 2 ' Set variables for output - 0 is no output, 1 is output for initialization, 2 is full output SwitchBack = 0 ' If switchback=1 then switch back to ISP1 when available RouteFlag=1 ' Set RouteFlag = 1, 1 is ISP1, 2 is ISP2 WaitVal = 5000 ' Number of milliseconds to wait between gateway checks SwitchWait = 720 ' Number of loops before rechecking ISP1 if SwitchBack is one. The check may disconnect all internet traffic ' ' Set up the two different ISP IP addresses and names ISP1_IP = "xx.xx.xx.xx" ISP1_GateWay = "xx.xx.xx.xx" ISP2_IP = "xx.xx.xx.x" ISP2_GateWay = "xx.xx.xx.xx" PING_IP = "xx.xx.xx.xx" ' ' These adapter names must match the NIC exactly ISP1_AdapterName = "Primary ISP" ISP2_AdapterName = "Secondary ISP" ' ' Create necessary objects Dim WshShell Dim ISP1MAC Dim ISP2MAC Set WshShell = CreateObject("WScript.Shell") 'On Error Resume Next strComputer = "." ' This computer ' ' Choose ISP1 adapter by Name Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set objISP1Adapter = objWMIService._ ExecQuery("Select * from Win32_NetworkAdapter where NetConnectionID = '" _ & ISP1_AdapterName & "'") ',,48) ' ' Choose ISP2 adapter by Name Set objISP2Adapter = objWMIService._ ExecQuery("Select * from Win32_NetworkAdapter where NetConnectionID = '" _ & ISP2_AdapterName & "'") ',,48) ' ' Store the MAC addresses of each interface if strDebug <> 0 then Wscript.Echo "===================================================================" For Each objItem in objISP1Adapter if strDebug <> 0 then Wscript.Echo "Primary ISP MACAddress: " & objItem.MACAddress ISP1MAC = objItem.MACAddress ISP1InterfaceIndex = Hex(objItem.InterfaceIndex) if strDebug <> 0 then Wscript.Echo "Primary ISP InterfaceIndex: " & ISP1InterfaceIndex Next For Each objItem in objISP2Adapter if strDebug <> 0 then Wscript.Echo "Secondary ISP MACAddress: " & objItem.MACAddress ISP2MAC = objItem.MACAddress ISP2InterfaceIndex = Hex(objItem.InterfaceIndex) if strDebug <> 0 then Wscript.Echo "Secondary ISP InterfaceIndex: " & ISP2InterfaceIndex Next if strDebug <> 0 then Wscript.Echo "===================================================================" ' ' Choose ISP1 adapter CONFIGURATION by MACAddress Set CONFobjISP1Adapter = objWMIService._ ExecQuery("Select * from Win32_NetworkAdapterConfiguration where MACAddress = '" _ & ISP1MAC & "'") ',,48) ' Choose ISP2 adapter CONFIGURATION by MACAddress Set CONFobjISP2Adapter = objWMIService._ ExecQuery("Select * from Win32_NetworkAdapterConfiguration where MACAddress = '" _ & ISP2MAC & "'") ',,48) For Each objItem in CONFobjISP1Adapter if strDebug <> 0 then Wscript.Echo "Primary ISP Description: " & objItem.Description ISP1MAC = objItem.MACAddress Next For Each objItem in CONFobjISP2Adapter if strDebug <> 0 then Wscript.Echo "Secondary ISP Description: " & objItem.Description UltraMAC = objItem.MACAddress Next if strDebug <> 0 then Wscript.Echo "===================================================================" SwitchCount = 0 Do ' ' The loop checks the public IP PING_IP to see if it is reachable. If so, then it is assumed the ' ISP is up. If not, it is assumed the ISP is down. Later a periodic check is done which ' will see if ISP1 is up and switch to it if it is up. ' Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._ ExecQuery("select * from Win32_PingStatus where address = '" _ & PING_IP & "'") For Each ISPStatus in objPing If IsNull(ISPStatus.StatusCode) or ISPStatus.StatusCode<>0 Then if RouteFlag = 1 Then if strDebug = 2 Then WScript.Echo "Primary ISP Status PING Status Code: " & ISPStatus.StatusCode if strDebug = 2 Then WScript.Echo " " & Now() & " ####### Switching to Secondary ISP ##########" ' Change the routing table to use ISP2 gateway for default route WshShell.Run "route DELETE 0.0.0.0", True WshShell.Run "route ADD 0.0.0.0 MASK 0.0.0.0 " & ISP2_Gateway, True RouteFlag = 2 SwitchCount = 0 ElseIf RouteFlag = 2 Then if strDebug = 2 Then WScript.Echo "Secondary ISP Status PING Status Code: " & ISPStatus.StatusCode if strDebug = 2 Then WScript.Echo " " & Now() & " ####### Switching to Primary ISP ##########" WshShell.Run "route DELETE 0.0.0.0", True WshShell.Run "route ADD 0.0.0.0 MASK 0.0.0.0 " & ISP1_Gateway, True RouteFlag = 1 End If End If Next ' If switchback is set then increment the counter and see if we should test again If SwitchBack = 1 And RouteFlag = 2 Then SwitchCount = SwitchCount + 1 If SwitchCount = SwitchWait Then if strDebug = 2 Then WScript.Echo "Checking Primary ISP: " & ISPStatus.StatusCode if strDebug = 2 Then WScript.Echo " " & Now() & " ####### Switching to Primary ISP ##########" WshShell.Run "route DELETE 0.0.0.0", True WshShell.Run "route ADD 0.0.0.0 MASK 0.0.0.0 " & ISP1_Gateway, True RouteFlag = 1 Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._ ExecQuery("select * from Win32_PingStatus where address = '" _ & PING_IP & "'") For Each ISPStatus in objPing If IsNull(ISPStatus.StatusCode) or ISPStatus.StatusCode<>0 Then if strDebug = 2 Then WScript.Echo "Primary ISP Status PING Status Code: " & ISPStatus.StatusCode if strDebug = 2 Then WScript.Echo " " & Now() & " ####### Switching back to Secondary ISP ##########" ' Change the routing table to use ISP2 gateway for default route WshShell.Run "route DELETE 0.0.0.0", True WshShell.Run "route ADD 0.0.0.0 MASK 0.0.0.0 " & ISP2_Gateway, True RouteFlag = 2 SwitchCount = 0 Else if strDebug = 2 Then WScript.Echo "Primary ISP up, leaving active" End If Next End If End If WScript.Sleep WaitVal Loop
|