Jump to content

Toggle blockage


Recommended Posts

I`m making a script that switches the gateway when the internet is down. I have one little problem, how do i toggle between gateways ?

$dgw = $gw1
$gw1 = gate_ch1()
$gw2 = gate_ch2()
$gw3 = gate_ch3()
$gw4 = gate_ch4()

$ping_errors = 0
$ping_errors_switch = 10
While 1
    If ping("www.yagoo.com")<1 Then
        $ping_errors+=1
    Else
        $ping_errors=1
    EndIf
    
    if $ping_errors >= $ping_errors_switch Then
        $dgw
        $ping_errors = 0
    EndIf
    
WEnd

I need something that changes the $dgw between $gw1,2,3,4 constantly when the internet is down.

Link to comment
Share on other sites

While your description indicates you want to change the value of $dgw, I suspect that you want to call one of the gate_ch#() functions. Which will change the default gateway.

You can make a script that calls those functions using Call("gate_ch" & $i) with a changing value of $i, but if the functions are all almost identical it would be nicer to make a single function that takes a variable to indicate what gateway should be changed to.

Could you post the entire script, including the gate_ch#() functions, so we can make suggestions?

Link to comment
Share on other sites

Something along these lines?

$dgw = $gw1
$gw1 = gate_ch1()
$gw2 = gate_ch2()
$gw3 = gate_ch3()
$gw4 = gate_ch4()

$ping_errors = 0
$ping_errors_switch = 10

While 1
    If Ping("www.yagoo.com") < 1 Then
        $ping_errors += 1
    Else
        $ping_errors = 1
    EndIf

    If $ping_errors >= $ping_errors_switch Then
        $dgw
        $ping_errors = 0
        $gw1 = gate_ch1()
        $gw2 = gate_ch2()
        $gw3 = gate_ch3()
        $gw4 = gate_ch4()
    EndIf
WEnd
Edited by LaCastiglione
Link to comment
Share on other sites

FileWrite("D:\ip.txt" , $Net_IP)
$text = FileRead("D:\ip.txt")
$array = StringSplit($text, "," , 1)
FileDelete("D:\ip.txt")
MsgBox(0,"",$array[1])

func gate_ch1()
#RequireAdmin
Run( @comspec & ' /c netsh interface ip set address name="Local Area Connection" static ' & $array[1] & " 255.255.255.0 10.0.0.30 1","",@SW_SHOW)
EndFunc
func gate_ch1()
#RequireAdmin
Run( @comspec & ' /c netsh interface ip set address name="Local Area Connection" static ' & $array[1] & " 255.255.255.0 10.0.0.3 1","",@SW_SHOW)
EndFunc
func gate_ch1()
#RequireAdmin
Run( @comspec & ' /c netsh interface ip set address name="Local Area Connection" static ' & $array[1] & " 255.255.255.0 10.0.0.5 1","",@SW_SHOW)
EndFunc
func gate_ch1()
#RequireAdmin
Run( @comspec & ' /c netsh interface ip set address name="Local Area Connection" static ' & $array[1] & " 255.255.255.0 10.0.0.12 1","",@SW_SHOW)
EndFunc

$dgw = $gw1
$gw1 = gate_ch1()
$gw2 = gate_ch2()
$gw3 = gate_ch3()
$gw4 = gate_ch4()

$ping_errors = 0
$ping_errors_switch = 10
While 1
    If ping("www.yagoo.com")<1 Then
        $ping_errors+=1
    Else
        $ping_errors=1
    EndIf
    
    if $ping_errors >= $ping_errors_switch Then
        ..........
        $ping_errors = 0
    EndIf
    
WEnd

Here is the entire code, it's a mess :)

Link to comment
Share on other sites

If the gate_ch#() functions work the way I think they do that would always switch to gateway4, rather than cycling through them when needed.

I suspect he's looking for something like this:

Global $i = 1
Global $ping_errors = 0
Global $ping_errors_switch = 10

While 1
    If Ping("www.yagoo.com") < 1 Then
        $ping_errors += 1
    Else
        $ping_errors = 1
    EndIf
    If $ping_errors >= $ping_errors_switch Then
        Switch $i
            Case 1
                gate_ch2()
                $i = 2
            Case 2
                gate_ch3()
                $i = 3
            Case 3
                gate_ch4()
                $i = 4
            Case 4
                gate_ch1()
                $i = 1
        EndSwitch
        $ping_errors = 0
    EndIf
WEnd

But depending on what the gate_ch#() functions do it could probably be done in a nicer way.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...