Jump to content

What is the best way to retrieve FQHN/IP using Ping cmd


Recommended Posts

#include <Constants.au3>

Global $DOS, $Message = ''

$DOS = Run(@ComSpec & " /c Ping -n 1 comp001", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($DOS)
$Message = StdoutRead($DOS)

;FQHN
$delimFqhnLeft = "Pinging "
$delimFqhnRight = " ["
$fqhnRight = StringMid($Message, StringInStr($Message, $delimFqhnLeft) + StringLen($delimFqhnLeft))
$fqhn = StringLeft($fqhnRight, StringInStr($fqhnRight, $delimFqhnRight) - 1)

;IP
$delimIPLeft = " ["
$delimIPRight = "] "
$IPRight = StringMid($Message, StringInStr($Message, $delimIPLeft) + StringLen($delimIPLeft))
$IP = StringLeft($IPRight, StringInStr($IPRight, $delimIPRight) - 1)
MsgBox(0, "FQHN", $fqhn)
MsgBox(0, "IP", $IP)

When querying a domain computer, sometimes nbtstat or TCPNameToIP gives "host not found." while Ping command works. So I decided to use Ping command to check a domain computer online/offline status and also getting its FQHN (fully qualified hostname) and IP address.

e.g.

PC name: comp001

Domain: domain.org

FQHN: comp001.domain.org

Ping -n 1 www.google.ca

Raw result:

Pinging www.google.ca [74.125.28.94] with 32 bytes of data:
Reply from 74.125.28.94: bytes=32 time=19ms TTL=48

Ping statistics for 74.125.28.94:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 19ms, Maximum = 19ms, Average = 19ms

I would like to show result in following format:
IP address: 74.125.28.94
FQHN: www.google.ca

========================

I wonder if the above code is the best way to strip down the Ping output to retrieve FQHN and IP address. 

I really appreciate any help anyone can provide.

Edited by wuruoyu
Link to comment
Share on other sites

Or you could use NSLOOKUP:

#include <Constants.au3>
#include <Array.au3>
Global $iPID, $sComputerName = @ComputerName, $sOutput, $aArray

$iPID = Run(@ComSpec & " /c NSLOOKUP " & $sComputerName, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($iPID) ; Wait until the process has closed using the PID returned by Run.
$sOutput = StdoutRead($iPID) ; Read the Stdout stream of the PID returned by Run.

; Use StringSplit to split the output of StdoutRead to an array. All carriage returns (@CRLF) are stripped and @CRLF (line feed) is used as the delimiter.
$aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "It appears there was an error running NSLOOKKUP.")
ConsoleWrite("FQHN: " & StringStripWS(StringSplit($aArray[4], ":")[2], $STR_STRIPALL)  & @CRLF)
ConsoleWrite("IP: " & StringStripWS(StringSplit($aArray[5], ":")[2], $STR_STRIPALL) & @CRLF)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Regex is your friend :)

StringRegExp() - find the pattern in your raw data

 

I am sure this can be prettier / faster / better, but it works

Your raw output can be parsed with this regex: "\s*([a-z0-9]+\.(\s|[a-z0-9]|\.)+)"

https://regex101.com/

You can simplify the regex to ONLY provide numbers (URI/URL) by using \d - I tested this with

Pinging www.google.ca [74.125.28.94] with 32 bytes of data:
Reply from 74.125.28.94: bytes=32 time=19ms TTL=48

 www.tucows.co.za 
 www.wordpress.com 
www.arin.net
www.easytravel.my.africa


Ping statistics for 74.125.28.94:

it matched on all the ?.?.?.? domain names and URLs.  Using StringStripWS() to strip any extra white space.

Skysnake

Why is the snake in the sky?

Link to comment
Share on other sites

Thanks Water for the magic code. Looks like NSlookup is much direct and cleaner approach. I will still need to do more test with some problematic domain computers(can't lookup using nbtstats/ TCPNameToIP ) and compare the result of ping and nslookup. 

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