-
-
Save nitz/398cd9924ecccddfdca026ebea58353a to your computer and use it in GitHub Desktop.
Simple daemon to use tmate to control a remote machine behind NAT. Uses tmate to generate sessions and save them in a cloud-sync provider folder (ex Dropbox or GDrive)
This file contains hidden or 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 | |
IP=$(ip a s wlp3s0 | grep inet | awk '{print $2}' | head -n 1) | |
while true; do | |
ping -q -w 1 -c 1 `ip r | grep default | awk '{print $3}'` > /dev/null | |
# No internet connection | |
if (( $? != 0 )); then | |
sleep 20s | |
ping -q -w 1 -c 1 `ip r | grep default | awk '{print $3}'` > /dev/null | |
if (( $? != 0 )); then | |
echo "Lost connection, reset ssh" | |
touch /path/to/Cloud-sync-service/.ssh_control/.reset_ssh | |
fi | |
fi | |
# Check if changed ip, if true, reset_ssh and update the ip | |
TMPIP=$(ip a s wlp3s0 | grep inet | awk '{print $2}' | head -n 1) | |
echo $TMPIP | |
echo $IP | |
if [[ "$TMPIP" == "$IP" ]]; then | |
# Check if I have to reset or block_ssh | |
if [ -e /path/to/Cloud-sync-service/.ssh_control/.reset_ssh -o -e /path/to/Cloud-sync-service/.ssh_control/.block_ssh ]; then | |
echo "Resetting all Sockets" | |
killall tmate | |
rm -rf /path/to/Cloud-sync-service/.ssh_control/.reset_ssh | |
fi | |
if [ ! -e /path/to/Cloud-sync-service/.ssh_control/.block_ssh ]; then | |
ps aux | grep -v grep | grep tmate.sock | |
if (( $? == 0 )); then | |
/usr/bin/tmate -S /tmp/tmate.sock display -p '#{tmate_ssh}' > /path/to/Cloud-sync-service/.ssh-key.txt | |
else | |
unset TMUX | |
ps aux | grep '/usr/bin/tmate -S /tmp/tmate.sock new-session -d' | grep -v grep | awk '{print $2}' | xargs kill -9 | |
/usr/bin/tmate -S /tmp/tmate.sock new-session -d | |
sleep 20s | |
/usr/bin/tmate -S /tmp/tmate.sock display -p '#{tmate_ssh}' > /path/to/Cloud-sync-service/.ssh-key.txt | |
fi | |
ps aux | grep -v grep | grep tmate-2.sock | |
if (( $? == 0 )); then | |
/usr/bin/tmate -S /tmp/tmate-2.sock display -p '#{tmate_ssh}' > /path/to/Cloud-sync-service/.ssh-key-2.txt | |
else | |
unset TMUX | |
ps aux | grep '/usr/bin/tmate -S /tmp/tmate-2.sock new-session -d' | grep -v grep | awk '{print $2}' | xargs kill -9 | |
/usr/bin/tmate -S /tmp/tmate-2.sock new-session -d | |
sleep 20s | |
/usr/bin/tmate -S /tmp/tmate-2.sock display -p '#{tmate_ssh}' > /path/to/Cloud-sync-service/.ssh-key-2.txt | |
fi | |
ps aux | grep -v grep | grep tmate-3.sock | |
if (( $? == 0 )); then | |
/usr/bin/tmate -S /tmp/tmate-3.sock display -p '#{tmate_ssh}' > /path/to/Cloud-sync-service/.ssh-key-3.txt | |
else | |
unset TMUX | |
ps aux | grep '/usr/bin/tmate -S /tmp/tmate-3.sock new-session -d' | grep -v grep | awk '{print $2}' | xargs kill -9 | |
/usr/bin/tmate -S /tmp/tmate-3.sock new-session -d | |
sleep 20s | |
/usr/bin/tmate -S /tmp/tmate-3.sock display -p '#{tmate_ssh}' > /path/to/Cloud-sync-service/.ssh-key-3.txt | |
fi | |
ps aux | grep -v grep | grep tmate-4.sock | |
if (( $? == 0 )); then | |
/usr/bin/tmate -S /tmp/tmate-4.sock display -p '#{tmate_ssh}' > /path/to/Cloud-sync-service/.ssh-key-4.txt | |
else | |
unset TMUX | |
ps aux | grep '/usr/bin/tmate -S /tmp/tmate-4.sock new-session -d' | grep -v grep | awk '{print $2}' | xargs kill -9 | |
/usr/bin/tmate -S /tmp/tmate-4.sock new-session -d | |
sleep 20s | |
/usr/bin/tmate -S /tmp/tmate-4.sock display -p '#{tmate_ssh}' > /path/to/Cloud-sync-service/.ssh-key-4.txt | |
fi | |
chmod +x /path/to/Cloud-sync-service/.ssh-key*.txt | |
else | |
echo "Connections Blocked" | |
fi | |
else | |
echo "Changed ip, reset ssh" | |
IP=$TMPIP | |
touch /path/to/Cloud-sync-service/.ssh_control/.reset_ssh | |
fi | |
sleep 20s | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment