Pinging 192.0.2.2 Receives a Response...but shouldn't - windows

I'm using the windows batch script function ping 192.0.2.2 -n 1 -w 10000 > nul on my local machine (Windows 7) in a command prompt to test its functionality. The explanation of the function is here: "How to wait in a batch script?".
I've used 192.0.2.2 because it is considered a reserved IP address, so there should be no response. But instead, my results are inconsistent - sometimes I get a response with the error "Destination net unreachable."
C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000
Pinging 192.0.2.2 with 32 bytes of data:
Reply from 10.10.251.2: Destination net unreachable.
Ping statistics for 192.0.2.2:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000
Pinging 192.0.2.2 with 32 bytes of data:
Request timed out.
Ping statistics for 192.0.2.2:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000
Pinging 192.0.2.2 with 32 bytes of data:
Request timed out.
Ping statistics for 192.0.2.2:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000
Pinging 192.0.2.2 with 32 bytes of data:
Request timed out.
Ping statistics for 192.0.2.2:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000
Pinging 192.0.2.2 with 32 bytes of data:
Reply from 10.10.251.2: Destination net unreachable.
Ping statistics for 192.0.2.2:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
C:\Users\MrF>ping 192.0.2.2 -n 1 -w 10000
Pinging 192.0.2.2 with 32 bytes of data:
Request timed out.
Ping statistics for 192.0.2.2:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
C:\Users\MrF>
Unfortunately, the function only waits 10 seconds when the ping is lost. Pings that receive a response don't wait at all. This has lead to some frustrating lack of functionality in my scripts that require wait commands. How can I fix this? Why is it happening?

That is not a link local or LAN reserved IP range, but it is routable (although no one should ever route to it). See this whois info for full details. A link local 169.254 range IP would give more consistent, reliable results. See this answer for further explanation.
Edit: I see what you mean. Even my previous 169.254 solution is subject to the same inconsistency. Well, really, the biggest benefit to pinging a bogus IP with the -w switch is so you can pause in milliseconds. If you're pausing in whole seconds (such as ten seconds in your example above), just
ping -n 11 localhost >NUL
... where 11 is N+1 seconds to pause. That command will pause for 10 seconds.
Alternatively, you could use the timeout /t 10 /NOBREAK >NUL command if your script will be run on Vista or newer.
If you need fraction-of-a-second pausing, you could always employ VBScript or JScript.
#if (#CodeSection == #Batch) #then
#echo off
setlocal
set /P "=Pausing 1.5 seconds."<NUL
call :sleep 500
set /P "=."<NUL
call :sleep 500
set /P "=. "<NUL
call :sleep 500
echo Done.
goto :EOF
:sleep <milliseconds>
cscript /nologo /e:JScript "%~f0" "%~1"
goto :EOF
#end // end Batch / begin JScript
WSH.Sleep(WSH.Arguments(0) * 1);

Windows has many wait commands, I use waitfor. But there is choice and timeout as well.
Maybe stop searching the internet. Ping is a silly idea. What would happen if all 175,000 computers at my work all started pinging at the same time.
waitfor /t 99999 Fred

Related

Test if computer is available on network

I work on a company which has many Shop. Each shop has his own IP address :
SHOP 1 = 192.168.1.0/24, SHOP 2 = 192.168.2.0/24, SHOP 3 = 192.168.3.0/24, etc...
I have a number of Windows computers (between 1 and 9) on the same subnet (255.255.255.0) on each shop.
Each computer has a static IP address : 1st = 192.168.1.10, 2nd = 192.168.1.20, 3rd = 192.168.1.30, 4th = 192.168.1.40, etc.
The HQ of my company can deploy softs ONLY on the first computer on each shop (licences issue).
Here, we will try to install Xerox drivers on ALL windows computer on all shop.
I made 3 batch script :
The first, launched as the superadmin of the computer, install a new user with admin rights to install drivers
The second, launched as the new user, install Xerox drivers and set as default the printer.
The third, launched as the superadmin of the computer, try to install Xerox drivers on other computers of the same shop :
Loop over my 9 ip address (for /l %%G in (...) do (...)
For each IP address, test if it ping.
Do a ftp transfer between the computer 1 and the IP address (drivers packages + scripts)
PsExec64.exe to run the first script
PsExec64.exe to run the second script
The first and the second script works perfectly, we'll not speak about them.
Where I need help is on the thrid.
I try 3 scripts :
The first :
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
ping -n 1 %subnet%%%G | findstr "TTL="
if %errorlevel%==0 (
echo Ping %subnet%%%G : OK >> log_ping_errorlevel.txt
) else (
echo Ping %subnet%%%G : KO >> log_ping_errorlevel.txt
)
)
Here the log_ping_errorlevel.txt with 2 computers on my subnet :
Ping 192.168.1.20 : OK
Ping 192.168.1.30 : OK
Ping 192.168.1.40 : OK
Ping 192.168.1.50 : OK
Ping 192.168.1.60 : OK
Ping 192.168.1.70 : OK
Ping 192.168.1.80 : OK
Ping 192.168.1.90 : OK`
I tried to surround %errorlevel% and 0 by simple quote but I get the same result.
Here an extract of the output if I run the batch in cmd window :
C:\Temp\Xerox_Install>(
ping -n 1 192.168.1.90 | findstr "TTL="
if '0'=='0' (echo "Ping 192.168.1.90 : OK" 1>>log_ping_errorlevel.txt ) el
se (echo "Ping 192.168.1.90 : KO" 1>>log_ping_errorlevel.txt )
)
It seems that the errorlevel is always = 0 in my script, IDK why.
The second :
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
ping -n 1 %subnet%%%G && set pingtest=ok
if '%ping_test%'=='ok' (
echo Ping %subnet%%%G : OK >> log_ping_errorlevel.txt
) else (
echo Ping %subnet%%%G : KO >> log_ping_errorlevel.txt
)
)
Here the log_ping_set.txt with 2 computers on my subnet :
Ping 192.168.1.20 : OK
Ping 192.168.1.30 : OK
Ping 192.168.1.40 : OK
Ping 192.168.1.50 : OK
Ping 192.168.1.60 : OK
Ping 192.168.1.70 : OK
Ping 192.168.1.80 : OK
Ping 192.168.1.90 : OK`
I think the ping command result is always 0 then the setter pingtest can be executed.
The third :
Because I'm stuck with the ping command, I tried the if exist folder on remote computer :
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
if exist \\%subnet%%%G\C$\Temp\ (
echo Remote folder reachable >> log_folder_reachable.txt
) else (
echo ERROR 404 - Remote folder not found >> log_folder_reachable.txt
)
)
Here the log_folder_reachable.txt with 2 computers on my subnet :
Remote folder reachable
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
It works BUT the if exist timeover is about 160 seconds and I don't want to wait for 160 seconds before going to the next computer...
Can someone help me with any method but not using external tools ?
You should read SO items about delayedexpansion.
if %errorlevel%==0 (
in the first script should be
if not errorlevel 1 (
[obviously, reverse the logical condition and remove the "not"]
if errorlevel 1 means "if errorlevel is 1 or greater than 1" and is interpreted on the run-time value of errorlevel.
Next:
ping -n 1 %subnet%%%G && set pingtest=ok
if '%ping_test%'=='ok' (
should be
set "ping_test="
ping -n 1 %subnet%%%G && set "ping_test=ok"
if defined ping_test (
Notes
you are setting pingtest in your ping command, then you are attempting to test ping_test
Even had the variablename been correct, the purpose for the quotes in an if statement is to combine the contents as a single string. Single-quotes don't cut the mustard, double-quotes are required.
With the code you have, ping_test once set will retain its value for the next loop, you need to clear it before theping.
The replacement code first sets the value of ping_test to nothing. The quotes delimit the variable name and value so that trailing spaces on the line are not included in the value assigned. The ping command then may set the value of ping_test to (ok, but any value other than nothing (ie something) is valid). The if defined command interprets the run-time value of the variable.
The third part - well, getting desperate, but no need to go this far as the changes I've outlined should solve the problem.
In regards to your first set of code where you said you tried to use delayed expansion it should look like this. Tested and works on my network.
#echo off
setlocal enabledelayedexpansion
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
ping -n 1 %subnet%%%G | findstr "TTL="
if !errorlevel!==0 (
echo Ping %subnet%%%G : OK >> log_ping_errorlevel.txt
) else (
echo Ping %subnet%%%G : KO >> log_ping_errorlevel.txt
)

Error when executing ping command

host.txt:
www.google.com
test.sh:
#!/usr/bin/env bash
while IFS=$'\n' read -r line; do
echo $line
echo "#1"
ping -c 1 $line
line2="www.google.com"
echo "#2"
ping -c 1 $line2
done < $hostfile
exit 0
output:
> test.sh
www.google.com
#1
ping: unknown host www.google.com
#2
PING www.google.com (74.125.206.147) 56(84) bytes of data.
64 bytes from wk-in-f147.1e100.net (74.125.206.147): icmp_seq=1 ttl=46 time=22.1 ms
--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 22.111/22.111/22.111/0.000 ms
Could someone tell why first ping failed?
Thanks.
Possibly your txt file contains CRLF line endings
and bash actually tried to ping www.google.com^M name.

Windows command line, how to filter with pipe

I'm trying to filter ping stats in windows command line using pipe. What I want is to get the 'time' of each ping.
"ping -n 1 -w 1000 " + ip
prints
Pinging 216.52.241.254 with 32 bytes of data:
Reply from 216.52.241.254: bytes=32 time=82ms TTL=240
Ping statistics for 216.52.241.254:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 82ms, Maximum = 82ms, Average = 82ms
The output I'm looking for from that would just be 82.
and my attempt with findstr "ping -n 1 -w 1000 " + ip + ' | findstr time=[0-9]*' prints:
Reply from 216.52.241.254: bytes=32 time=83ms TTL=240
The output should just be 83 this time.
I've tried using grep ?("time=")[0-9]*ms but on top of that line probably not being right I don't think windows grep includes conditionals

How to get output of ping command without Ping statistics?

For example, in Windows 7 output of command
ping -n 1 ::1
is following:
Pinging ::1 with 32 bytes of data:
Reply from ::1: time<1ms
Ping statistics for ::1:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
Is it possible to get output without Ping statistics (without the last four lines in this case or without two lines in case if destination host is unreachable)?
Maybe using this command:
ping -n 1 ::1 | find "string to search"
The currently accepted answer would filter out 'Request timed out' errors. Instead, use findstr /B R, which only outputs lines that start with R:
ping -n 1 ::1 | findstr /B R

Data Capture Issue using expect in tcl

I have one doubt in expect. I want to store the result of command in a string variable. Can anyone tell me what is the mistake in my code? It is not working. I have used expect_out(buffer) but its not working.
#!/usr/bin/expect
package require Expect
set Argu1 [lindex $argv 0]
set Argu2 [lindex $argv 1]
if {$argc == 2} {
if {[regexp {^(\d|[1-9]\d|1\d\d|2[0-4]\d|[2][5][0-5])\.(\d|[1-9]\d|1\d\d|2[
0-4]\d|[2][5][0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|[2][5][0-5])\.(\d|[1-9]
\d|1\d\d|2[0-4]\d|[2][5][0-5])$} $Argu1 match ]} {
puts "VALID IP-ADDRESS"
set timeout $Argu2-1
spawn ping $Argu1
expect "Expect"
set string $expect_out(buffer)
puts $string
puts "SUCCESS"
exit
} else {
puts "INVALID IP-ADDRESS"
}
} else {
puts "YOU HAVE TO GIVE TWO ARGUMENTS\n FIRST ARGUMENT SHOULD BE
IP-ADDRESS \n SECOND ARGUMENT SHOULD BE THE NUMBER OF PACKETS
YOU WANT TO SEND"
}
I'm reluctant to answer your question, as you haven't responded to my answer on another question. However, seeing as nobody else has responded, I'll try to help you out.
I don't think you need to use $expect_out(buffer) here. Assuming that a ping returning "0% loss" or "0% packet loss" means the ping was successful, you could try the following (untested). Obviously, you can change the "0%" to whatever you want to signify success.
#!/usr/bin/expect
package require Expect
set Argu1 [lindex $argv 0]
set Argu2 [lindex $argv 1]
if {$argc == 2} {
if {[regexp {^(\d|[1-9]\d|1\d\d|2[0-4]\d|[2][5][0-5])\.(\d|[1-9]\d|1\d\d|2[
0-4]\d|[2][5][0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|[2][5][0-5])\.(\d|[1-9]
\d|1\d\d|2[0-4]\d|[2][5][0-5])$} $Argu1 match ]} {
puts "VALID IP-ADDRESS"
set timeout $Argu2-1
spawn ping $Argu1
expect {
"0%" {puts "The IP address was successfully pinged"}
"default" {puts "Could not ping this IP address"}
}
exit
} else {
puts "INVALID IP-ADDRESS"
}
} else {
puts "YOU HAVE TO GIVE TWO ARGUMENTS\n FIRST ARGUMENT SHOULD BE
IP-ADDRESS \n SECOND ARGUMENT SHOULD BE THE NUMBER OF PACKETS
YOU WANT TO SEND"
}
The reason I suggest "0%" is as follows. If you do a successful ping from a Windows machine you will see something like this (note the "0% loss"):
C:\Documents and Settings\Brian>ping 192.168.1.1
Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Ping statistics for 192.168.1.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 1ms, Average = 0ms
From a Unix machine, it might look like this (note the "0% packet loss") :
[brian#rex 4]$ ping -c 1 192.168.1.87
PING 192.168.1.87 (192.168.1.87) 56(84) bytes of data.
64 bytes from 192.168.1.87: icmp_seq=0 ttl=64 time=42.1 ms
--- 192.168.1.87 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 42.153/42.153/42.153/0.000 ms, pipe 2
You could pick many indicators of a successful ping, I chose the fact that there was a 0% loss of packets (a clear indicator of success). The expect command searches the result of the ping and if it finds "0%", it outputs "The IP address was successfully pinged".

Resources