Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Ping & Excute Script

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,45873
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Ping & Excute Script
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Ping & Excute Script - 4/15/2007 12:56:55 AM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
Hey guys,

  I have am trying to figure out how to do this.  It's probably a simple task ( I think ) but I could really use your help on this one.
Basically this is what I would like the script to do.


ping 192.168.1.1 if alive then go to the next ip.

If dead then ping google.com if alive then run

this batch file c:\batch\myfile.bat (and wait 60 seconds)

if google.com is dead then don't do anything and go to the next ip.


next ip 192.168.2.1  and do the same thing
as the top.


I don't know how hard or simple this is but any comments, suggestions, help would be apprciated.


Thanks
ScarEye

< Message edited by Sc4rEye -- 4/15/2007 12:59:40 AM >
 
 
Post #: 1
 
 RE: Ping & Excute Script - 4/15/2007 3:37:40 AM   
  dm_4ever


Posts: 2637
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
What have you tried so far?

I posted some ping related stuff here: http://www.visualbasicscript.com/m_42535/tm.htm
If you search this forum for "ping" you will find plenty examples.

For running you .bat you may want to search this forum for WScript.Shell and .Run

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sc4rEye)
 
 
Post #: 2
 
 RE: Ping & Excute Script - 4/15/2007 12:13:42 PM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
dm_4ever,

   I saw the thread you posted.  And I saw the NON-WMI version is what I need.  Here is my problem


If Reachable("10.50.138.48") Then
WScript.Echo "Computer is Reachable!"   (I need to change WScript.Echo to, go to the next ping cycle if the ping is successful)
Else
WScript.Echo "Computer is Unreachable!"   (Else here, would be ping google.com is successful execute myfile.bat if ping fails here to go to next IP to ping)
End If
Function Reachable(strComputer)
'     On Error Resume Next
Dim objShell, objExec, strCmd, strTemp

strCmd = "ping -n 1 " & strComputer

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strCmd)
strTemp = UCase(objExec.StdOut.ReadAll)

If InStr(strTemp, "REPLY FROM") Then
   Reachable = True
Else
   Reachable = False
End If
End Function



But I can't figure out the syntax

Again, any comments suggestions or help is greatly appreciated. 


Thanks
ScarEye

(in reply to dm_4ever)
 
 
Post #: 3
 
 RE: Ping & Excute Script - 4/15/2007 12:30:28 PM   
  dm_4ever


Posts: 2637
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
How are you getting/defining what ip addr's will be ping'd?

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sc4rEye)
 
 
Post #: 4
 
 RE: Ping & Excute Script - 4/15/2007 12:54:01 PM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
It can be hard coded in this script 

These are the IP's

192.168.1.1 (if alive to go the next IP (192.168.2.1), If failed then ping google.com, if google.com is alive then run c:\myfile.bat if google.com is dead then go to the next IP.
192.168.2.1
192.168.3.1
192.168.4.1

so on and so fourth. 



What is going on is the batch file (when executed) is told to connect to a router and restart a IPSec connections. 



So if this VBScript can ping 192.168.1.1 then the VPN tunnel is alive and move on to the next IP. 

If the ping has failed, ping google.com (this is to make sure the internet connection is up before executing the script) If google.com is alive then run c:\myfile.bat
If google.com is dead then move on the the next IP. 

Basically if 192.168.1.1 is dead and so is google.com  then most likley the internet connections is down and there is no need to do anything.

I hope this wasn't confusing. 

Again, I appreciate your help and taking the time out to help me.  I truly appreciate it.


Thanks
ScarEye





(in reply to dm_4ever)
 
 
Post #: 5
 
 RE: Ping & Excute Script - 4/15/2007 1:25:03 PM   
  dm_4ever


Posts: 2637
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Dim arrIPAddr : arrIPAddr = Array("192.168.1.1", "192.168.2.1", "192.168.3.1", _
                                                       "192.168.4.1", "192.168.5.1", "192.168.6.1")             
Dim strIP
For Each strIP In arrIPAddr
   If Not Reachable(strIP) Then
       If Reachable("www.google.com") Then
           WScript.Echo "google was reachable...call bat"
       End If
   End If
Next

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sc4rEye)
 
 
Post #: 6
 
 RE: Ping & Excute Script - 4/15/2007 1:39:22 PM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
dm_4ever,


Thanks for your quick reply.  I ran the script.  And I get this message.

Line:5
Char:4
Error: Type mismatch: 'Reachable'


It is spelled right, Looking at the code it looks right (not that I am an expert or anyting) but it looks good.

Any ideas ?

Thanks
ScarEye

(in reply to dm_4ever)
 
 
Post #: 7
 
 RE: Ping & Excute Script - 4/15/2007 1:51:21 PM   
  dm_4ever


Posts: 2637
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Did you include the function that goes along with it? 

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sc4rEye)
 
 
Post #: 8
 
 RE: Ping & Excute Script - 4/15/2007 2:16:56 PM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
Oh, that would do it.  I have no clue where to add what. I was just reading your code
and trying to learn from it.

Dim arrIPAddr : arrIPAddr = Array("192.168.1.1", "192.168.2.1", "192.168.3.1", _
                                                      "192.168.4.1", "192.168.5.1", "192.168.6.1")             
Dim strIP
For Each strIP In arrIPAddr
 If Not Reachable(strIP) Then
     If Reachable("www.google.com") Then
         WScript.Echo "google was reachable...call bat"
     End If
 End If
Next





I tried this.......

Dim arrIPAddr : arrIPAddr = Array("192.168.1.1", "192.168.2.1", "192.168.3.1", _
                                "192.168.4.1", "192.168.5.1", "192.168.6.1")             
Dim strIP
For Each strIP In arrIPAddr
  If Reachable("arrIpAddr") Then
  If Not Reachable(strIP) Then
  If Reachable("www.google.com") Then
          WScript.Echo "google was reachable...call bat"
      End If
  End If
Finish


But still no good.  Again, I am learning, Sorry for being a total newb.  Any guidance is getting this setup is appreciated.


Thanks
ScarEye

(in reply to dm_4ever)
 
 
Post #: 9
 
 RE: Ping & Excute Script - 4/15/2007 2:32:06 PM   
  dm_4ever


Posts: 2637
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Look at your second post...you'll see there was a function there.... should look like this


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sc4rEye)
 
 
Post #: 10
 
 RE: Ping & Excute Script - 4/15/2007 3:08:51 PM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
Okay, I am acutally learning something....  


Dim arrIPAddr : arrIPAddr = Array("192.168.1.1", "192.168.2.1", "192.168.3.1", _
                                                      "192.168.4.1", "192.168.5.1", "192.168.6.1", "192.168.80.1")  <--- I added this IP and it's dead on purpose (192.168.80.1)  THEN......      
Dim strIP
For Each strIP In arrIPAddr
  If Not Reachable(strIP) Then
      If Reachable("www.google.com") Then
          WScript.Echo "google was reachable...call bat" <----  This line is executed.  However, I would like this line to just execute c:\myfile.bat and move on to the next IP.
      End If
  End If
Next
Function Reachable(strComputer)
'     On Error Resume Next
 
  Dim objShell, objExec, strCmd, strTemp
 
  strCmd = "ping -n 1 " & strComputer
 
  Set objShell = CreateObject("WScript.Shell")
  Set objExec = objShell.Exec(strCmd)
  strTemp = UCase(objExec.StdOut.ReadAll)
 
  If InStr(strTemp, "REPLY FROM") Then
     Reachable = True
  Else
     Reachable = False
  End If
End Function



I tried Wscript.execute,  But that doesn't work because it doesn't know what Wscript.execute is.   So how would I tell the script,   HEY! run my batch file now and move on ?


Thanks  dm_4ever   

(in reply to dm_4ever)
 
 
Post #: 11
 
 RE: Ping & Excute Script - 4/15/2007 3:22:17 PM   
  dm_4ever


Posts: 2637
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
To run you batch file look at my first reply.
Also: http://msdn2.microsoft.com/en-us/library/d5fk67ky.aspx

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sc4rEye)
 
 
Post #: 12
 
 RE: Ping & Excute Script - 4/15/2007 6:55:14 PM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
Okay I have done some searching on this forum and also looked at microsoft's MSDN.  But I am having
problems, I guess I can't learn everyhing overnight

Here is my code that I have been playing with but don't know what I am doing wrong. 

I do get this error.

Line:7
Char: 8
Object required:'oShell'


Again any help is appreciated.


Thanks
ScarEye



Dim arrIPAddr : arrIPAddr = Array("192.168.1.1", "192.168.2.1", "192.168.3.1", _
                               "192.168.83.1")            
Dim strIP
For Each strIP In arrIPAddr
If Not Reachable(strIP) Then
     If Reachable("www.google.com") Then
     oShell.run
     End If
End If
Next
Function Reachable(strComputer)
'     On Error Resume Next

Dim objShell, objExec, strCmd, strTemp, oShell

strCmd = "ping -n 1 " & strComputer
Set objShell = CreateObject("WScript.Shell")
Set oShell = WScript.CreateObject ("WSCript.shell")
Set objExec = objShell.Exec(strCmd)
oShell.run "c:\plink.bat"
strTemp = UCase(objExec.StdOut.ReadAll)

If InStr(strTemp, "REPLY FROM") Then
    Reachable = True
Else
    Reachable = False
End If
End Function

< Message edited by Sc4rEye -- 4/15/2007 6:56:45 PM >

(in reply to dm_4ever)
 
 
Post #: 13
 
 RE: Ping & Excute Script - 4/16/2007 12:49:43 AM   
  dm_4ever


Posts: 2637
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Dim oShell : Set oShell = WScript.CreateObject ("WSCript.shell")
Dim arrIPAddr : arrIPAddr = Array("192.168.1.1", "192.168.2.1", "192.168.3.1", _
                                                      "192.168.4.1", "192.168.5.1", "192.168.6.1")             
Dim strIP
For Each strIP In arrIPAddr
  If Not Reachable(strIP) Then
      If Reachable("www.google.com") Then
          oShell.Run "c:\somefile.bat"
      End If
  End If
Next


Function Reachable(strComputer)
'     On Error Resume Next
 
  Dim objShell, objExec, strCmd, strTemp
 
  strCmd = "ping -n 1 " & strComputer
 
  Set objShell = CreateObject("WScript.Shell")
  Set objExec = objShell.Exec(strCmd)
  strTemp = UCase(objExec.StdOut.ReadAll)
 
  If InStr(strTemp, "REPLY FROM") Then
     Reachable = True
  Else
     Reachable = False
  End If
End Function 

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sc4rEye)
 
 
Post #: 14
 
 RE: Ping & Excute Script - 4/16/2007 4:30:58 AM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
dm_4ever,

Thank you very much, I went to sleep 10 in the morning just trying to finish up all my projects.  But from your examples
this is making more and more sense to me.  One last thing (maybe lol), this script is going to be running on a server how can
I set this up so that the little poping up DOS windows don't show up ?


Thanks again.

ScarEye


P.S. If your ever in NY, I owe you a few beers  =) 

< Message edited by Sc4rEye -- 4/16/2007 4:36:08 AM >

(in reply to dm_4ever)
 
 
Post #: 15
 
 RE: Ping & Excute Script - 4/16/2007 5:08:43 AM   
  dm_4ever


Posts: 2637
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
oShell.Run "c:\somefile.bat", 0



_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sc4rEye)
 
 
Post #: 16
 
 RE: Ping & Excute Script - 4/16/2007 5:44:31 AM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
haha,  that one I knew from the MSDN link you sent me. 


The boxes I am trying to stop from poping up are these:

Dim arrIP                      Addr : arrIPAddr = Array("192.168.1.1", "192.168.2.1", "192.168.3.1",

I have tried this, but didn't work.

Dim arrIP                      Addr : arrIPAddr = Array("192.168.1.1",0 "192.168.2.1",0 "192.168.3.1",0



Thanks
ScarEye






(in reply to dm_4ever)
 
 
Post #: 17
 
 RE: Ping & Excute Script - 4/16/2007 5:49:30 AM   
  Country73


Posts: 732
Score: 10
Joined: 8/25/2004
From: USA
Status: offline
You need to run the script in CSCRIPT mode.
I'm sure you can find several examples on how to force your script to run in that mode, but this is what I like to use.


IF INSTR(1, Wscript.FullName, "CScript", VBTEXTCOMPARE) = 0 THEN
   oShell.Run "cscript """ & Wscript.ScriptFullName & """", 0, FALSE
   Wscript.Quit
END IF


This needs to go up towards the very top of your script right after:
Dim oShell : Set oShell = WScript.CreateObject ("WSCript.shell")

(in reply to Sc4rEye)
 
 
Post #: 18
 
 RE: Ping & Excute Script - 4/16/2007 6:30:07 AM   
  Sc4rEye

 

Posts: 26
Score: 0
Joined: 10/28/2005
Status: offline
Thanks  Country73 that worked perfectly. 

Now, I was putting this all together with everything I have.  And I realized something.  I should have thought
of this before, but I got caught up in this whole scripting thing, plus a few other projects I got going on.  Anyway
this is the problem.  

When a IP fails.  It starts running the script myfile.bat,  GREAT!   BUT, BUT,  this is the problem
depending on which IP fails it has to execute that particular .bat file.  For example

If 192.168.2.1 fails ping it would have to run c:\myfile2.bat

If 192.168.3.1 fails ping it would have to run c:\myfile3.bat

If 192.168.4.1 passes ping then move to the next IP.


I don't know if this possible, any suggestions ?


Thanks, again & again & again (x infinity)
ScarEye




Dim oShell : Set oShell = WScript.CreateObject ("WSCript.shell")
IF INSTR(1, Wscript.FullName, "CScript", VBTEXTCOMPARE) = 0 THEN
  oShell.Run "cscript """ & Wscript.ScriptFullName & """", 0, FALSE
  Wscript.Quit
END IF
Dim arrIPAddr : arrIPAddr = Array("192.168.1.1", "192.168.2.1", "192.168.3.1", _
                                                     "192.168.4.1", "192.168.5.1", "192.168.222.1")             
Dim strIP
For Each strIP In arrIPAddr
If Not Reachable(strIP) Then
     If Reachable("www.google.com") Then
         oShell.Run "c:\somefile.bat"
     End If
End If
Next


Function Reachable(strComputer)
'     On Error Resume Next

Dim objShell, objExec, strCmd, strTemp

strCmd = "ping -n 1 " & strComputer

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strCmd)
strTemp = UCase(objExec.StdOut.ReadAll)

If InStr(strTemp, "REPLY FROM") Then
    Reachable = True
Else
    Reachable = False
End If
End Function 

(in reply to Country73)
 
 
Post #: 19
 
 RE: Ping & Excute Script - 4/16/2007 6:44:18 AM   
  ebgreen


Posts: 4946
Score: 31
Joined: 7/12/2005
Status: offline
Look at the documentation for Select - Case constructs.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Sc4rEye)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Ping & Excute Script Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts