Skip to content

Instantly share code, notes, and snippets.

@rokibhasansagar
Last active October 19, 2023 15:49
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 rokibhasansagar/077b66b16cd50e50e5ad9489084dc926 to your computer and use it in GitHub Desktop.
Save rokibhasansagar/077b66b16cd50e50e5ad9489084dc926 to your computer and use it in GitHub Desktop.
Modified ngrok2actions for own use
#!/usr/bin/env bash
#
# Copyright (c) 2020-2021 P3TERX <https://p3terx.com>
#
# https://github.com/P3TERX/ssh2actions
# File name:ngrok2actions.sh
# Description: Connect to Github Actions VM via SSH by using ngrok
# Version: 2.0
#
GreenFont="\033[32m"
RedFont="\033[31m"
FontReset="\033[0m"
INFO="[${GreenFont}INFO${FontReset}]"
ERROR="[${RedFont}ERROR${FontReset}]"
LOG_FILE='/tmp/ngrok.log'
TELEGRAM_LOG="/tmp/telegram.log"
CONTINUE_FILE="/tmp/continue"
# REPO_PURPOSE = "Description of for which ngrok is running"
if [[ -z "${NGROK_TOKEN}" ]]; then
echo -e "${ERROR} Please set 'NGROK_TOKEN' environment variable."
exit 2
fi
if [[ -z "${SSH_PASSWORD}" ]]; then
echo -e "${ERROR} Please set 'SSH_PASSWORD' environment variable."
exit 3
fi
if [[ $OSTYPE == "linux-gnu" ]]; then
echo -e "${INFO} Install ngrok ..."
curl -fsSL https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip -o ngrok.zip
unzip ngrok.zip ngrok
rm ngrok.zip
chmod +x ngrok
sudo mv ngrok /usr/local/bin
ngrok -v
elif [[ $OSTYPE == "darwin"* ]]; then
echo -e "${INFO} Install ngrok ..."
curl -fsSL https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-darwin-amd64.zip -o ngrok.zip
unzip ngrok.zip ngrok
rm ngrok.zip
chmod +x ngrok
sudo mv ngrok /usr/local/bin
ngrok -v
USER=root
echo -e "${INFO} Set SSH service ..."
echo 'PermitRootLogin yes' | sudo tee -a /etc/ssh/sshd_config >/dev/null
sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist
sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
else
echo -e "${ERROR} This system is not supported!"
exit 1
fi
if [[ -n "${SSH_PASSWORD}" ]]; then
echo -e "${INFO} Set user(${USER}) password ..."
echo -e "${SSH_PASSWORD}\n${SSH_PASSWORD}" | sudo passwd "${USER}"
fi
echo -e "${INFO} Start ngrok proxy for SSH port..."
screen -dmS ngrok \
ngrok tcp 22 \
--log "${LOG_FILE}" \
--authtoken "${NGROK_TOKEN}" \
--region "${NGROK_REGION:-us}"
while ((${SECONDS_LEFT:=5} > 0)); do
echo -e "${INFO} Please wait ${SECONDS_LEFT}s ..."
sleep 1
SECONDS_LEFT=$((${SECONDS_LEFT} - 1))
done
ERRORS_LOG=$(grep "command failed" ${LOG_FILE})
if [[ -e "${LOG_FILE}" && -z "${ERRORS_LOG}" ]]; then
SSH_CMD="$(grep -oE "tcp://(.+)" ${LOG_FILE} | sed "s/tcp:\/\//ssh ${USER}@/" | sed "s/:/ -p /")"
MSG="
*GitHub Actions - ngrok access for ${REPO_PURPOSE}:*
⚡ *CLI:*
\`${SSH_CMD}\`
"
if [[ -n "${TELEGRAM_BOT_TOKEN}" && -n "${TELEGRAM_CHAT_ID}" ]]; then
echo -e "${INFO} Sending message to Telegram..."
curl -sSX POST "${TELEGRAM_API_URL:-https://api.telegram.org}/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d "disable_web_page_preview=true" \
-d "parse_mode=Markdown" \
-d "chat_id=${TELEGRAM_CHAT_ID}" \
-d "text=${MSG}" >${TELEGRAM_LOG}
TELEGRAM_STATUS=$(cat ${TELEGRAM_LOG} | jq -r .ok)
if [[ ${TELEGRAM_STATUS} != true ]]; then
echo -e "${ERROR} Telegram message sending failed: $(cat ${TELEGRAM_LOG})"
else
echo -e "${INFO} Telegram message sent successfully!"
fi
fi
while ((${PRT_COUNT:=1} <= ${PRT_TOTAL:=5})); do
SECONDS_LEFT=${PRT_INTERVAL_SEC:=5}
while ((${PRT_COUNT} > 1)) && ((${SECONDS_LEFT} > 0)); do
echo -e "${INFO} (${PRT_COUNT}/${PRT_TOTAL}) Please wait ${SECONDS_LEFT}s ..."
sleep 1
SECONDS_LEFT=$((${SECONDS_LEFT} - 1))
done
echo "------------------------------------------------------------------------"
echo "To connect to this session copy and paste the following into a terminal:"
echo -e "${GreenFont}$SSH_CMD${FontReset}"
echo "------------------------------------------------------------------------"
PRT_COUNT=$((${PRT_COUNT} + 1))
done
else
echo -e "${ERRORS_LOG}"
exit 4
fi
while [[ -n $(ps aux | grep ngrok) ]]; do
sleep 2
touch ${CONTINUE_FILE}
if [[ -e ${CONTINUE_FILE} ]]; then
echo -e "${INFO} Continuing to the next step."
exit 0
fi
done
#!/usr/bin/env bash
GreenFont="\033[32m"
RedFont="\033[31m"
FontReset="\033[0m"
INFO="[${GreenFont}INFO${FontReset}]"
ERROR="[${RedFont}ERROR${FontReset}]"
LOG_FILE='/tmp/ngrok.log'
TELEGRAM_LOG="/tmp/telegram.log"
CONTINUE_FILE="/tmp/continue"
# REPO_PURPOSE = "Description of for which ngrok is running"
if [[ -z "${NGROK_TOKEN}" ]]; then
echo -e "${ERROR} Please set 'NGROK_TOKEN' environment variable."
exit 2
fi
if [[ -z "${SSH_PASSWORD}" ]]; then
echo -e "${ERROR} Please set 'SSH_PASSWORD' environment variable."
exit 3
fi
if [[ $OSTYPE == "linux-gnu" ]]; then
echo -e "${INFO} Install ngrok ..."
curl -fsSL https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz -o ngrok.tgz
tar xf ngrok.tgz ngrok
rm ngrok.tgz
chmod +x ngrok
sudo mv ngrok /usr/local/bin
ngrok -v
elif [[ $OSTYPE == "darwin"* ]]; then
echo -e "${INFO} Install ngrok ..."
curl -fsSL https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-darwin-amd64.zip -o ngrok.zip
unzip ngrok.zip ngrok
rm ngrok.zip
chmod +x ngrok
sudo mv ngrok /usr/local/bin
ngrok -v
USER=root
echo -e "${INFO} Set SSH service ..."
echo 'PermitRootLogin yes' | sudo tee -a /etc/ssh/sshd_config >/dev/null
sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist
sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
else
echo -e "${ERROR} This system is not supported!"
exit 1
fi
if [[ -n "${SSH_PASSWORD}" ]]; then
echo -e "${INFO} Set user(${USER}) password ..."
echo -e "${SSH_PASSWORD}\n${SSH_PASSWORD}" | sudo passwd "${USER}"
fi
echo -e "${INFO} Start ngrok proxy for SSH port..."
screen -dmS ngrok \
ngrok tcp 22 \
--log "${LOG_FILE}" \
--authtoken "${NGROK_TOKEN}" \
--region "${NGROK_REGION:-us}"
while ((${SECONDS_LEFT:=5} > 0)); do
echo -e "${INFO} Please wait ${SECONDS_LEFT}s ..."
sleep 1
SECONDS_LEFT=$((${SECONDS_LEFT} - 1))
done
ERRORS_LOG=$(grep "command failed" ${LOG_FILE})
if [[ -e "${LOG_FILE}" && -z "${ERRORS_LOG}" ]]; then
SSH_CMD="$(grep -oE "tcp://(.+)" ${LOG_FILE} | sed "s/tcp:\/\//ssh ${USER}@/" | sed "s/:/ -p /")"
MSG="
*GitHub Actions - ngrok access for ${REPO_PURPOSE}:*
⚡ *CLI:*
\`${SSH_CMD}\`
"
if [[ -n "${TELEGRAM_BOT_TOKEN}" && -n "${TELEGRAM_CHAT_ID}" ]]; then
echo -e "${INFO} Sending message to Telegram..."
curl -sSX POST "${TELEGRAM_API_URL:-https://api.telegram.org}/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d "disable_web_page_preview=true" \
-d "parse_mode=Markdown" \
-d "chat_id=${TELEGRAM_CHAT_ID}" \
-d "text=${MSG}" >${TELEGRAM_LOG}
TELEGRAM_STATUS=$(cat ${TELEGRAM_LOG} | jq -r .ok)
if [[ ${TELEGRAM_STATUS} != true ]]; then
echo -e "${ERROR} Telegram message sending failed: $(cat ${TELEGRAM_LOG})"
else
echo -e "${INFO} Telegram message sent successfully!"
fi
fi
while ((${PRT_COUNT:=1} <= ${PRT_TOTAL:=5})); do
SECONDS_LEFT=${PRT_INTERVAL_SEC:=10}
while ((${PRT_COUNT} > 1)) && ((${SECONDS_LEFT} > 0)); do
echo -e "${INFO} (${PRT_COUNT}/${PRT_TOTAL}) Please wait ${SECONDS_LEFT}s ..."
sleep 2
SECONDS_LEFT=$((${SECONDS_LEFT} - 2))
done
echo "------------------------------------------------------------------------"
echo "To connect to this session copy and paste the following into a terminal:"
echo -e "${GreenFont}$SSH_CMD${FontReset}"
echo "------------------------------------------------------------------------"
PRT_COUNT=$((${PRT_COUNT} + 1))
done
else
echo -e "${ERRORS_LOG}"
exit 4
fi
while [[ -n $(ps aux | grep ngrok) ]]; do
sleep 2
touch ${CONTINUE_FILE}
if [[ -e ${CONTINUE_FILE} ]]; then
echo -e "${INFO} Continuing to the next step."
exit 0
fi
done
#!/usr/bin/env bash
mkdir -p ~/.ssh
cat << EOK > ~/.ssh/config
Host *
ServerAliveInterval 30
StrictHostKeyChecking no
Host github.com
User git
Port 22
Hostname github.com
TCPKeepAlive yes
IdentitiesOnly yes
EOK
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment