samiam41
asked on
Ping computer variable by name and by ip in script
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.
Thanks in advance experts.
**UPDATED** Could nslookup be used?
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%
Thanks in advance experts.
**UPDATED** Could nslookup be used?
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
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
ASKER
Thanks for the quick response experts!
Here is the script I'm using:
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%.
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%
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%.
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%
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%
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
OK. Change %pingresult% to %logfile%
ASKER
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.
I want to give TropicalBound a chance to fix their suggestion before I close out and award points.
ASKER
tropicalbound, here is what I am using for the script to test your suggestion:
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
@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%
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
OK. As long as you have an acceptable solution from oBdA, no point in continuing. Correct?
ASKER
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.
Thank you samiam41. I appreciate that. Go ahead and give oBdA the points.
Have a great day!
TB
Have a great day!
TB
ASKER
Can do. Thanks for contributing and hopefully we get to work together again in the near future!
ASKER
Amazing work as always oBdA! Thank you for your help.
ASKER
If anyone wants some points and have a few minutes, please check out my new question. Thank you.
https://www.experts-exchange.com/questions/28602773/insert-code-to-prompt-for-OU-to-audit.html
https://www.experts-exchange.com/questions/28602773/insert-code-to-prompt-for-OU-to-audit.html
nslookup %computer%
If you have more than one DNS server you can query each:
nslookup %computer% dnsserver1
nslookup %computer% dnsserver2
....