Solved

Ping computer variable by name and by ip in script

Posted on 2014-12-10
17
396 Views
Last Modified: 2015-01-23
Hey Experts!  I'm not sure if this can be done but if it can, I know this is the group that will figure it out.

In the following script, I use the computer name as the %computer% variable and then ping it.  The %computer% variable is logged to a file as is the IP.  The problem that I ran into is that (in rare occasions) the IP actually resolves to a different hostname.  So, I was curious how I could get the script to ping the %computer% and based on the IP given, run a ping -a %ip% next so that I can see what the DNS record is.  Again, not sure if it is possible but I figured I would ask as it would help our trouble-shooting here.  

echo ----[Computer Info]----> %logfile%
echo %Computer% >> %logfile%
ping -n 2 %computer%>> %logfile%
echo. >> %logfile%

Open in new window


Thanks in advance experts.
**UPDATED**  Could nslookup be used?
0
Comment
Question by:samiam41
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 2
  • +1
17 Comments
 
LVL 3

Expert Comment

by:TropicalBound
ID: 40491259
I was going to suggest nslookup:

nslookup %computer%

If you have more than one DNS server you can query each:

nslookup %computer% dnsserver1
nslookup %computer% dnsserver2
....
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40491261
I'm on mobile at moment but do you get the right answer using nslookup?

nslookup %computer%

Also you can do all the redirects in one go if wanted:

(echo whatever
echo whatever
)>logfile.txt

Will look back from pc later if no one else does it first.

Steve
0
 
LVL 9

Author Comment

by:samiam41
ID: 40491273
Thanks for the quick response experts!

Here is the script I'm using:

@echo off
setlocal

set /p "Computer=Please enter computer name: "
if "%Computer%" EQU "" (
  echo No computer name entered, ending.
  exit /b
)

set logfile= \\___\public\FG_Diagnostic\%computer%.log

echo ----[Computer Info]----> %logfile%
echo %Computer% >> %logfile%
ping -n 2 %computer%>> %logfile%
echo. >> %logfile%

Open in new window


What I want to add is an nslookup of the IP that was provided when the computer was pinged.  I don't know how to use the IP provided as a variable so that I can run nslookup %ip%.
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 3

Expert Comment

by:TropicalBound
ID: 40491291
OK.  So, after you ping the computer name and save it in the logfile:
ping -n 2 %computer%>> %logfile%

Ping it again and save it to a variable:
ping -n 2 %computer % >> %computerip%

Then do a ping -a on that variable:
ping -a %computerip% >> %pingresult%
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 40491295
Something like that?
@echo off
setlocal

set /p "Computer=Please enter computer name: "
if "%Computer%" EQU "" (
  echo No computer name entered, ending.
  exit /b
)
set logfile= \\___\public\FG_Diagnostic\%computer%.log

 >"%LogFile%" echo ----[Computer Info]----
>>"%LogFile%" echo %Computer%
>>"%LogFile%" echo ---- forward nslookup of IP '%Computer%':
nslookup.exe %Computer% >>"%LogFile%" 2>&1
set IP=
for /f "tokens=2 delims=[]" %%a in ('ping.exe -n 2 %computer% ^| find.exe /i "Pinging %computer%"') do set IP=%%a
>>"%LogFile%" echo --- Pinging by name '%computer%'
ping.exe -n 2 %computer% >>"%LogFile%"
if "%IP%"=="" (
	>>"%LogFile%" echo Could not determine the IP.
) else (
	>>"%LogFile%" echo ---- reverse nslookup of IP '%IP%':
	nslookup.exe %IP% >>"%LogFile%" 2>&1
	>>"%LogFile%" echo ---- Pinging by IP '%IP%'
	ping.exe -a -n 2 %computer% >>"%LogFile%"
)
echo.

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 40491313
oBdA, hey there.  Here are the results when I run the script you posted above (I did update the set logfile path to include the correct server name)

Please enter computer name: fin7acaswed1
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
0
 
LVL 84

Assisted Solution

by:oBdA
oBdA earned 500 total points
ID: 40491322
Remove the space directly after the "=" in "set logfile=" in line 9, and try again.
0
 
LVL 9

Author Comment

by:samiam41
ID: 40491323
TropicalBound, I put what you posted above into a script but the results aren't logged in the logfile.  This is all I get.


Pinging x [81.131] with 32 bytes of data:
Reply from 81.131: bytes=32 time<1ms TTL=128
Reply from 81.131: bytes=32 time<1ms TTL=128

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


I need to take that IP and run nslookup or ping -a on it.
0
 
LVL 3

Expert Comment

by:TropicalBound
ID: 40491332
OK.  Change %pingresult% to %logfile%
0
 
LVL 9

Author Comment

by:samiam41
ID: 40491335
oBdA, that fixed it!!  It looks great.  

I want to give TropicalBound a chance to fix their suggestion before I close out and award points.
0
 
LVL 9

Author Comment

by:samiam41
ID: 40491352
tropicalbound, here is what I am using for the script to test your suggestion:

@echo off
setlocal

set /p "Computer=Please enter computer name: "
if "%Computer%" EQU "" (
  echo No computer name entered, ending.
  exit /b
)
set logfile=\\__\public\FG_Diagnostic\%computer%.log

ping -n 2 %computer%>> %logfile%

ping -n 2 %computer% >> %computerip%

ping -a %computerip% >> %logfile%

Open in new window


I'm getting this in the logfile


Pinging x 81.131] with 32 bytes of data:
Reply from 81.131: bytes=32 time<1ms TTL=128
Reply from 81.131: bytes=32 time<1ms TTL=128

Ping statistics for 81.131:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
0
 
LVL 3

Expert Comment

by:TropicalBound
ID: 40491361
OK.  As long as you have an acceptable solution from oBdA, no point in continuing.  Correct?
0
 
LVL 9

Author Comment

by:samiam41
ID: 40491368
I would split points if you wanted to continue to find a way to make your suggestion work.  I felt that only fair to give that opportunity since you presented a possible solution.  It's your call on how to proceed.
0
 
LVL 3

Expert Comment

by:TropicalBound
ID: 40491373
Thank you samiam41.  I appreciate that.  Go ahead and give oBdA the points.

Have a great day!

TB
0
 
LVL 9

Author Comment

by:samiam41
ID: 40491378
Can do.  Thanks for contributing and hopefully we get to work together again in the near future!
0
 
LVL 9

Author Closing Comment

by:samiam41
ID: 40491380
Amazing work as always oBdA!  Thank you for your help.
0
 
LVL 9

Author Comment

by:samiam41
ID: 40567140
If anyone wants some points and have a few minutes, please check out my new question.  Thank you.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28602773.html
0

Featured Post

Create Professional Looking Email Signatures

Create "Professional HTML Email Signatures" with ease.
7 Day Money Back Guarantee if not 100% Satisfied.
Affordable - Try it out for 7 Days Totally Risk Free.
Installers provided for over 45 Email clients.
Both Windows & MAC Supported.
Highly Recommended!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question