Jump to content

How to change Default gateway and Preferred DNS server?


Recommended Posts

Friends,my system Windows 7 Ultimate x64I want to change the default gateway
The following code does not work on every computer

RegWrite('HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{1EB6DC88-5D30-4291-B637-F28C06CADCA6}', 'DefaultGateway', 'REG_MULTI_SZ', '192.168.1.2')
Spoiler

defaultgateway.png

 

Edited by youtuber
Link to comment
Share on other sites

You can try with this :

 

#RequireAdmin
#Include "network.au3"

_SetGateways("Broadcom NetLink (TM) Gigabit Ethernet", "192.168.1.1") ; Replace "Broadcom...." by the name of the network adapter name (or connection name)

 

Link to comment
Share on other sites

Is my line of code healthy or is there another alternative?
The purpose of these codes is to change the default gateway if someone on the same network is using the internet, if someone is playing a game and it is detected that they are using the internet.

Do you think this would be a problem?

While 1
    If ProcessExists('chrome.exe') Or ProcessExists('iexplore.exe') Or ProcessExists('firefox.exe') Then
        Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.1", @SystemDir, @SW_HIDE)
    Else
        Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.2", @SystemDir, @SW_HIDE)
    EndIf
    Sleep(20)
WEnd

 

Link to comment
Share on other sites

  • Developers

You seriously want to shell 5 instances of route in a seconds endlessly and ask us whther this is "healthy"?

You also mention the forbidden word while you should know better. 

Edit: Merged related threads to keep context.

Jos 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hallo,

this is a better way

#include <AutoItConstants.au3>

$DG = GetDefaultGateway()

While 1
    If ProcessExists('chrome.exe') Or ProcessExists('iexplore.exe') Or ProcessExists('firefox.exe') Then
        If ($DG <> "192.162.1.1") Then
            Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.1", @SystemDir, @SW_HIDE)
            $DG = "192.168.1.1"
        EndIf
    Else
        If ($DG <> "192.168.1.2") Then
            Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.2", @SystemDir, @SW_HIDE)
            $DG = "192.168.1.2"
        EndIf
    EndIf
    Sleep(1000)
WEnd

Func GetDefaultGateway()
    Local $PID = Run("route print", @ScriptDir, @SW_HIDE, $STDOUT_CHILD)
    Local $out = ""

    While 1
        $out &= StdoutRead($PID)
        If @error Then ExitLoop
    WEnd
    Return StringRegExp($out, "\s*0.0.0.0\s+0.0.0.0\s+(\d+\.\d+\.\d+\.\d+).*", 1)[0]
EndFunc   ;==>GetDefaultGateway

 

Edited by bernd670

greetings
bernd


I hacked 127.0.0.1 -> pcfred6.gif

Link to comment
Share on other sites

20 hours ago, youtuber said:

@jguinch I have 35 computers.
Each computer has a different network adapter name ...

You can get the network name using _GetNetworkAdapterList , get informations with _GetNetworkAdapterInfos, check if the adapter is wireless of not using _IsWirelessAdapter

Link to comment
Share on other sites

@youtuber : here is an example or how you can do it :

#RequireAdmin

#Include "Network.au3"

Local $aNetAdapter = _GetNetworkAdapterList()

For $i = 0 To UBound($aNetAdapter) - 1
    If _IsWirelessAdapter($aNetAdapter[$i][0]) Then
        ConsoleWrite($aNetAdapter[$i][0] & ' is a wireless adapter')
    Else
        ConsoleWrite('"' & $aNetAdapter[$i][0] & '" is not a wireless adapter')
        _SetGateways($aNetAdapter[$i][0], "192.168.99.1")
    EndIf
Next

 

Link to comment
Share on other sites

  • 3 months later...
On 05.04.2018 at 8:06 PM, bernd670 said:

Hallo,

this is a better way

#include <AutoItConstants.au3>

$DG = GetDefaultGateway()

While 1
    If ProcessExists('chrome.exe') Or ProcessExists('iexplore.exe') Or ProcessExists('firefox.exe') Then
        If ($DG <> "192.162.1.1") Then
            Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.1", @SystemDir, @SW_HIDE)
            $DG = "192.168.1.1"
        EndIf
    Else
        If ($DG <> "192.168.1.2") Then
            Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.2", @SystemDir, @SW_HIDE)
            $DG = "192.168.1.2"
        EndIf
    EndIf
    Sleep(1000)
WEnd

Func GetDefaultGateway()
    Local $PID = Run("route print", @ScriptDir, @SW_HIDE, $STDOUT_CHILD)
    Local $out = ""

    While 1
        $out &= StdoutRead($PID)
        If @error Then ExitLoop
    WEnd
    Return StringRegExp($out, "\s*0.0.0.0\s+0.0.0.0\s+(\d+\.\d+\.\d+\.\d+).*", 1)[0]
EndFunc   ;==>GetDefaultGateway

 

This works better for me, but I also want to change the DNS address

I tried this for it but it does not work

Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.1 8.8.8.8 8.8.4.4", @SystemDir, @SW_HIDE)

 

Link to comment
Share on other sites

  • Developers
9 hours ago, youtuber said:

This works better for me, but I also want to change the DNS address

I tried this for it but it does not work

Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.1 8.8.8.8 8.8.4.4", @SystemDir, @SW_HIDE)

Could you please explain what you mean as you talk about DNS and the shown command is supposed to change a Route in the routing table?
So what do you want to accomplish with this commandline?

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers
8 minutes ago, youtuber said:

can it be done with this command?

No, but you could not find the answer to this yourself anywhere?

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

It would be smart to try and understand the difference between IP routing and the purpose of DNS, but fully appreciate the fact it isn't easy when starting to look at all of this.
You will find that it really isn't that difficult, but it means investing some time to learn though. 

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

yes I try for a long time but unfortunately I have not been successful I need an example
I am glad you will give me an example of how to do it.

 

Example that worked very well for me but here
In addition it would be better to change the preferred DNS server and it will be exactly what I want

On 05.04.2018 at 8:06 PM, bernd670 said:

Hallo,

this is a better way

#include <AutoItConstants.au3>

$DG = GetDefaultGateway()

While 1
    If ProcessExists('chrome.exe') Or ProcessExists('iexplore.exe') Or ProcessExists('firefox.exe') Then
        If ($DG <> "192.162.1.1") Then
            Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.1", @SystemDir, @SW_HIDE)
            $DG = "192.168.1.1"
        EndIf
    Else
        If ($DG <> "192.168.1.2") Then
            Run("route -p change 0.0.0.0 mask 0.0.0.0 192.168.1.2", @SystemDir, @SW_HIDE)
            $DG = "192.168.1.2"
        EndIf
    EndIf
    Sleep(1000)
WEnd

Func GetDefaultGateway()
    Local $PID = Run("route print", @ScriptDir, @SW_HIDE, $STDOUT_CHILD)
    Local $out = ""

    While 1
        $out &= StdoutRead($PID)
        If @error Then ExitLoop
    WEnd
    Return StringRegExp($out, "\s*0.0.0.0\s+0.0.0.0\s+(\d+\.\d+\.\d+\.\d+).*", 1)[0]
EndFunc   ;==>GetDefaultGateway

 

 

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...