Reputation: 1
Recursive Ping test failing with "Could not find host" error when passed to a new command prompt instance from batch file
Picking smarter DOS Batch Scripting/Windows brains than mine.
Running into an odd issue with a PC Reboot script that I wrote 7 or 8 years ago to remote reboot a workstation then start a recursive ping test to the machine so that you know whether Windows actually shut down and when Windows has come back on the target box.
The recursive ping had been working fine until a couple of years ago, haven't had time to really dig into the issue until now (may be Win10 specific, not sure)...can't recall if my Win7 box had run into this same issue or not...I do remember it working previous though.
I can ping the machine from standard Command Prompt with no issues, so I know the box is up and running.
@ECHO OFF
SETLOCAL
ECHO/WScript.Echo "SET IN=" ^& InputBox("Enter value:")>_TEMP.VBS
cscript//nologo _TEMP.VBS>_TEMP.BAT
CALL _TEMP.BAT
DEL _TEMP.BAT _TEMP.VBS
ECHO. The input string was: %IN%
GOTO CAPTURE_FQDN
:CAPTURE_FQDN
FOR /F "tokens=2 delims= " %%F IN ('nslookup %IN%^|Find "Name"') DO (SET FQDN=%%F)
Echo %FQDN%
ping %FQDN%
start "" /w "cmd /k ping -t %FQDN%"
The original version of the script used start "" /w "cmd /k ping -t %IN%"
which had been working....however now every attempt to run this results in the "ping request could not find host" error.
After googling to see if anyone else has run into this and finding a post where someone else had found his machine wasn't making the DNS request I tried modifying my script to run nslookup on the machine name to get the FQDN and see if the ping test would work that way.....no dice.
Interestingly the ping %FQDN%
ran from the same cmd instance as the script gets replies....however the start "" /w "cmd /k ping -t %FQDN%"
ping throws the "Could not find host" error....ideas???
Upvotes: 0
Views: 1719
Reputation: 1463
The problem is not related to ping
, It is related to way that you've invoked the new CMD
instance:
"cmd /k ping -t %FQDN%"
You've enclosed the executable name along with its parameters in double quotes.
In typical circumstances where the external command name is anything other than cmd
, It means: find and invoke an external command with the name which is enclosed in the quotes.
For instance trying to execute start "" /w "ping example.net"
, throws an error indicating that the command or executable '"ping example.net"'
can not be found.
Because it tries to find and execute "ping example.net.exe
" or "ping example.net.bat
" or "ping example.net.vbs"
or ... (The extensions are determined by the PATHEXT
environment variable).
But you didn't get that error message, Instead you've got a complain from the ping
command about the host name.
This is because the command interpreter(CMD.EXE) treats "cmd "
as special and acts differently, which leads to the outcome that you've observed.
When the command interpreter sees "cmd "
it will invoke the executable which is referenced by the COMSPEC
environment variable (which typically is C:\Windows\System32\cmd.exe
) and passes the whole quoted command token to the new cmd
instance as the command line argument.
But when using the start
command in this combination (start "" "cmd ..."
), an extra SPACE will be appended to the command line. This extra SPACE is the source of error as it is explained below:
This is what happens with start "" /w "cmd /k ping -t example.net"
:
It sees "cmd ..."
as the command token so it invokes
C:\Windows\system32\cmd.exe "cmd /k ping -t example.net"
SPACE
The second instance of cmd
starts and sees "cmd /k ping -t example.com"
SPACE as its command line argument
The second instance splits the command line at /K
(or at /C
):"cmd
and /k ping -t example.net"
SPACE
It ignores the "cmd
because it is not a valid command line argument
It executes ping -t example.net"
SPACE because it is after /K
The C Runtime
component of the ping
command is smart enough to remove the double
quote("
) but the SPACE will become troublesome
Now the ping
command tries to resolve example.net
SPACE as the host name.
Clearly, example.net"
SPACE is not what you wanted to resolve and ping, also it is not a valid host name hence the error message.
Putting the problem of extra SPACE aside, It is better not to rely on the special interpretation of "cmd ..."
and instead use the standard method of invoking the external commands. So any of the following should be used instead:
start "" /w cmd /k ping -t %FQDN%
Or
start "" /w "%COMSPEC%" /k ping -t %FQDN%
Or
start "" /w "CMD " /k ping -t %FQDN%
Upvotes: 2
Reputation: 1
@sst Thanks for the input....I tried start "" /w cmd /k ping -t %FQDN%
however that resulted in a Windows error, Start is not a known Internal/External command. so I tried start "" /w "CMD " /k ping -t %FQDN%
Could not find CMD error....so tried a variant of this: start "" /w "cmd " /k "ping -t %FQDN%"
this worked.
Upvotes: 0