Last active
March 21, 2023 06:28
-
-
Save hrz6976/ba664703d347de03d929d9bb483b0eda to your computer and use it in GitHub Desktop.
Auto connect to PKU Gateway & send IP Address to ServerChan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################################################### | |
# To Run this script hourly, you need to add the following cron job: | |
# crontab -e | |
# | |
# Add the following line: | |
# 0 * * * * /path/to/check-server-status.sh | |
# | |
# Then save and exit. | |
############################################################################### | |
### SECRETS ### | |
# Serverchan sendkey https://sct.ftqq.com/ | |
SCKEY='SCTxxxxxxxxxxxxxxxxxxxxxxxxx' | |
# SSH Target | |
SSH_TARGET='random@guy.im' | |
# Credit: https://stackoverflow.com/questions/296536/ | |
rawurlencode() { | |
local string="${1}" | |
local strlen=${#string} | |
local encoded="" | |
local pos c o | |
for (( pos=0 ; pos<strlen ; pos++ )); do | |
c=${string:$pos:1} | |
case "$c" in | |
[-_.~a-zA-Z0-9] ) o="${c}" ;; | |
* ) printf -v o '%%%02x' "'$c" | |
esac | |
encoded+="${o}" | |
done | |
echo "${encoded}" # You can either set a return variable (FASTER) | |
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p | |
} | |
send_sc_message() { | |
if [ -z "$SCKEY" ]; then | |
# time | |
echo "SCKEY not set" | |
return 1 | |
fi | |
if [ -z "$1" ]; then | |
# time | |
echo "No message to send" | |
return 1 | |
fi | |
curl -s -X POST -H 'Content-Type: application/x-www-form-urlencoded' \ | |
-d "title=$(rawurlencode "$1")&desp=$(rawurlencode "$2")" \ | |
"https://sc.ftqq.com/$SCKEY.send" | |
} | |
# main routine | |
ssh -q -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5 "$SSH_TARGET" 'exit 0' | |
if [ $? -ne 0 ]; then | |
# time | |
echo "Lost Connection to $SSH_TARGET" | |
send_sc_message "$SSH_TARGET is down" "$SSH_TARGET is down" | |
echo "" | |
else | |
echo "Connected to $SSH_TARGET" | |
exit 0 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
############################################################################### | |
# To Run this script hourly, you need to add the following cron job: | |
# crontab -e | |
# | |
# Add the following line: | |
# 0 * * * * /path/to/pku-auto-connect.sh | |
# | |
# Then save and exit. | |
############################################################################### | |
### SECRETS ### | |
# Serverchan sendkey https://sct.ftqq.com/ | |
SCKEY='SCTxxxxxxxxxxxxxxxxxxxxxxxx' | |
# UserName and Password of PKU IGSW | |
CONNECT_USER='1800000000' | |
CONNECT_PASSWORD='PKUIGSWPASSWORD' | |
### CONFIG ### | |
ALWAYS_ONLINE_SITES='http://www.baidu.com http://www.taobao.com' | |
ALWAYS_ONLINE_SITES_V6='http://byr.pt' | |
# CONNECT_PATH='./connect' | |
CONNECT_URL="https://its4.pku.edu.cn/cas/ITSClient" | |
# New API Interface | |
connect() { | |
CONNECT_ARGS="cmd=open&username=${CONNECT_USER}&password=${CONNECT_PASSWORD}&iprange=free" | |
curl -Gs -X POST --connect-timeout 5 -d $CONNECT_ARGS "$CONNECT_URL" | |
} | |
send_sc_message() { | |
if [ -z "$1" ] || [ -z "$2" ]; then | |
echo "Missing message or title: message=$1 title=$2" | |
return 1 | |
fi | |
curl -Gs "https://sc.ftqq.com/$SCKEY.send" \ | |
-X POST \ | |
-H 'Content-Type: application/x-www-form-urlencoded' \ | |
--data-urlencode "$(printf 'title=%s' "$1")" \ | |
--data-urlencode "$(printf 'desp=%s' "$2")" | |
} | |
# main routine | |
SC_TITLE="" | |
SC_BODY="" | |
# test whether we are online | |
for site in $ALWAYS_ONLINE_SITES; do | |
echo "Checking link to $site" | |
# if redirected to login page, then we are not online | |
curl --connect-timeout 5 -s -o /dev/null -I -w "%{http_code}" "$site" | grep -q 302 | |
if [ $? -eq 0 ]; then | |
# time | |
echo " Link lost, try reconnecting..." | |
connect | |
if [ $? -ne 0 ]; then | |
# time | |
echo "Error: reconnect failed" | |
exit 1 | |
else | |
# get_ip | |
# printf '%-12s %s\n' gateway $gateway iface $iface ip $ip | |
ip=$(ip route get 1 | awk '{print $(NF-2);exit}') | |
gateway=$(ip route get 1 | awk '{print $3;exit}') | |
iface=$(ip route get 1 | awk '{print $5;exit}') | |
SC_TITLE="Reconnected: $ip" | |
SC_BODY="[IPv4] Gateway: $gateway, Interface: $iface, IP: $ip" | |
fi | |
elif [ -z "$SC_TITLE" ]; then | |
echo " Link established" | |
break | |
fi | |
done | |
# check IPv6 | |
for site in $ALWAYS_ONLINE_SITES_V6; do | |
echo "Checking link to $site" | |
# if redirected to login page, then we are not online | |
curl --connect-timeout 5 -s -o /dev/null -I -w "%{http_code}" "$site" | |
if [ $? -eq 0 ]; then | |
# get_ip | |
# printf '%-12s %s\n' gateway $gateway iface $iface ip $ip | |
ip=$(ip route get 2001:da8::666 | awk '{print $(NF-4);exit}') | |
gateway=$(ip route get 2001:da8::666 | awk '{print $5;exit}') | |
iface=$(ip route get 2001:da8::666 | awk '{print $7;exit}') | |
SC_BODY="${SC_BODY} [IPv6] Gateway: $gateway, Interface: $iface, IP: $ip" | |
break | |
else | |
# do a curl -v | |
SC_BODY="${SC_BODY} [IPv6] $(curl -v --connect-timeout 5 $site)" | |
break | |
fi | |
done | |
if [[ ! -z "$SC_TITLE" ]]; then | |
send_sc_message "$SC_TITLE" "$SC_BODY" | |
echo "" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment