Thursday, March 21, 2019

RDP Ping

UPDATE: This content is no longer maintained here. Instead go to

UPDATE: Version 2.3 Released!  This version downloads TCPing if you don't have it.  Since this script is now fully contained, I will no longer be hosting the script except as embedded in this post (thank you to my new code host

UPDATE: Version 2.2 Released!  This version fixes some bugs with the previous version and adds a help section. Run reboot.bat without any arguments and instead of rebooting the local box it will display help.
UPDATE: Version 2.1 Released!  This version adds a switch that allows you to skip the reconnect.  Just add NOCONNECT after the name of the server.

Original Post

UPDATE: I decided to just post this as a script.
I also updated the script to just perform the whole reboot ping tcping mstsc series of commands.  Now it only requires the name of the server to reboot (the credentials you used to launch this script will need admin access on the target server).  It'll shut down the server, do a continuous ping, look for failed pings, then look for successful pings, then use tcping to watch for the RDP service to come up.  Once it does, it'll launch your RDP client and connect you to the server.  Tcping will have to be in the same directory where this batch file exists.  I also noted a way of doing this for a bunch of servers at one time.  Enjoy!
@echo off
:: This script reboots a server, pings until it doesn't respond, 
:: pings until it responds, then waits for RDP to come up, then 
:: launches the RDP client and connects to the server.  If your 
:: password is saved and you don't have a welcome message before 
:: logon, you should be brought directly to the desktop of the 
:: server after rebooting.
:: the first and only argument is the name of the server to reboot.
:: use a command like the following to run this for a list of servers
:: FOR %A in (server1 server2 server3) DO (start reboot.bat %A)

::shutdown the server
echo %1 rebooting...
shutdown /r /d p:4:1 /m \\%1 /t 0 /c "Remote reboot requested"
if errorlevel 1 GOTO:EOF
::Ping until unsuccessful then successful
set pingfailyet=FALSE
set pingwaittime=3
echo Pinging %1...
ping -n 1 %1 | find "Reply"
if %errorlevel%==0 (
 if %pingfailyet%==FALSE ( 
  ::echo %1 hasn't gone down yet.  Pinging again in %pingwaittime% seconds...
  CHOICE /C x /N /T %pingwaittime% /D x > NUL
  goto startping
 ) else ( 
  echo Successfully pinged %1.
  goto endping
) else (
 set pingfailyet==TRUE
 echo No reply from %1.
 CHOICE /C x /N /T %pingwaittime% /D x > NUL
 goto startping
::use tcping to check when RDP becomes available
echo Waiting for RDP on %1 to become available...
tcping.exe -t -i %pingwaittime% -s %1 3389
if %errorlevel%==1 GOTO:EOF
::launch RDP
echo RDP is available on %1.  Connecting...
start mstsc /v:%1 /f
) ELSE (
echo RDP is available on %1.

Original Post

I find myself rebooting servers in remote locations from time to time.  The standard procedure for checking to see whether or not the server is up is by doing a continuous ping.  Once the ping starts timing out, you know the server has gone completely down.  Once it starts responding, you know it's back up.  Well, you know that the NIC is back online, which means the OS is online on some level.  However, most of the time, the first thing I want to do when it comes back up is connect to the server via RDP.  Most of the time, if you start trying to connect via RDP as soon as the server starts responding to pings, the RDP daemon isn't up and running yet.  So you'll get a bunch of timeouts until a minute or two later when it finally comes online.  So, I've wanted a way to check to see that the RDP daemon is up and running before trying to connect.

Now I have a way.  There is a great little utility called tcping.exe that does what ping does, except it does it for TCP ports instead of just checking the IP address.  However, in order to not have to set things up every time and also to automatically kick off the RDP session, I've put together the following batch file:
This can be run either from a shortcut (tip, for Windows 7 users put it in the All Programs folder and you can run it from the start menu just by typing rdping) or it can be run from the command line with the name or IP address as the only argument of the batch file.

1 comment: