Jump to content

VijayS

Members
  • Posts

    2
  • Joined

  • Last visited

Everything posted by VijayS

  1. 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.
  2. Can you help with this code that I made inspired from this post? The script quits unexpectedly when left clicking or double clicking. If compiled to exe the error is variable used before declaring. But the syntax checker doesn't show any error otherwise. Thanks. #include #include #include #include ; This script requires full Administrative rights #requireadmin #NoTrayIcon 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 CheckChange(false) CheckChange(false) While 1 Sleep(5000) ; Idle loop CheckChange() WEnd Exit ; Functions Func ExitScript() Exit EndFunc ;==>ExitScript Func VerPressed() Run("winver") EndFunc Func IP1Pressed($stitle = "IP Addresses", $log = True) $stmp = _IPDetails(1) TrayTip($stitle,$stmp,5,1) if $log then WriteLog($stitle & @CRLF & $stmp) ;MsgBox(64, "IP Addresses",$stmp) EndFunc 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 Func GetDNS () ;Occasionally returns none!, even when there are valid entries. TOFIX Local $sResult, $line $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) $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 Func WriteLog($Data, $FileName = -1, $TimeStamp = True) If $FileName == -1 Then $FileName = @ScriptDir & '' & @ScriptName & '.Log' $hFile = FileOpen($FileName, 1) If $hFile <> -1 Then If $TimeStamp = True Then $Data = _Now() & ' - ' & $Data FileWriteLine($hFile, $Data) FileClose($hFile) EndIf EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Func ReleasePressed() $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 Func RenewPressed() $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 Func FlushPressed() $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 Func TCPIPPressed() $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 ;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 Func Ping4Pressed() $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 Func InetPressed() Run("control inetcpl.cpl") WinWait("Internet Options", "", 5) EndFunc Func ServPressed() $File = @SystemDir & "services.msc" Run(@SystemDir & "mmc.exe " & $File) EndFunc 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 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
×
×
  • Create New...