wuruoyu

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

7 posts in this topic

#1 ·  Posted (edited)

#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

Share this post


Link to post
Share on other sites



Perhaps show the raw output here, and then explain what you would prefer to see? This obviously works but your are not comfortable with the results? 


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

Hi Skysnake, thanks for your reply. I have updated the original post with raw result. Just want to know if this is the most efficient way.

Share this post


Link to post
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 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
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?

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

Thank Skysnake, I will check out regex, and user Ping as backup when nslookup fails

Share this post


Link to post
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