Ping with timestamp and log - batch-file

Guys im using this script in a batch file to perform continuous ping with timestamp and to be recorded in a log file, it is working fine on my computer ( windows 8 64 bit ) but when i try to use it on a windows 7 machine also 64 bit, whenever i run the batch i keep getting that:
Could not find C:\user\administrator\pinglog.txt
knowing that i have created the file pinglog.txt but i really cant figure out the problem.
#echo off
del pinglog.txt
for /f "tokens=*" %%A in ('ping localhost -n 1 ') do (echo %%A>>pinglog.txt && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping localhost -n 1 ') do (echo %date% %time% %%A>>pinglog.txt && GOTO Ping)
i appreciate any help.
Thank you in advance

Over PowerShell you can use this CLI.
ping.exe -t 10.227.23.241 |Foreach{"{0} - {1}" -f (Get-Date),$_} >> Ping_IP.txt

Could not find C:\user\administrator\pinglog.txt means that the file was not found. And the reason for this is really simple. The folder is called users and not user. So the correct path is C:\users\administrator\pinglog.txt.

Related

Ping all IPs in a network and store output in a text file

I posted this a few days ago, but I am still having some issues. Please help if possible. Here is my code
#echo off
rem This script pings all IPAdresses on an Xfinity Router.
::v1.2 - BTE - 01Mar19
::
for /L %%i in (1,1,254) DO ping -n 1 10.0.0.%%i | findstr "ms" && (echo 10.0.0.%%i)>>"pingable_ips.txt"
The above was a batch file that I tried to create to ping all IP addresses on my network, then write all pingable IPs to a text file. After some troubleshooting from others here, I got it to at least ping all IPs on the network. I am still having trouble with getting the batch file to create a text file with the pingable IPs.
This batch file runs all the way from 10.0.0.1 to 10.0.0.254, but it stops after.
EDIT: Thank You, I got it to work!
While I don't know what went wrong with your code, I would suggest the following one which shouldn't produce any errors/misbehaviours except if this is your fault:
#echo off
rem This script pings all IPAdresses on an Xfinity Router.
rem v1.2 - BTE - 01Mar19
for /L %%A IN (1 1 254) do (
(ping -n 1 10.0.0.%%A | findstr "ms") && (
echo 10.0.0.%%A>>"pingable_ips.txt"
) || (
echo Failed to find string "ms" when pinging 10.0.0.%%A!
)
)
Or in one line:
#echo off
rem This script pings all IPAdresses on an Xfinity Router.
rem v1.2 - BTE - 01Mar19
for /L %%A IN (1 1 254) do (ping -n 1 10.0.0.%%A | findstr "ms") && (echo 10.0.0.%%A>>"pingable_ips.txt") || (echo Failed to find string "ms" when pinging 10.0.0.%%A!)
Actually, my code checks if command ping -n 1 10.0.0.%%A | findstr "ms" returns errorlevel EQUal to 0. If yes, then it runs command echo 10.0.0.%%A>>"pingable_ips.txt". If no, it will run echo Failed to find string "ms" when pinging 10.0.0.%%A!.

Extract text from text file, produces error Batch file

Recently I posted something that extracts text from a text file (Here), now what I am trying to do is extract everything from [ to ] in my script which pings any server but it just keeps returning ]= instead of the Ip address. I'm tearing my hair out trying to figure out why... anyone know?
echo Enter Minecraft Server Address:
set /p ad=">"
ping "%ad%" /n 1 >test.txt
Set/P var=<test.txt
Set var=%var:]=&:%
Set var=%var:*[=%
Echo=%var%
(Returns ]=)
thanks for your help!
You probably don't need to use the same method:
#Echo Off
Set/P "ad=Enter Minecraft Server Address > "
For /F "Tokens=2 Delims=[]" %%A In ('Ping -n 1 "%ad%"') Do Set "var=%%A"
Echo=%var%
Timeout -1

Automatically open the browser after losing the connection to a specific IP (Batch File)

Actually I am very new to batch and this forum, so this is my first question here.
My goal is to create a batch-program which keeps checking if a specific IP is reachable.
As soon as it stops to be reachable / loses connection, it should open a browser and a specific website on it.
Could maybe someone help me with it? Here is, what i created so far, but it does not work very well:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=127.0.0.1
:loop
set state=down
for /f "tokens=8,10" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%a"=="xLost" if "x%%b"=="x1," set state=up
(cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe http://www.google.com
)
)
echo.Link is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
endlocal
unsure, what "does not work very well" means. But I suggest another way, that works independend of locale Settings and uses the default browser:
set ipaddr=www.stackoverflow.com
:Loop
ping -n 6 127.0.0.1 >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul && goto Loop
echo Connection lost
REM start the site in the Default browser:
start "" http://www.google.com

Batch to pattern matching in a loop

I am trying to loop through a file which contains few names and search the same with two patterns in another file and echo some statements.
Like I have two files :
1.Installers.txt
2.Progress.txt
Installers.txt
abc.jar
def.jar
tef.jar
....
....
Progress.txt
abc.jar deployment started
abc.jar deployed successfully
def.jar deployment started
So my requirement is to read the Installers.txt file one line at a time and search for the 2 patterns "abc.jar deployment started" and "abc.jar deployed successfully" and report successful or else if both patterns are yet to be found to show as still in progress.
I have tried writing below but its failing at many things while doing pattern and the logic also does not look good. can someone help here.
for /F "usebackq delims=" %%a in ("Installer.txt") do (
set /A i+=1
call echo installing %%i%% : %%a
:NOTVALID
findstr /I "%%k\ in\ progress" %1%\progress.txt
If errorlevel 1 (
echo "installation still in progress.."
PING 127.0.0.1 -n 1 >NUL 2>&1 || PING ::1 -n 1 >NUL 2>&1
goto NOTVALID
) else (
set /A i+=1
echo "installation completed.."
call set array[%%i%%]=%%a
call set n=%%i%%
)
Try the below code which suite your requirement :
cls
#echo off
for /f %%g in (Installers.txt) do type Progress.txt|findstr "%%g" || echo %%g is Yet to start , still in progress
Above code will read a single line from installer and then search for that string in file Progress.txt and then print the output if it is found or not.

How do I skip a computer in a batch file that is unreachable?

#echo off
:begin
for /f %%a in (computerlist.txt) do (
setlocal
psexec \\%%a -u user -p password -i -d "d:\path\command.exe"
endlocal
)
when the script is running, when it finds a machine to be unreachable I want it to skip it.
How do i write the script to skip an unreachable computer and continue to the next one in the txt file?
How about a simple test to see whether a machine is pingable?
#echo off
setlocal
:begin
for /f %%a in (computerlist.txt) do (
ping -n 1 %%a >NUL 2>NUL
if %errorlevel%==0 (
psexec \\%%a -u user -p password -i -d "d:\path\command.exe"
) else echo Skipping unreachable host %%a
)
endlocal
Actually, windows ping.exe doesn't return meaningful errorlevels, (huh?)
It ALWAYS returns a "0" errorlevel, unless the IP-Protocol stack itself is hosed.
WHAT YOU HAVE TO DO IS: pipe the output of your ping to a txt-file, and "find" for TTL=
ie
ping 10.10.10.10. >pingtest.txt
findstr /C:"TTL=" pingtest.txt >nul
if %ERRORLEVEL% equ 0 (do whatever) else (echo skipping unreachable host "whatever")
Then use Find.exe or Findstr.exe to look for TTL= in your output file
(note find, and findstr both use "0" errorlevel when they "find" what your searched on)
1. Ping can fail for a whole lot of reasons, but "TTL=" is always part of a successful ping
2. I always ping at least -n 3 times, because occasionally the first one or two might have problems, and I want to give it more possible chances to succeed before I skip it.
3. For this to work, you have to use the FOR loop method above, just raw psexec.exe has not current means to test/skip targets called from a text file.
4. If you need to use network resource in your psexec session then you need to run the "-h" option so that you get the elevated token, which allows you to map drives
I HOPE THIS HELPS !

Resources