Kidscorpion Posted June 19, 2008 Posted June 19, 2008 (edited) Hi All,I am newbie to this forum.I need a script to ping gateway and if gateway is down, change default gateway setting in TCP/IP property and switch to new gateway.I think it can do with vbscript. But i don't know how write.Could someone help me or guide me?Thanks you very much for your time and sharing knowledge. Edited June 19, 2008 by Kidscorpion
CoffeeFiend Posted June 19, 2008 Posted June 19, 2008 That is both a simple and a complicated problem at once. Ping'ing is trivial (you use the Win32_PingStatus WMI class, or if you have older OS'es i.e. win2k and below, then you have have to call ping.exe and parse the text output). As for the gateways, you can enumerate them (string DefaultIPGateway[]; property of the class), and set it with the SetGateways method of the same class. All good so far.Now the hard part: how do you determine which NIC to use it against if it has more than one? (this box has 4)A quick and dirty script (5 minute job, could be a lot nicer), that works more or less blindly, and tries to set it on all NICs:On Error Resume NextstrComputer = "."Dim arrGateways(0)arrGateways(0) = "192.168.1.1"Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colNicConfigs = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")For Each objNicConfig In colNicConfigs If Not IsNull(objNicConfig.DefaultIPGateway) Then strDefaultIPGateway = Join(objNicConfig.DefaultIPGateway) Set colPings = objWMIService.ExecQuery ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strDefaultIPGateway & "'") For Each objPing in colPings If objPing.StatusCode <> 0 Then objNicConfig.SetGateways(arrGateways) End If Next Else 'gateway is not set! uncomment next line to force set it 'objNicConfig.SetGateways(arrGateways) End IfNextChange the gateway IP from 192.168.1.1 to whatever you need to set it to. You can also make the array bigger, and add more values to it if you need more than one gateway.Anything else just ask.
Kidscorpion Posted June 19, 2008 Author Posted June 19, 2008 HI Crahak,Thanks a lot for your help. I will try.All pc are WinXp with one NIC. We have only two gateways for this branch. If one down, we want to switch to another one.Cheers....!
CoffeeFiend Posted June 19, 2008 Posted June 19, 2008 All pc are WinXp with one NIC. We have only two gateways for this branch. If one down, we want to switch to another one.Then the script should work.You could try having both Gateways there in the config though, like such:Dim arrGateways(1)arrGateways(0) = "192.168.1.1"arrGateways(1) = "192.168.1.2"Where 192.168.1.1 and 192.168.1.2 are the addresses of both your gateways, but I'm not 100% sure if windows will auto-switchover if one fails (we don't have redundant gateways, never tried, or looked into it much for that matter). You might wanna look at MS Article ID's 128978 and 171564.
Kidscorpion Posted June 19, 2008 Author Posted June 19, 2008 Dear Crahak,What i am trying to do is that i want to ping from client PC in every minute to default Router( we have 2 routers) and if the link is down, it will need to change default gateway to another router.I had tried manually with Netsh command to change default gateway setting in TCP/IP properties.Currently, if the link is down, user need to manually run this Netsh batch file to change default gateway.After that their pc can switch to another default gateway.I think it will ok with vbscript for auto ping and change default gateway setting.But the problem is that i have a little knowledge in vbscript.Thanks for your time and knowledge sharing.
Mijzelf Posted June 19, 2008 Posted June 19, 2008 What about a batchfile?@echo off set GATEWAY1=192.168.1.1 set GATEWAY2=192.168.1.2 set CURRENTGATEWAY=%GATEWAY2% :CheckGateway ping %CURRENTGATEWAY% | find "TTL=" if errorlevel 1 goto ChangeGateway </P><P>:: When we arrive here the gateway was reachable. Wait for a minute :: 192.168.254.254 should be unreachable ping 192.168.254.254 -n 1 -w 60000 >NUL goto CheckGateway :ChangeGateway if "%GATEWAY1%"=="%CURRENTGATEWAY%" goto ChangeGateway2 set CURRENTGATEWAY=%GATEWAY1% call ChangeGateway.cmd %CURRENTGATEWAY% goto CheckGateway :ChangeGateway2 set CURRENTGATEWAY=%GATEWAY2% call ChangeGateway.cmd %CURRENTGATEWAY% goto CheckGatewayWhen you use this code you'll have to retype the | between ping and find.
Kidscorpion Posted June 19, 2008 Author Posted June 19, 2008 HI Mijzelf,Thanks for your help.I will try and revert to you.Have a nice day.
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now