Skip to content

Instantly share code, notes, and snippets.

@matejc
Created August 29, 2023 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matejc/79dc24e90fb69a15eeb713da6c761f23 to your computer and use it in GitHub Desktop.
Save matejc/79dc24e90fb69a15eeb713da6c761f23 to your computer and use it in GitHub Desktop.
Unattended login into ARM RDP
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash freerdp3 gnugrep chromedriver jq
# Used for unattended (afk) logins
# My usecase:
# when machine locks its screen,
# there is no way to paste to paste longer password to login,
# so just close it and run this script again
#
# Conditions:
# - for xfreerdp v3
# - access through ARM gateway
# - need to be logged in with SSO in the browser
#
# Run browser with enabled debugging port on 9222:
# google-chrome --remote-debugging-port=9222 --user-data-dir="$USER_DATA_DIR"
#
# Tested with google-chrome, could work with other chromium based browsers
set -e
# Script need to set following variables, example contents:
#
# export RDPW_FILE="$HOME/Downloads/file.rdpw"
# export RDP_USERNAME='your@email'
# export RDP_PASSWORD='your password'
source "$(dirname "$0")/secrets.sh"
debuggerPort=9222
chromedriverPort=9223
rdpPidFile="$(mktemp)"
rdpInStream="$(mktemp --dry-run)"
mkfifo "$rdpInStream"
trap 'kill $chromedriverPid; kill $(cat "$rdpPidFile"); rm $rdpPidFile' EXIT SIGINT
while read -r line
do
echo "$line"
if [[ $line = Browse?to:* ]]
then
chromedriver --port=$chromedriverPort & chromedriverPid=$!
sleep 1
sessionId=$(curl -d "{ \"desiredCapabilities\": { \"goog:chromeOptions\": { \"debuggerAddress\": \"localhost:$debuggerPort\" } } }" "http://localhost:$chromedriverPort/session" | jq -r '.sessionId')
rdpLoginUrl="$(echo "$line" | grep -o "https://.*")"
rdpLoginHandle="$(curl -d "{\"type\":\"tab\"}" "http://localhost:$chromedriverPort/session/$sessionId/window/new" | jq -r '.value.handle')"
sleep 0.5
curl -d "{\"name\":\"$rdpLoginHandle\"}" "http://localhost:$chromedriverPort/session/$sessionId/window"
sleep 0.5
curl -d "{\"url\":\"$rdpLoginUrl\"}" "http://localhost:$chromedriverPort/session/$sessionId/url"
sleep 0.5
rdpUrl="$(curl "http://localhost:$chromedriverPort/session/$sessionId/url" | jq -r '.value')"
elif [[ $line = Paste?redirect?URL?here: ]]
then
echo "$rdpUrl" >> "$rdpInStream"
fi
done < <(tail -f "$rdpInStream" | stdbuf -o0 -i0 xfreerdp "$RDPW_FILE" /u:"$RDP_USERNAME" /p:"$RDP_PASSWORD" /sec:nla /cert:ignore +clipboard /gateway:type:arm /network:auto /gfx:AVC444 /rfx /size:1918x1172 /smart-sizing 2>&1 & rdpPid=$!; echo -n $rdpPid > "$rdpPidFile"; wait $rdpPid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment