Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

Script that pings multiple IPs, and then performs an action

- - - - -

  • Please log in to reply
10 replies to this topic

#1
Haris1977

Haris1977
  • Member
  • 5 posts
  • Joined 22-September 14
  • OS:Windows 7 x86
  • Country: Country Flag

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!

 

 

 




How to remove advertisement from MSFN

#2
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,465 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag
Sure it is possible.
Something *like*:
 
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET BaseRAnge=192.168.1
SET Min=100
SET Max=115
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% GTR 0 (
ECHO Successful ping[s] %Counter%
) ELSE (
ECHO No successful pings
)
should do.
 
jaclaz

Edited by jaclaz, 22 September 2014 - 04:42 AM.


#3
Haris1977

Haris1977
  • Member
  • 5 posts
  • Joined 22-September 14
  • OS:Windows 7 x86
  • Country: Country Flag

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, 22 September 2014 - 06:11 AM.


#4
Tripredacus

Tripredacus

    K-Mart-ian Legend

  • Super Moderator
  • 9,841 posts
  • Joined 28-April 06
  • OS:Server 2012
  • Country: Country Flag

Donator

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:


MSFN RULES | GimageX HTA for PE 3-5 | lol probloms
msfn2_zpsc37c7153.jpg

#5
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,465 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

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



#6
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,419 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag

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
Attached File  PingMultiRange.vbs.txt   2.38KB   1 downloads




GunSmokingMan



#7
Haris1977

Haris1977
  • Member
  • 5 posts
  • Joined 22-September 14
  • OS:Windows 7 x86
  • Country: Country Flag

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:)



#8
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,465 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

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[s] %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[s] %Counter%

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

)

 

jaclaz



#9
Haris1977

Haris1977
  • Member
  • 5 posts
  • Joined 22-September 14
  • OS:Windows 7 x86
  • Country: Country Flag

@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[s] %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:(



#10
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,537 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

Try something like this:

@ECHO OFF & SETLOCAL
:LOOP
SET "_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
:LOOP
SET "_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,0
TIMEOUT /T 300 /NOBREAK>NUL
GOTO :LOOP 

</Edit>

 

Hope it helps.


Edited by Yzöwl, 23 September 2014 - 07:03 AM.
See <Edit>


#11
Haris1977

Haris1977
  • Member
  • 5 posts
  • Joined 22-September 14
  • OS:Windows 7 x86
  • Country: Country Flag

Thanks Wise Owl !! That's it !!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users