Ping multiple workstations(using batch script)

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Ping multiple workstations(using batch script)

Post by MigrationUser »

21 Feb 2008 02:10
corvus2606

Hi all,

I need a script that will ping a list of workstations that are provided in a .txt file.

if the ping is returned i need it to display "[workstation name] online" otherwise "[workstation name] offline"

not really any good with writing batch files, so i need some help here.

Regards,

Bryden

----------------------------

#2 21 Feb 2008 14:32
bluesxman


You're in luck, I wrote something like that a little while ago:

Code: Select all

@echo off

setlocal enabledelayedexpansion

set serverdata=%~dpn0.dat

set wait=500
set sleep=5

set tempfile=%temp%\%~n0.tmp

title Initialising...

echo:Initialising...

set /a sleep+=1

set _previous=
set _current=

:top

set _time=%time%

set _change=0
set _down=0
set _up=0

set _previous=%_current%
set _current=

for /f "usebackq tokens=1* eol=# delims=    " %%a in ("%serverdata%") do (
    set ip=%%~a
    set name=%%~b
    if defined name (set "name=!name: =_!") ELSE (set name=!ip!)
    ping -w %wait% -n 2 !ip! | findstr "Reply.*bytes" >nul
    if !errorlevel! EQU 0 (set /a _up+=1 & set "_state/!ip!/!name!=UP  ") ELSE (set /a _down+=1 & set "_state/!ip!/!name!=DOWN")
    set _current=!_current! "!ip!/!name!"
    call :check !ip! "!name!"
)

if %_change% EQU 0 call :wait & goto :top

cls


echo:[Last status change: %_time:~0,8%]
echo:

set _last=

for /f "usebackq delims=/= tokens=2,3,4" %%a in (`set _state/`) do (
    echo:  %%~a ^(%%~b^) is %%~c (^!_time/%%~a/%%~b!^)
    set _last=%%~c
)

title UP: %_up% / DOWN: %_down%

echo:

call :wait

goto :top

:check 

if "!_state/%~1/%~2!" NEQ "!_last/%~1/%~2!" set /a _change=1 & set _time/%~1/%~2=%_time:~0,8%
if not defined _time/%~1/%~2 set _time/%~1/%~2=%_time:~0,8%

set _last/%~1/%~2=!_state/%~1/%~2!

goto :EOF

:wait

set /a "_sleep=sleep - (( %wait% * %_down% ) / 1000)

if %_sleep% LSS 1 set _sleep=1

ping 127.0.0.1 -n %_sleep% >nul

goto :EOF
The data file should be named the same as the script (and be kept in the same directory), but with ".dat" instead of ".cmd" as the extension. It should also take this format:

Code: Select all

# IP[TAB]Description

10.1.1.11    WebServ11
10.1.1.12    WebServ12
10.1.1.13    WebServ13
10.1.1.14    WebServ14
This script will actually monitor the given IPs constantly ... I.E. it will run until you close it.

You can add servers to the .dat file on-the-fly, but I haven't gotten around to coding to take account for if they are removed from the .dat file.

Increasing the "set wait=" value (it's in milliseconds) will make it more tolerant of "far away" servers.

cmd | *sh | ruby | chef

----------------------------

#3 22 Feb 2008 02:56
corvus2606


Thats a great script,

can you help me with just one more thing though, All i am provided with is Network names and not IP addresses, is there any way to change this code so that it pings the network name instead, and doesn't require an ip in the .dat file?

any help would be great,

Cheers

----------------------------

#4 22 Feb 2008 12:05
bluesxman


Assuming your network DNS can resolve the name you're given, there should be no reason you can't use that instead of the IP address.

cmd | *sh | ruby | chef
Post Reply