Jump to content
youtuber

How to change Default gateway and Preferred DNS server?

Recommended Posts

youtuber
Posted (edited)

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

Share this post


Link to post
Share on other sites
jguinch

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)

 

  • Thanks 1

Share this post


Link to post
Share on other sites
youtuber

@jguinch I have 35 computers.
Each computer has a different network adapter name and they only connect to the ethernet cable.
Do I need to add network adapter name for each one separately?

Share this post


Link to post
Share on other sites
youtuber

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

 

Share this post


Link to post
Share on other sites
Jos
Posted (edited)

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
bernd670
Posted (edited)

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
  • Thanks 1

greetings
bernd


I hacked 127.0.0.1 -> pcfred6.gif

Share this post


Link to post
Share on other sites
jguinch
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

  • Thanks 1

Share this post


Link to post
Share on other sites
youtuber

I don't know exactly how to use it.

#RequireAdmin
#Include "network.au3"
#Include <array.au3>

$infos = _GetNetworkAdapterList()
_ArrayDisplay($infos)

_SetGateways($infos, "192.168.1.1")
Spoiler

Screenshot_1.png

 

Share this post


Link to post
Share on other sites
jguinch

@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

 

  • Thanks 1

Share this post


Link to post
Share on other sites
youtuber
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)

 

Share this post


Link to post
Share on other sites
Jos
Posted (edited)
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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
youtuber

I want to change my default gateway and Preferred DNS server

Spoiler

6tEFr2.jpg

example is like this

Run(@ComSpec & " /c " & "netsh interface ip set dns ""Local area network"" static 8.8.8.8", "", @SW_HIDE)

 

can it be done with this command?

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

 

Share this post


Link to post
Share on other sites
youtuber

there are some answers but it is very complicated :unsure:

so I guess it works simpler and better for me

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

 

Share this post


Link to post
Share on other sites
Jos

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
youtuber

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

 

 

Share this post


Link to post
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

×