Jump to content

Script that pings multiple IPs, and then performs an action


Recommended Posts

Greetings

 

I am trying to make a batch file (i guess a *.bat would be ok) but i am not a proggramer. What i want to do is a bat file with 4 steps:

 

1) Ping a range of ip's (192.168.1.100 to 192.168.1.115)

2) If the script finds at least ONE active IP from the specific range (e.g 192.168.1.104) it continues to ping the ip's. Thus mean it would loop all ip's all the time

3) If the script doesn't find ANY active ip then make my server pc to sleep (not restart). I think the cmd command is :

rundll32.exe powrprof.dll,SetSuspendState 0,1,0

4) If pc wakes then restart all steps 1-3

 

My server pc is a win7 ultimate X86 laptop

 

Is that possible?

 

Thanks!

 

 

 

Link to comment
Share on other sites


Sure it is possible.

Something *like*:

 

@ECHO OFFSETLOCAL ENABLEEXTENSIONSSET BaseRAnge=192.168.1SET Min=100SET Max=115SET Counter=0FOR /L %%A IN (%Min%,1,%Max%) DO (ECHO Pinging %BaseRange%.%%APING -n 1 %BaseRange%.%%A >nul 2>&1 &&SET /A Counter+=1)IF %Counter% GTR 0 (ECHO Successful ping[s] %Counter%) ELSE (ECHO No successful pings)
should do.

 

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Thanks jaclaz. But when i run it and the line goes to

 

pinging 192.168.1.115

 

the script stops. It doesnt make my pc to sleep.. :(

 

Same thing happens when an ip is active..Smth is missing here..

Edited by Haris1977
Link to comment
Share on other sites

Use the full post editor to fix your topic title. :thumbdown

 

11. Do not use CAPITALIZATIONS in the topic title or when participating in threads because they will not attract attention - instead it will annoy many of MSFN's members. Using unnecessary font formatting (i.e., bold font, increased font sizes, colored fonts, etc.) on the full body of posts is also discouraged.

 

:hello:

Link to comment
Share on other sites

Sure, what I posted is obviously just an example, in order to give you some ideas in order to help you write your own batch.

The two actions executed are just ECHO commands, you should replace them with the action you want to be executed in one case or the other.

 

I read the:

 

I am trying to make a batch file ...

as "I am trying to make a batch file ...." ;) and NOT as "I am wanting someone to write a batch file for me ..." :whistle:



jaclaz

Link to comment
Share on other sites

Here is a VBS script that does
1:\ Make sure it's run by Cscript
2:\ Ping a range of computer Ip 192.168.1.100 to 192.168.1.115
3:\ Has a Timed 5 minute message box that has Yes No Default time out action
4:\ Will run until user select no
 

'-> Script By Gunsmokingman AKA Jake1Eye'->   This Script And Or Any Code Is The Property Of Gunsmokingman Or'->  Jake1Eye, Except Where Acknowledgement Comments Exists for Code'->  Written By Other Coders.'->   If Any Part Of This Code Is Used In Other Coding Project, There'->  Must Be An Acknowledgement Comments To The Original Coder Must Be '->  Included In Any Other Coding Project. '-> Runtime Objects Dim Act :Set Act = CreateObject("Wscript.Shell") Dim Wmi :Set Wmi = GetObject("winmgmts:\\.\root\cimv2")'-> Runtime Varibles Dim C1, Col, Obj, R'-> Make Sure Cscript.exe Is Used  If InStr(1,WScript.FullName,"cscript",1) Then   PingComputer()  Else   MsgBox _    "Wrong scripting engine selected. This script was ment to be " & _    "run using Cscript.exe. Right click this script and select " & _     "either Cmd Prompt or Cscript.exe.",4128,"Error Wrong Script Engine"  End If'-> Function For Pinging Computer, Looping Until No Is Selected'-> Code Bassed On This MSDN Code'-> URL http://msdn.microsoft.com/en-us/library/aa394595(v=vs.85).aspx   Function PingComputer()    For C1 = 100 To 115      Set Ping = Wmi.ExecQuery( _    "Select * From Win32_PingStatus where Address = '92.168.1."& C1 &"'")     For Each Obj in Ping      If IsNull(Obj.StatusCode) Or Obj.StatusCode<>0 Then'-> Code Here For Offline Action       WScript.Echo "Computer Off Line : " & Obj.Address      Else'-> Code Here For Online Action       Wscript.Echo "Computer On Line  : " & Obj.Address      End If       Next    Next '-> Timed Messagebox, With Yes No And Timeout     R = Act.Popup( _    "Would you like to continue pinging the computers?" & vbCrLf & _    "If nothing is selected in 5 minutes, than the script willl" & vbCrLf & _    "restart pinging the computers",300, "Stop Or Continue",4132)'-> Timeout, Yes, No R Return Action    Select Case R     Case -1'-> Timed Messagebox Timeout     Wscript.Echo vbCrLf &"This Script Has Time Out" & vbCrLf & _     "Restarting pinging computers" & vbCrLf      PingComputer()     Case 6'-> Yes Action     Wscript.Echo vbCrLf & "User selected yes" & vbCrLf &_     "Restarting pinging computers" & vbCrLf      PingComputer()     Case 7'-> User cancel Script     Wscript.Echo vbCrLf & "Ending Script, rerun it later"     WScript.Sleep(3000)     WScript.Quit(0)     End Select   End Function 

Rename PingMultiRange.vbs.txt to PingMultiRange.vbs to make active
PingMultiRange.vbs.txt

Link to comment
Share on other sites

Thanks GunSmokingMan for your help but this isnt exactly what i want . Anyway your help is highly appreciated !

 

I think i ll have to dig into scripting to make it work though..

 

Besides i have made some alternations on my first post, because it wasnt exactly what i wanted..The ip of the server that the script is going to be run is 192.168.1.110

 

So what i want is this (slightly different from my 1rst  post)

 

1) Ping a range of ip's (192.168.1.100 to 192.168.1.115)

2) If the script finds >= 2 IP's replies from the specific range (one is 192.168.1.110 which is my server's ip, so it is always "on") it continues to ping the ip's. Thus mean it would loop all ip's all the time. It would be nice a e.g 5 min pause and then restart pinging

3) If the script finds exactly 1 active ip reply (the 192.168.1.110) then my server will go to sleep (not restart, not hybernate).

4) If pc wakes then restart all steps 1-3

 

I think i can achieve 4 step with the help of a task scheduler.

 

In other words this is a diagramm with what i want

 

ping 192.168.1.100 to 192.168.1.115
if active ip reply=1 then run this command –> rundll32.exe powrprof.dll,SetSuspendState 0,1,0
if active ip reply is >=2 then auto restart the script

 

Anyway thanks:)

Link to comment
Share on other sites

Hmmm,

changing:

IF %Counter% GTR 0 (

 

into

IF %Counter% LEQ 1 (

 

or, reversing the logic,

 IF %Counter% GEQ 2 (

 

 

Doesn't seem to me that much an issue, you can't have the 0 value in any case if the PC where this is running is within the pinged range.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET BaseRAnge=192.168.1
SET Min=100
SET Max=115

:loop
SET Counter=0

FOR /L %%A IN (%Min%,1,%Max%) DO (
ECHO Pinging %BaseRange%.%%A
PING -n 1 %BaseRange%.%%A >nul 2>&1 &&SET /A Counter+=1
)

IF %Counter% LEQ 1 (
ECHO Successful ping %Counter%

rundll32.exe powrprof.dll,SetSuspendState 0,1,0
) ELSE (

 

REM Poorman's WAIT
REM Very roughly 60 second intervals
REM it would be much better to use AT, SCHTASKS or any of the n third party "WAIT" tools
FOR /L %%B in (0,1,4100) DO PING -n 1 127.0.1.1>nul
GOTO :loop

)

 

 

or:

IF %Counter% GEQ 2 (

REM Poorman's WAIT
REM Very roughly 60 second intervals
REM it would be much better to use AT, SCHTASKS or any of the n third party "WAIT" tools
FOR /L %%B in (0,1,4100) DO PING -n 1 127.0.1.1>nul
GOTO :loop

) ELSE (

 

ECHO Successful ping %Counter%

rundll32.exe powrprof.dll,SetSuspendState 0,1,0

)

 

jaclaz

Link to comment
Share on other sites

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET BaseRAnge=192.168.1
SET Min=100
SET Max=115

:loop
SET Counter=0

FOR /L %%A IN (%Min%,1,%Max%) DO (
ECHO Pinging %BaseRange%.%%A
PING -n 1 %BaseRange%.%%A >nul 2>&1 &&SET /A Counter+=1
)

IF %Counter% LEQ 1 (
ECHO Successful ping %Counter%

rundll32.exe powrprof.dll,SetSuspendState 0,1,0
) ELSE (

 

REM Poorman's WAIT
REM Very roughly 60 second intervals
REM it would be much better to use AT, SCHTASKS or any of the n third party "WAIT" tools
FOR /L %%B in (0,1,4100) DO PING -n 1 127.0.1.1>nul
GOTO :loop

)

 

that crushes my crashes my cmd:(

Link to comment
Share on other sites

Try something like this:

@ECHO OFF & SETLOCAL:LOOPSET "_CNT=0"FOR /L %%A IN (100,1,115) DO (	PING -n 2 192.168.1.%%A|FIND "TTL=">NUL && SET/A _CNT+=1)IF %_CNT% NEQ 1 (TIMEOUT /T 300 /NOBREAK>NUL) ELSE (	RUNDLL32 POWRPROF.DLL,SETSUSPENDSTATE 0,1,0)GOTO :LOOP

<Edit>

or perhaps:

@ECHO OFF & SETLOCAL:LOOPSET "_CNT=0"FOR /L %%A IN (100,1,115) DO (	PING -n 2 192.168.1.%%A|FIND "TTL=">NUL && SET/A _CNT+=1)IF %_CNT% EQU 1 RUNDLL32 POWRPROF.DLL,SETSUSPENDSTATE 0,1,0TIMEOUT /T 300 /NOBREAK>NULGOTO :LOOP 

</Edit>

 

Hope it helps.

Edited by Yzöwl
See <Edit>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...