Link to home
Start Free TrialLog in
Avatar of bjv211
bjv211

asked on

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.
Avatar of Kieran_Burns
Kieran_Burns

:start
ping -n 1 <server_ip>
if errorlevel 0 goto remote_desktop
goto start
:remote_desktop
mstsc /v:<server_ip>
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
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.
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
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

also forgot the /v parameter, like this:
Shell "mstsc /v:" & myComputerName
SOLUTION
Avatar of Christian de Bellefeuille
Christian de Bellefeuille
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bjv211

ASKER

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

:start
ping -n 1 %1
if %errorlevel% == 0 goto remote_desktop
goto start
:remote_desktop
ping -n 60 %1
mstsc /v:%1
 
 Works
Avatar of bjv211

ASKER

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
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
you mean
ping -n 60000 %1
forget what i said :-)
Avatar of bjv211

ASKER

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!
ping 127.0.0.1 then :-)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This code still works today and is amazing!  Great job Kieran_Burns!