Skip to content

Instantly share code, notes, and snippets.

@jeremyje
Last active October 7, 2021 22:30
Show Gist options
  • Select an option

  • Save jeremyje/df65eee6cff02419cd1552ae9a03f85d to your computer and use it in GitHub Desktop.

Select an option

Save jeremyje/df65eee6cff02419cd1552ae9a03f85d to your computer and use it in GitHub Desktop.
rdp
#!/bin/bash
# curl -s -L -H 'Cache-Control: no-cache' https://gist.githubusercontent.com/jeremyje/df65eee6cff02419cd1552ae9a03f85d/raw/rdp.sh | bash -s PROJECT_ID_OPTIONAL NETWORK_NAME_OPTIONAL
# Creates an allow firewall rule for your machine to access a Windows via RDP.
# This firewall rule will only apply to VMs with the "rdp" network tag.
# This script creates a firewall rule in the target project that accepts
# port 3389 traffic. You may need to change this every so often as your IP
# address changes.
#
# ./rdp.sh <PROJECT_ID> <NETWORK>
PROJECT_ID=${1:-$(gcloud config get-value project)}
NETWORK=${2:-default}
# https://whatismyipaddress.com/api
MY_IP_ADDRESS=$(curl ipv4bot.whatismyipaddress.com)
MY_IP_UNDERSCORED=$(echo "${MY_IP_ADDRESS}" | tr . -)
RDP_RULE_NAME="rdp-${NETWORK}-${MY_IP_UNDERSCORED}-all"
NODEPORT_RULE_NAME="k8snp-${NETWORK}-${MY_IP_UNDERSCORED}-all"
echo "Enabling RDP to ${MY_IP_ADDRESS} for ${PROJECT_ID} via ${RDP_RULE_NAME}"
gcloud --project "${PROJECT_ID}" compute firewall-rules create \
"${RDP_RULE_NAME}" \
--allow=tcp:3389 \
--description="Allow RDP access from ${MY_IP_ADDRESS}" \
--direction=INGRESS \
--network="${NETWORK}" \
--source-ranges="${MY_IP_ADDRESS}/32"
echo "Enabling NodePort access to ${MY_IP_ADDRESS} for ${PROJECT_ID} via ${NODEPORT_RULE_NAME}"
gcloud --project "${PROJECT_ID}" compute firewall-rules create \
"${NODEPORT_RULE_NAME}" \
--allow=tcp:3000-32767 \
--description="Allow NodePort access to ${MY_IP_ADDRESS}" \
--direction=INGRESS \
--network="${NETWORK}" \
--source-ranges="${MY_IP_ADDRESS}/32"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment