Jump to content

Display IP Address, Default Gateway & DNS Servers


Recommended Posts

@VijayS

Declaring some more of the variables seems to have helped.

#NoTrayIcon
#RequireAdmin
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <Date.au3>

; This script requires full Administrative rights

Opt("MustDeclareVars", 1)
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown.

TraySetClick(16) ; Only secondary mouse button will show the tray menu.

TrayCreateItem("Info")
TrayItemSetOnEvent(-1, "IP1Pressed")

TrayCreateItem("Renew")
TrayItemSetOnEvent(-1, "RenewPressed")
TrayCreateItem("")

TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "ExitScript")

TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN, "IP1Pressed")
TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "RenewPressed")

TraySetState()
IP1Pressed()

Global $oldIP, $newIP, $oldDNS, $newDNS, $stitle, $log
CheckChange(False)
;CheckChange(False)
While 1
Sleep(5000) ; Idle loop
CheckChange()
WEnd

Exit


; Functions

Func ExitScript()
Exit
EndFunc ;==>ExitScript


Func VerPressed()
Run("winver")
EndFunc ;==>VerPressed

Func IP1Pressed($stitle = "IP Addresses", $log = True)
Local $stmp = _IPDetails(1)
TrayTip($stitle, $stmp, 5, 1)
If $log Then WriteLog($stitle & @CRLF & $stmp)
;MsgBox(64, "IP Addresses",$stmp)
EndFunc ;==>IP1Pressed

Func CheckChange($log = True)
$oldIP = $newIP
$oldDNS = $newDNS
$newIP = @IPAddress1
$newDNS = GetDNS()
Local $rIP = $oldIP <> $newIP
Local $rDNS = $oldDNS <> $newDNS
If ($log) Then
If $rIP Then
IP1Pressed("IP CHANGED", $log)
MsgBox(64, "Debug IP DNS", "DNS:" & $oldDNS & " " & $newDNS & $rDNS & @CRLF & "IP:" & $oldIP & " " & $newIP & $rIP)
EndIf
If $rDNS Then
IP1Pressed("DNS CHANGED", $log)
MsgBox(64, "Debug IP DNS", "DNS:" & $oldDNS & " " & $newDNS & $rDNS & @CRLF & "IP:" & $oldIP & " " & $newIP & $rIP)
EndIf
EndIf
;$curr = GetIP (or get dns) or whatever else you want to monitor
EndFunc ;==>CheckChange

Func GetDNS()
;Occasionally returns none!, even when there are valid entries. TOFIX
Local $sResult, $line
Local $hRun = Run(@ComSpec & " /c netsh interface ip show dns", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
While 1
$line = StdoutRead($hRun)
If @error Then ExitLoop
$sResult &= $line
WEnd
;$answer1 = StringRegExp($sResult,'((?:d+)(?:.d+){3})',3)
Local $sString = StringRegExp($sResult, '((?:[1-2]?[0-9]?[0-9].){3}(?:[1-2]?[0-9]?[0-9]))', 3)
$sString = _ArrayToString($sString)
;MsgBox(64,"DNS:",$sResult & "capture:" & $sString)
Return $sString
EndFunc ;==>GetDNS

Func WriteLog($Data, $FileName = -1, $TimeStamp = True)
If $FileName == -1 Then $FileName = @ScriptDir & '' & @ScriptName & '.Log'
Local $hFile = FileOpen($FileName, 1)
If $hFile <> -1 Then
If $TimeStamp = True Then $Data = _Now() & ' - ' & $Data
FileWriteLine($hFile, $Data)
FileClose($hFile)
EndIf
EndFunc ;==>WriteLog
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func ReleasePressed()
Local $IP_Address = _RunStdOutRead('ipconfig /release')
$IP_Address = StringRegExpReplace($IP_Address, "(?i)(?s).*IP.*?(d+.d+.d+.d+).*{:content:}quot;", "1")
MsgBox(64, "Release IP Address", StringFormat("Your IP Address is: %s", $IP_Address))
EndFunc ;==>ReleasePressed

Func RenewPressed()
Local $IP_Address = _RunStdOutRead('ipconfig /renew')
$IP_Address = StringRegExpReplace($IP_Address, "(?i)(?s).*IP.*?(d+.d+.d+.d+).*{:content:}quot;", "1")
MsgBox(64, "Renew IP Address", StringFormat("Your IP Address is: %s", $IP_Address))
EndFunc ;==>RenewPressed

Func FlushPressed()
Local $IP_Address = _RunStdOutRead('netsh int ip delete arpcache & ipconfig/flushdns')
$IP_Address = StringRegExpReplace($IP_Address, "(?i)(?s).*IP.*?(d+.d+.d+.d+).*{:content:}quot;", "1")
MsgBox(64, "Flush DNS", StringFormat("%s", $IP_Address))
EndFunc ;==>FlushPressed

Func TCPIPPressed()
Local $IP_Address = _RunStdOutRead('netsh int ip reset resetlog.txt & netsh winsock reset')
$IP_Address = StringRegExpReplace($IP_Address, "(?i)(?s).*IP.*?(d+.d+.d+.d+).*{:content:}quot;", "1")
MsgBox(64, "Reset TCP/IP & Winsock", StringFormat("%s", $IP_Address))
EndFunc ;==>TCPIPPressed

;Function to read from Command Line process.
Func _RunStdOutRead($sRunCmd)
Local $iPID = Run(@ComSpec & ' /c ' & $sRunCmd, @ScriptDir, @SW_HIDE, 4 + 2)
Local $sStdOutRead = ""

While ProcessExists($iPID)
$sStdOutRead &= StdoutRead($iPID)
WEnd

Return $sStdOutRead
EndFunc ;==>_RunStdOutRead

Func Ping4Pressed()
Local $IP_Address = _RunStdOutRead('ping www.google.com')
$IP_Address = StringRegExpReplace($IP_Address, "(?i)(?s).*IP.*?(d+.d+.d+.d+).*{:content:}quot;", "1")
MsgBox(64, "Ping Out By URL", StringFormat("%s", $IP_Address))
EndFunc ;==>Ping4Pressed

Func InetPressed()
Run("control inetcpl.cpl")
WinWait("Internet Options", "", 5)
EndFunc ;==>InetPressed

Func ServPressed()
Local $File = @SystemDir & "services.msc"
Run(@SystemDir & "mmc.exe " & $File)
EndFunc ;==>ServPressed

Func SpecialEvents()

Select
Case @GUI_CtrlId = $GUI_EVENT_CLOSE
Exit

Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
MsgBox(0, "Window Minimized", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

Case @GUI_CtrlId = $GUI_EVENT_RESTORE
MsgBox(0, "Window Restored", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

EndSelect

EndFunc ;==>SpecialEvents

Func _IPDetails($num = 100)
Local $totalReturn = ""
Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!" & "." & "rootcimv2")
Local $oColItems = $oWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration", "WQL", 0x30), $aReturn[6] = [5]
Local $count = 0
If IsObj($oColItems) Then
For $oObjectItem In $oColItems
If IsString($oObjectItem.IPAddress(0)) Then
$count += 1
;MsgBox(64,"debug",$count & '/' & $num)
If ($count > $num) Then ExitLoop ;limiting the number of adapters to show

If IsString($oObjectItem.Description) Then
$aReturn[1] = "Description:" & @TAB & $oObjectItem.Description
Else
$aReturn[1] = "Description:" & @TAB & "Not Available"
EndIf

If IsString($oObjectItem.IPAddress(0)) Then
$aReturn[2] = "IP Address:" & @TAB & $oObjectItem.IPAddress(0)
Else
$aReturn[2] = "IP Address:" & @TAB & "Not Available"
EndIf

If IsString($oObjectItem.DefaultIPGateway(0)) Then
$aReturn[3] = "Default Gateway:" & @TAB & $oObjectItem.DefaultIPGateway(0)
Else
$aReturn[3] = "Default Gateway:" & @TAB & "Not Available"
EndIf

If IsArray($oObjectItem.DNSServerSearchOrder()) Then
$aReturn[4] = "DNS Servers:" & @TAB & _WMIArrayToString($oObjectItem.DNSServerSearchOrder(), " - ")
Else
$aReturn[4] = "DNS Servers:" & @TAB & "Not Available"
EndIf

If IsString($oObjectItem.MACAddress) Then
$aReturn[5] = "MAC: " & @TAB & @TAB & $oObjectItem.MACAddress
Else
$aReturn[5] = "MAC: " & @TAB & @TAB & "Not Available"
EndIf

$totalReturn &= $aReturn[1] & @CRLF & $aReturn[2] & @CRLF & $aReturn[3] & @CRLF & $aReturn[4] & @CRLF & $aReturn[5] & @CRLF & @CRLF
EndIf
Next
Return StringTrimRight($totalReturn, 4)
EndIf
Return SetError(1, 0, $aReturn)
EndFunc ;==>_IPDetails

Func _WMIArrayToString($aArray, $sDelimeter = "|")
If IsArray($aArray) = 0 Then
Return SetError(1, 0, "")
EndIf
Local $iUbound = UBound($aArray) - 1, $sString
For $A = 0 To $iUbound
$sString &= $aArray[$A] & $sDelimeter
Next
Return StringTrimRight($sString, StringLen($sDelimeter))
EndFunc ;==>_WMIArrayToString

Hope this helps,

-Mike

P.S. You may want to move this to a new thread if you still need help, and just reference the old thread as your source of inspiration.

Edited by mdwerne
Link to comment
Share on other sites

Nothing wrong with my function I'm afraid.

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

Thank you mdwerne, that did the trick! This was my first attempt at creating a script in autoit.

The objective was to monitor the IP & DNS of a connection and log it when it changed, as the dns on my machine was mysteriously changing. Ultimately the culprit was a DHCP server on an old router that we were using as switches. :/

Anyway the script has served it's purpose, I hope somebody enjoys it in the future.

Link to comment
Share on other sites

  • 2 years later...

Dear All,

Dear Guiness,

Thanks for sharing your script, It is working well.

In script I request you to change few more things for my convenient,

is it possible to open the properties window for both   Local area connection and wireless connection by press the button.

(For giving manual IP)

like how your made coding internet option in your script ...

same way it needs to open both wired and wireless card...

and also I need to disable and enabe the Proxy setting in internet exporer.

If any body know the script or idea please share with the me.

Thanks in Advance...

Link to comment
Share on other sites

You expect that we provide you a working code ?
Do you really took the time to search in the forum ? When I look for "ProxyEnable" in the forum search engine, I have 86 results. The 2nd and 3rd links contain the solution to enable/disable the proxy.

Link to comment
Share on other sites

Thanks for sharing your script, It is working well.

In script I request you to change few more things for my convenient,

You're welcome, but the shop is closed for requests. Perhaps pay attention to what jguinch is saying.

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

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