Pre-connect scripts

A connection can run a script or executable before startup.

The pre-connect action runs locally on your Mac before the RDP connection is established.

This is useful when the remote machine needs some preparation first — for example, waking a sleeping computer, starting a virtual machine, bringing up a VPN, or performing some other setup required before the RDP service becomes reachable.

Pre-connect actions are configured on the Scripting tab of the connection properties.

Script location

For security, pre-connect scripts must be stored in:

~/Library/Application Scripts/com.gershnik.RDPlenty

or in a subdirectory beneath it.

The selected file itself must reside there. A symbolic link in this directory that points to an executable somewhere else cannot be used.

If you need to run a program or script located elsewhere, place a small wrapper script in the application scripts directory and have the wrapper invoke it.

The script must be executable. If it is not already, you can give it execute permission from Terminal with:

chmod a+x myscript

Arguments

You can specify command-line arguments to pass to the script in the connection properties.

Arguments are parsed using normal command-line quoting and escaping rules. For example:

"argument with spaces"
'argument with spaces'
argument\ with\ spaces

Backslash escapes such as \n can also be used.

Environment variables are not expanded in the arguments field. For example, $HOME is passed to the script literally rather than being replaced with your home directory.

If you need environment-variable expansion or other shell processing, perform it inside the script itself.

While the script is running

The connection does not begin until the pre-connect action has finished.

While it is running, RDPlenty displays the script’s output so you can see its progress or any errors it reports.

From that screen, you can choose to:

  • continue with the RDP connection without waiting for the script, or

  • cancel the connection.

This is useful if a preparation step is taking longer than expected or has failed in a way that does not actually prevent you from connecting.

Example: waking a remote computer

The following script uses an external wakeonlan utility to wake a computer, then waits briefly for its RDP port to become reachable before allowing the connection to proceed.

#!/bin/bash
set -eu

HOST="${1:?Usage: $0 HOST}"
PORT=3389
WAIT_SECONDS=5

rdp_ready() {
    echo "waiting for host..."
    nc -z -G 2 "$HOST" "$PORT" >/dev/null 2>&1
}

/usr/local/bin/wakeonlan "$HOST"

if rdp_ready; then
    exit 0
fi

deadline=$(( $(date +%s) + WAIT_SECONDS ))

while [ "$(date +%s)" -lt "$deadline" ]; do
    if rdp_ready; then
        exit 0
    fi
    sleep 1
done

The host name or address can then be supplied to the script through the arguments field in the connection properties.