-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtmateSender.sh
87 lines (60 loc) · 1.9 KB
/
tmateSender.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# a script that checks if connection is down for 5 minutes
# then waits till connection comes back again
# exits tmate session
# creates a new tmate session and send session id using a telegram bot
TIME=$(date +"%c")
DOWN_TIME=0
LAST_SEEN=$(date +"%s")
MAX_DOWN_TIME=1200 # 20 minutes
SLEEP_INTERVAL=30 # 30 seconds
SITE=google.com # SITE to check against
# Telegram Credentials
TOKEN= # bot token
CHAT_ID= # your telegram chat id
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
# Interface
INTF=''
BOOT_MESSAGE="Greeting" # Greeting Message
POWERED_ON=1
func sendMessage ( ) {
#stop previous session of tmate
tmate -S /tmp/tmate.sock kill-sessioni 2> /dev/null
#start a new tmate session
tmate -S /tmp/tmate.sock new-session -d 2> /dev/null
tmate -S /tmp/tmate.sock wait tmate-ready 2> /dev/null
#extract the session id
SESSION_ID=$(tmate -S /tmp/tmate.sock display -p '#{tmate_ssh}')
LOCAL_IPV4=$(ip address show eth0 | grep "inet\b" | sed "s/^ *//" | cut -d ' ' -f 2)
# compose the message
MESSAGE="\[ $TIME \]
- Session $SESSION_ID
- IPV4 $LOCAL_IPV4"
#email the session id - changed to telegram
curl -s -X POST $URL -d chat_id=$CHAT_ID -d text="$MESSAGE" 2> /dev/null
}
while [ true ]
do # infintely run the script
if ! ping -c 1 "$SITE" ; then # checking if connection is down
DOWN_TIME=$(( $(date +"%s") - $LAST_SEEN ))
else
if [[ $POWERED_ON -eq 1 ]] ; then
curl -s -X POST $URL -d chat_id=$CHAT_ID -d text="$BOOT_MESSAGE"
POWERED_ON=0
fi
DOWN_TIME=0
LAST_SEEN=$(date +"%s")
fi
sleep $SLEEP_INTERVAL # lay low
if [ $DOWN_TIME -ge $MAX_DOWN_TIME ]; then
while [ ! nc -zw1 google.com 443 ]; do
# loop until connection comes up
sleep $SLEEP_INTERVAL # sleep for two minutes
done
sendMessage
sleep (($SLEEP_INTERVAL * 10 )) # sleeps for 30 * 10 seconds = 6 min
# reset everything
DOWN_TIME=0
LAST_SEEN=$(date +"%s")
fi
done