Jump to content

Identify active network connection and change DNS server


Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

Please post what you have tried so far. Also, google "wmi enumerate network adapters"

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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