User751139 Posted August 16, 2013 Share Posted August 16, 2013 Hello everyone I was hoping if someone can possibly spare a few min to add a feature that i'm quite not sure how to include into this script. This script simply pings a list of IP address / Hostnames listed in the "IPList.txt" and sends the output into a text file ("PingLogFile.txt") indicating if the IP is alive/dead. The additional feature I would like to have is when I input the IP/Hostname the result would display BOTH the IP and the host name as well...so It would have to resolve IP to hostname / Hostname to IP to the result file ("PingLogFile.txt") If anyone can help me with this I would highly appreciate it! I have a lot of work and this would definitely make the job easier The code: #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=..\CAN.ico #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <file.au3> Local $aIPList,$count,$ms $count=0 If Not _FileReadToArray("IPList.txt",$aIPList) Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) Exit EndIf For $x = 1 to $aIPList[0] $ms=Ping($aIPList[$x]) If $ms then FileWriteLine ("PingLogFile.txt", $aIPList[$x] & "==> ONLINE " & $ms) $count+=1 EndIf Next FileWriteLine("PingLogFile.txt", @CRLF & "IP's Alive: ==> " & $count & @CRLF & "Dead: ==> " & $aIPList[0]-$count) Msgbox(0,"Scan Complete") Run("notepad.exe PingLogFile.ext") Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted August 16, 2013 Moderators Share Posted August 16, 2013 If you're looking to grab either by host name or IP, maybe try nslookup instead. Something like this: #include <File.au3> Local $aPCs _FileReadToArray(@DesktopDir & "\Machines.txt", $aPCs) For $i = 1 To $aPCs[0] Run(@ComSpec & ' /c nslookup ' & $aPCs[$i] & ' >> C:\Output.txt', "", @SW_HIDE) Sleep(250) Next "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 More sharing options...
mikell Posted August 16, 2013 Share Posted August 16, 2013 Or you can use these funcs from the help file : TCPNameToIP _GetIP _TCPIpToName Link to comment Share on other sites More sharing options...
User751139 Posted August 16, 2013 Author Share Posted August 16, 2013 (edited) Thanks guys, @JLogan3o13 that works perfectly but it's giving me the server address as well which is something I don't want. Also is there anyway to incorporate that into the original script? If not thanks anyways! Also the script is returning too much info for example I only wanted the terminals below: MJTCRN1 MJTCRN0 MJTCVN6 MJTCTM4 PS744-BDP MJTCRR4 MJTCRM6 MJTCRP0 MJTCRR2 MJTCRM8 MJTCRM3 But it's also returning the server address as well which I do not want: expandcollapse popupServer: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCRN1.psource.ad.ca Address: 42.76.49.22 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCRN0.psource.ad.ca Address: 42.76.49.21 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCVN6.psource.ad.ca Address: 42.76.49.24 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCTM4.psource.ad.ca Address: 42.76.49.25 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: PS744-BDP.psource.ad.ca Address: 42.76.49.132 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCRR4.psource.ad.ca Address: 42.76.49.30 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCRM6.psource.ad.ca Address: 42.76.49.31 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCRP0.psource.ad.ca Address: 42.76.49.133 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCRR2.psource.ad.ca Address: 42.76.49.29 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCRM8.psource.ad.ca Address: 42.76.49.23 Server: hocp-dc2.corp.ad.ca Address: 42.120.4.123 Name: MJTCRM3.psource.ad.ca Address: 42.76.49.134 Edited August 16, 2013 by User751139 Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted August 16, 2013 Moderators Share Posted August 16, 2013 If you're looking for whether or not a machine is currently online, that may not be the way to go. NSLookup is going to query DNS, so it'll return the last IP even if the machine is not currently online. How is the data in your text file going to be formatted - always IP, always Hostname, or a mixture? "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 More sharing options...
User751139 Posted August 16, 2013 Author Share Posted August 16, 2013 (edited) The current output is fine, I just don't want the server info listed. If anything having it listed with name and IP would be amazing...this would be ideal but it's not a must ex: inputting either IP or Hostname in the "Machine.txt" should result to displaying both IP / Hostname..Just not the server info Name: MJTCRN1.psource.ad.ca Address: 42.76.49.22 Edited August 16, 2013 by User751139 Link to comment Share on other sites More sharing options...
Solution Gianni Posted August 16, 2013 Solution Share Posted August 16, 2013 (edited) hi User751139I added some lines (marked with <--- <) to your listing in order to obtain your goal. you have to format the output to the file as you like using the variables in lines 25 and 26. expandcollapse popup#region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=..\CAN.ico #endregion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <file.au3> #include <iNet.au3> ; <---< Local $aIPList, $count, $ms $count = 0 If Not _FileReadToArray("IPList.txt", $aIPList) Then MsgBox(4096, "Error", " Error reading log to Array error:" & @error) Exit EndIf For $x = 1 To $aIPList[0] $ms = Ping($aIPList[$x]) If $ms Then $Names = Device_Names($aIPList[$x]) ; <---< $Names[0]= IPAddress ; $Names[1] = Hostname.domain $Hname = StringSplit($Names[1], ".") ; <---< $Hname[1] = hostnameOnly ; Format the FileWriteLine as you like using above variables ; <---< FileWriteLine("PingLogFile.txt", $aIPList[$x] & "==> ONLINE " & $ms) $count += 1 EndIf Next FileWriteLine("PingLogFile.txt", @CRLF & "IP's Alive: ==> " & $count & @CRLF & "Dead: ==> " & $aIPList[0] - $count) MsgBox(0, "Scan Complete") Run("notepad.exe PingLogFile.ext") ; This Function accepts either an IPaddress or a Hostname and returns ; a 2 elements array element[0]=IPaddress element[1]=hostname.domain Func Device_Names($Net_device) ; <---< Local $Net_Names[2] TCPStartup() If StringRegExp($Net_device, "^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$") Then $Net_Names[1] = _TCPIpToName($Net_device) ; Resolves IP adress to Hostname If @error Then $Net_Names[1] = "Hostname error" $Net_Names[0] = $Net_device Else $Net_Names[0] = TCPNameToIP($Net_device) ; Resolves Hostname to IP adress If @error Then $Net_Names[0] = "IP error" $Net_Names[1] = $Net_device EndIf TCPShutdown() Return $Net_Names EndFunc ;==>Device_Names hope this can do. (and hope it works also...) bye Edited August 16, 2013 by Pincopanco Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
User751139 Posted May 21, 2014 Author Share Posted May 21, 2014 (edited) hi User751139 I added some lines (marked with <--- <) to your listing in order to obtain your goal. you have to format the output to the file as you like using the variables in lines 25 and 26. expandcollapse popup#region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=..\CAN.ico #endregion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <file.au3> #include <iNet.au3> ; <---< Local $aIPList, $count, $ms $count = 0 If Not _FileReadToArray("IPList.txt", $aIPList) Then MsgBox(4096, "Error", " Error reading log to Array error:" & @error) Exit EndIf For $x = 1 To $aIPList[0] $ms = Ping($aIPList[$x]) If $ms Then $Names = Device_Names($aIPList[$x]) ; <---< $Names[0]= IPAddress ; $Names[1] = Hostname.domain $Hname = StringSplit($Names[1], ".") ; <---< $Hname[1] = hostnameOnly ; Format the FileWriteLine as you like using above variables ; <---< FileWriteLine("PingLogFile.txt", $aIPList[$x] & "==> ONLINE " & $ms) $count += 1 EndIf Next FileWriteLine("PingLogFile.txt", @CRLF & "IP's Alive: ==> " & $count & @CRLF & "Dead: ==> " & $aIPList[0] - $count) MsgBox(0, "Scan Complete") Run("notepad.exe PingLogFile.ext") ; This Function accepts either an IPaddress or a Hostname and returns ; a 2 elements array element[0]=IPaddress element[1]=hostname.domain Func Device_Names($Net_device) ; <---< Local $Net_Names[2] TCPStartup() If StringRegExp($Net_device, "^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$") Then $Net_Names[1] = _TCPIpToName($Net_device) ; Resolves IP adress to Hostname If @error Then $Net_Names[1] = "Hostname error" $Net_Names[0] = $Net_device Else $Net_Names[0] = TCPNameToIP($Net_device) ; Resolves Hostname to IP adress If @error Then $Net_Names[0] = "IP error" $Net_Names[1] = $Net_device EndIf TCPShutdown() Return $Net_Names EndFunc ;==>Device_Names hope this can do. (and hope it works also...) bye I ran into some issues lately and I needed another function added to this script. Basically can you make it so that the output is followed by standard IP structure. For example as the script sits right now, the results are put out in order of host name supplied. so for example: MJTCAX3 42.76.5.13 MJTCAB1 42.76.6.24 MJTCAD5 42.76.8.134 MJTCAB0 42.76.5.78 MJTCTL6 42.76.6.29 MJTCRG4 42.76.8.103 Is it possible to group them into the IP structure so that all the 42.76.5.xx will be grouped together followed by 42.76.6.xx etc... and also include a custom message (Increment store name) so the outcome looks like this: Store 700 (<-- Incremented store name) MJTCAX3 42.76.5.13 MJTCAB0 42.76.5.78 Store 701 (<-- Incremented store name) MJTCAB1 42.76.6.24 MJTCTL6 42.76.6.29 Store 702 (<-- Incremented store name) MJTCAD5 42.76.8.134 MJTCRG4 42.76.8.103 Im sorry if my explanation isn't clear. Please tell me if i need to explain better. Much appreciate the help!! you guys have helped me so much already!! Edited May 21, 2014 by User751139 Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted May 22, 2014 Moderators Share Posted May 22, 2014 I see what you would like to be added to the script, but don't see what you have tried on your own so far? Anything? "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 More sharing options...
Gianni Posted May 22, 2014 Share Posted May 22, 2014 hi User751139 This modifications to the script should work (assuming the subnet mask of all hosts is 255.255.255.0) let know what happens.... if you need clarifications just ask bye expandcollapse popup#region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=..\CAN.ico #endregion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <file.au3> #include <iNet.au3> ; <---< #include <array.au3> ; <==< Local $aIPList, $ms, $count = 0 If Not _FileReadToArray("IPList.txt", $aIPList) Then MsgBox(4096, "Error", " Error reading log to Array error:" & @error) Exit EndIf ;crate an array to hold results of pings ; <==< Local $aResults[1][3] Local $aIP, $aSubMask[4] = [255, 255, 255, 0] For $x = 1 To $aIPList[0] $ms = Ping($aIPList[$x]) If $ms Then $Names = Device_Names($aIPList[$x]) ; <---< $Names[0]= IPAddress ; $Names[1] = Hostname.domain $Hname = StringSplit($Names[1], ".") ; <---< $Hname[1] = hostnameOnly ; Format the FileWriteLine as you like using above variables ; <---< ; FileWriteLine("PingLogFile.txt", $aIPList[$x] & "==> ONLINE " & $ms) $count += 1 ReDim $aResults[$count][3] ; add one element to the array ; <==< $aResults[$count - 1][0] = $Hname[1] ; Hostname $aResults[$count - 1][1] = $Names[0] ; IP address $aIP = StringSplit($Names[0], '.', 2) ; split IP octets $aResults[$count - 1][2] = StringFormat("%03s", BitAND($aIP[0], $aSubMask[0])) & "." & _ StringFormat("%03s", BitAND($aIP[1], $aSubMask[1])) & "." & _ StringFormat("%03s", BitAND($aIP[2], $aSubMask[2])) & "." & _ StringFormat("%03s", BitAND($aIP[3], $aSubMask[3])) ; Subnet address formatted as xxx.xxx.xxx.xxx EndIf Next Local $aSubnets = _ArrayUnique($aResults, 3) _ArrayDelete($aSubnets, 0) ; remove first element to make it zero based For $i = 0 To UBound($aSubnets) - 1 ; loop all subnets $aHosts = _ArrayFindAll($aResults, $aSubnets[$i], 0, 0, 0, 0, 3) ; find all hosts for each subnet ; Output result to console & file ConsoleWrite("host(s) of subnet " & $aSubnets[$i] & " <-- (Increment store name)" & @CRLF) FileWriteLine("PingLogFile.txt", "host(s) of subnet " & $aSubnets[$i] & " <-- (Increment store name)") For $ii = 0 To UBound($aHosts) - 1 ConsoleWrite($aResults[$aHosts[$ii]][0] & @TAB & $aResults[$aHosts[$ii]][1] & @CRLF) FileWriteLine("PingLogFile.txt", $aResults[$aHosts[$ii]][0] & @TAB & $aResults[$aHosts[$ii]][1]) Next ConsoleWrite(@CRLF) FileWriteLine("PingLogFile.txt", @CRLF) Next FileWriteLine("PingLogFile.txt", @CRLF & "IP's Alive: ==> " & $count & @CRLF & "Dead: ==> " & $aIPList[0] - $count) MsgBox(0, "", "Scan Complete") Run("notepad.exe PingLogFile.txt") ; This Function accepts either an IPaddress or a Hostname and returns ; a 2 elements array element[0]=IPaddress element[1]=hostname.domain Func Device_Names($Net_device) ; <---< Local $Net_Names[2] TCPStartup() If StringRegExp($Net_device, "^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$") Then $Net_Names[1] = _TCPIpToName($Net_device) ; Resolves IP adress to Hostname If @error Then $Net_Names[1] = "Hostname error" $Net_Names[0] = $Net_device Else $Net_Names[0] = TCPNameToIP($Net_device) ; Resolves Hostname to IP adress If @error Then $Net_Names[0] = "IP error" $Net_Names[1] = $Net_device EndIf TCPShutdown() Return $Net_Names EndFunc ;==>Device_Names Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now