Jump to content
Sign in to follow this  
Neutro

Identify active network connection and change DNS server

Recommended Posts

Neutro

Hey guys,

I need to run a script on several computers that can identify the active network connection, then for each one of them:

- if the network connection is not using DHCP (fixed IP address), change the current DNS server to another one

- if the network connection is using DHCP, do nothing.

I don't know the names of the network connections on the different computers, so the script has to find this by itself.

I've been searching about this but I'm having troubles finding a working solutions.

Any help would be greatly appreciated!

TIA

Share this post


Link to post
Share on other sites
guinness

As well as netsh.


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
water

Give ScriptOMatic a try (see the Example Script section).

Selecting WMI Class "Win32_NetworkAdapterConfiguration" should give you the needed information.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Neutro

Thanks for your answers :)

@JLogan3o13 > Since I didn't find a way to identify the name of the active network cards I didn't try anything yet ^^

@guinness > I saw before posting that netsh can indeed be used to setup a dns server on a network connection, but you first need to know the connection name to use it!

Thanks for the advice on the WMI stuff, i'll look into it :)

Share this post


Link to post
Share on other sites
Neutro

Damn this WMI stuff is amazing, thanks again guys :)

I managed to do what I wanted like this:

#requireadmin

#include <Array.au3>

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$query = ""

Local $active_netword_cards[1]
Local $network_cards_to_setup[1]

$active_netword_cards[0]=""
$network_cards_to_setup[0]=""

;getting a list of all network cards

$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
     
      if $objItem.NetConnectionStatus == "2" OR $objItem.NetConnectionStatus == "9" Then ;if the network connection is active, we add the index of the network card and the connection name to $active_netword_cards array
         
         _arrayAdd($active_netword_cards, $objItem.Index)
         _arrayAdd($active_netword_cards, $objItem.NetConnectionID)
         
      endif

   Next
  
Endif

;getting settings from all network cards in the array $active_netword_cards

for $i = 1 to UBound($active_netword_cards) - 1 step 2
  
   $query = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & $active_netword_cards[$i], "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
   
   For $objItem In $query

   if $objItem.DHCPEnabled == "False" Then _arrayAdd($network_cards_to_setup, $active_netword_cards[$i+1]) ;if DHCP is disabled, we add the network card name in the $network_cards_to_setup array
  
   next
  
Next

;setting up primary DNS server of all network cards in the $network_cards_to_setup array
;DNS server used in this example is 10.10.2.45

for $i = 1 to UBound($network_cards_to_setup) - 1 step 1
  
   Runwait('netsh interface IP ADD DNS "'& $network_cards_to_setup[$i] &'" 10.10.2.45 index=1')
  
Next
Edited by Neutro

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
Sign in to follow this  

×