[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14213
  • Last Modified:

How to ping until I receive a response, then execute Remote Desktop?

I want to create a batch or vbs file to ping an IP address until I get a proper response, at which point I'll stop pinging and execute remote desktop (mstsc) to the same IP address.
0
bjv211
Asked:
bjv211
  • 6
  • 5
  • 3
  • +3
2 Solutions
 
Kieran_BurnsCommented:
:start
ping -n 1 <server_ip>
if errorlevel 0 goto remote_desktop
goto start
:remote_desktop
mstsc /v:<server_ip>
0
 
vertsyeuxCommented:
You could use the errorlevel of the PING command like this..

ping www.google.com > NUL
if ERRORLEVEL 1 goto Yep
if not ERRORLEVEL 1 goto Nope

Alternatively, here's a neat little trick that works by comparing files..

http://www.daniweb.com/forums/thread73589-2.html
0
 
abelCommented:
note that servers and firewalls allowing remote desktop, do not necessarily respond to Ping (echo) requests. Often that port is closed to prevent ping floods.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Kieran_BurnsCommented:
just for reference - you will need a pause / wait statement in the batch file as the terminal services service takes about a minute to come active
You could use the ping command again for 60 times which will wait the required minute
If you call the batch file REMOTE.BAT change the batch file above to:

:start
ping -n 1 %1
if errorlevel 0 goto remote_desktop
goto start
:remote_desktop
ping -n 60 %1
mstsc /v:%1
then you can use the command:
 REMOTE <server_ip> and use it for any IP address or Server name you choose
0
 
Christian de BellefeuilleProgrammerCommented:
Here's some VBA.

I forgot to implement the timeout but its just to avoid an infinite loop
Public Sub TestX()
    PingNRemoteDesktop "127.0.0.1", 1000
End Sub
 
Public Sub PingNRemoteDesktop(ByVal myComputerName As String, ByVal nTimeout As Long)
    Dim fs, f
    Dim s As String
    
    Set objShell = CreateObject("WScript.Shell")
        
    While (1)
        objShell.Run "cmd /c ping -n 2 -w 4000 " & myComputerName & " >C:\pingresult.txt", 0, True
        Set fs = CreateObject("Scripting.FileSystemObject")
        
        Set f = fs.OpenTextFile("c:\pingresult.txt")
        For i = 1 To 5
            s = f.ReadLine()
        Next i
        
        If Left(s, 10) = "Reply from" Then
            Shell "mstsc " & myComputerName
            Exit Sub
        End If
    Wend
End Sub

Open in new window

0
 
Christian de BellefeuilleProgrammerCommented:
also forgot the /v parameter, like this:
Shell "mstsc /v:" & myComputerName
0
 
Christian de BellefeuilleProgrammerCommented:
sorry, did 2 mistakes.  Forgot to close the file also...
so here's a version with timeout handling
Public Sub TestX()
    If Not PingNRemoteDesktop("www.microsoft.com", 2) Then
        MsgBox "Timeout"
    End If
End Sub
 
Public Function PingNRemoteDesktop(ByVal myComputerName As String, ByVal nTimeout As Long) As Boolean
    Dim fs, f
    Dim s As String
    Dim nCount As Long
    
    Set objShell = CreateObject("WScript.Shell")
        
    While (1)
        objShell.Run "cmd /c ping -n 1 -w 1000 " & myComputerName & " >C:\pingresult.txt", 0, True
        Set fs = CreateObject("Scripting.FileSystemObject")
        
        Set f = fs.OpenTextFile("c:\pingresult.txt")
        For i = 1 To 5
            s = f.ReadLine()
        Next i
        
        If Left(s, 10) = "Reply from" Then
            Shell "mstsc /v:" & myComputerName
            PingNRemoteDesktop = True
            Exit Function
        End If
        nCount = nCount + 1
        f.Close
        
        If nCount > nTimeout Then
            PingNRemoteDesktop = False
            Exit Function
        End If
    Wend
End Function

Open in new window

0
 
bjv211Author Commented:
I'm testing on a computer that I know is not online and it still executes the remote desktop command with no ping response...see attached snipping.

Capture.JPG
0
 
Kieran_BurnsCommented:
Whoops! :-)
 

:start
ping -n 1 %1
if %errorlevel% == 0 goto remote_desktop
goto start
:remote_desktop
ping -n 60 %1
mstsc /v:%1
 
 Works
0
 
bjv211Author Commented:
That works brilliantly, and I like the option of adding the IP as an argument in the shortcut.  One last item, if it doesn't get a response, pause for X seconds, then goto start
0
 
Kieran_BurnsCommented:
don't push it ;-)
Just add the ping -n 60 (or whatever time to wait) AFTER the if %errorlevel% line
:start
ping -n 1 %1
if %errorlevel% == 0 goto remote_desktop
ping -n <number_of_seconds> %1
goto start
:remote_desktop
ping -n 60 %1
mstsc /v:%1
0
 
Christian de BellefeuilleProgrammerCommented:
you mean
ping -n 60000 %1
0
 
Christian de BellefeuilleProgrammerCommented:
forget what i said :-)
0
 
bjv211Author Commented:
The reason for the pause is to not constantly ping as our security group would not like that, so if there's no way to do that I'll leave it as is :)
thanks again!
0
 
Kieran_BurnsCommented:
ping 127.0.0.1 then :-)
0
 
Kieran_BurnsCommented:
I've done a little tidying up and added some remarks for clarity. I'm pinging the local host now while waiting, rather than the remote one (in line with your wish not to upset the Security guys)

@echo off
rem batch file to ping a Server until
rem it is available. Then wait 60 seconds
rem and connect using Terminal Services
rem syntax is: REMOTE <name_of_server>

rem ping host until it responds
:start
ping -n 1 %1
if %errorlevel% == 0 goto remote_desktop

rem wait for 10 seconds before trying again
@echo Waiting 10 seconds...
ping -n 10 127.0.0.1 > nul

rem loopback to start
goto start

rem initiate remote desktop session
:remote_desktop
@echo *****************************************************************
@echo *Server is now available. Waiting 1 minute for services to start*
@echo *****************************************************************

rem wait 60 seconds after server is available to
rem allow services to start
ping -n 60 127.0.0.1 > nul
mstsc /v:%1  
 
By the way - if you create a .rdp file for each connection, and call it the same name as the Server (natch) then you could change the last line to:
mstsc %1.rdp
and launch straight into the session.
0
 
samiam41Commented:
This code still works today and is amazing!  Great job Kieran_Burns!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now