Post by Gene E. BlochBTW, the OP's problem is a bit ambiguous to me. He gave no indication of
where the IP addresses were coming from, and I was left wondering why he
didn't just manually remove the last octet from the one IP address he
was looking at...
"Octet".... now I know a new word... -)
The long story is that I'm having trouble with some IP cameras, but the
first zinger is that only 3 cams are giving trouble and one is working
a-ok....
When I do a Ping -t against the 3 problem children, it looks as if they
are re-booting over-and-over again: a bunch of Replys, then a few
"Request timed out.", then a few "Destination host unreachable"....
repeated over-and-over.
The second zinger is that, every so often, one of the cams becomes
reachable long enough for the camera server to get a short clip.
So I wanted to make a .BAT file to ping the cams with -t and log the
time-stamped results so I could see if anything informative was revealed
around the time of a clip.
First thing, I wanted to be able to invoke the .BAT file just by typing
"PingLog 145" to start the process for the camera with IP address
10.0.0.145.
Then I figured "Let's make this thing portable", so I extracted the IP
address of the machine running the .BAT file with the intent of
stripping off "The last octet".... so the .BAT file would run on a LAN
where the addresses were, for instance, in the vein of 192.168.0.123
instead of 10.0.0.123.
That's where my question came from.
Here's the .BAT file as it is now... still needs work, but it's doing
the job I want it to do - with the caveat that I'm coming from VB and
doing this mostly by rote with little-or-no understanding.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
:* PURPOSE: To create a log of timestamped pings
:* ACCEPTS: Either the last 3 numerics of the device
:* address OR the entire address tb pinged
:* e.g. for 10.0.0.151, you would supply 151
:* For PeteCress.DynDNS.org you would
:* supply PeteCress.DynDNS.org.
:* EMBEDDED: - "myTargetDir": Target directory for output file
:*
:* NOTES: - This does NOT work under XP. Only 7
:* (and maybe 8) The ping part is what
:* does not work.
:*
:* PROBLEMS: - If we run during the midnight date changeover,
:* myCurDate never gets updated
:*
:* REV: .003
ECHO OFF
:* --------------------------------------------------------
:* Set embedded values
SET myTargetDir=D:
:* --------------------------------------------------------
:* Parse the command-line argument and concoct the target
:* IP address. If 3 chars, tack them on to local domain
:* otherwise just use the command-line argument as-is
:* e.g. "151" vs "ExtremeSurfcam.DynDNS.org"
SET myCommandLineParm=%1
CALL :stringLenCompute myStringLen myCommandLineParm
IF %myStringLen% gtr 3 GOTO :useEntireParm
:* ---------------------------------------------------
:* Get local IP addres, strip off the suffix, and then
:* concat the command-line argument to it
CALL :getLocalIP myFullIP
CALL :truncateIP %myFUllIP% myTruncatedIP
SET myTargetIP=%myTruncatedIP%.%1
GOTO :concattedParmToDomain
:useEntireParm
SET myTargetIP=%1
:concattedParmToDomain
:* --------------------------------------------------------
:* Capture/set other variables
SET myTargetPath=%myTargetDir%\PingLog.%1.txt
FOR /f "tokens=1-4 delims=/-. " %%i in ('date /t') do (call :setDate %%i
%%j %%k %%l)
SET myCurDate=%YY% %MM%-%DD%
:* --------------------------------------------------------
:* Display variables (for debugging purposes)
ECHO commandLine=%1
ECHO stringLen=%myStringLen%
ECHO myCurDate=%myCurDate%
ECHO myFullIP=%myFullIP%
ECHO myTruncatedIP=%myTruncatedIP%
ECHO myTargetIP=%myTargetIP%
ECHO myTargetDir=%myTargetDir%
ECHO myTargetPath=%myTargetPath%
:* --------------------------------------------------------
:* Delete any existing output file
DEL %myTargetPath%
:* --------------------------------------------------------
:* Do the deed: keep looping the ping command and stamping
:* each reply with date/time
FOR /f "tokens=*" %%A in ('ping %myTargetIP% -n 1 ') DO (ECHO
%%A>>%myTargetPath% && GOTO Ping)
:Ping
FOR /f "tokens=* skip=2" %%A in ('ping %myTargetIP% -n 1 ') DO (ECHO
%myCurDate% %time% %%A>>%myTargetPath% && GOTO Ping)
:* ==========================================================================================
:* Subroutines called from main loop above
:getLocalIP <theLocalIP>
:* --------------------------------------------------------
:* Obtain the local IPv4 address of the computer we are running on
for /f "delims=[] tokens=2" %%a in ('ping %computername% -n 1 -4 ^|
findstr "["') do (set %~1=%%a)
:getLocalIP_xit
EXIT /b
:setDate
:* --------------------------------------------------------
:* Extract month/day/year from command line's Date command
:* so we can time/date stamp our output
:* NB: We *should* to do this with each ping in order to cath
:* the switchover at midnight... but we do not
if "%1:~0,1%" gtr "9" shift
for /f "skip=1 tokens=2-4 delims=(-)" %%m in ('echo,^|date') do (set
%%m=%1&set %%n=%2&set %%o=%3)
EXIT /b
:stringLenCompute <resultVar> <stringVar>
:* --------------------------------------------------------
:* Determine length of a given string
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
exit /b
)
:truncateIP <theFullIP> <theTruncatedIP>
:* --------------------------------------------------------
:* Take a complete IP address (e.g. 123.456.789.321)
:* and lop off the last octet and it's dot, returning
:* only the remainder, without the dot (e.g. 123.456.789)
FOR /F "usebackq delims=. tokens=1-3" %%f in (`echo %~1`) do SET
"%~2=%%f.%%g.%%h"
EXIT /b
:* ====================================== END OF FILE ===============================================
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
--
Pete Cresswell