Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call a SUB

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
Hi,

Written this code to call telnet then run a do while loop if the IP address of the server entered is not pingable. I am going to use it to reboot remotely some gprs signs on our network

anyway something strange happens when I run it and supply the text box with an IP address. It seems the sub runs twice and creates to cmd logins for telnet,

Have I done something wrong with the Do while Loop?

IPAddress = inputbox( "Please Enter IP Address:" )
Username = inputbox( "username:" )
password = inputbox( "password:" )





'==========================================================================
' The following function will test if a machine is reachable via a ping
' using WMI and the Win32_PingStatus
'==========================================================================
If Reachable(IPAddress) Then
'WScript.Echo "Computer is Reachable"
Else
WScript.Echo "Computer is Unreachable!"
End If

Function Reachable(strComputer)
' On Error Resume Next

Dim wmiQuery, objWMIService, objPing, objStatus

wmiQuery = "Select * From Win32_PingStatus Where " & _
"Address = '" & strComputer & "'"

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery)

For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
Reachable = False 'if computer is unreacable, return false
Else
Reachable = True 'if computer is reachable, return true
End If
Next


if reachable = true then
call TelnetFunc
else
wscript.echo "Contact TSS"
wscript.quit

end if
End Function






Sub Telnetfunc
Set WshShell = WScript.CreateObject("WScript.Shell")
wshshell.run("telnet " & IPAddress & " 21")


end sub


do while reachable(IPAddress) = false

wscript.sleep 10000
If Reachable(IPAddress) Then
'WScript.Echo "Computer is Reachable"
Else
WScript.Echo "Computer is Unreachable!"
End If


loop

wscript.sleep 10000

wscript.echo "Ping back"


James
 
Your Do loop will run until 'Reachable' = true; at that point your sub gets called. You also have an If block at the top of your code that calls the 'Reachable' function; if that evaluates to true, your sub will be called.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top