Open
Description
A ssh tunnel could be created in /etc/rc.local. As following:
vim /etc/rc.local
timeout=5
ssh -p 22 -o BatchMode=yes -o ConnectTimeout=3 -i /data/david/xxx.pem -fCNR 7280:localhost:22 root@101.20.5.6
while test $? -gt 0
do
sleep 3 # highly recommended - if it's in your local network, it can try an awful lot pretty quick...
echo "Trying again..."
ssh -p 22 -o BatchMode=yes -o ConnectTimeout=3 -i /data/david/xxx.pem -fCNR 7280:localhost:22 root@101.20.5.6
TIMEOUT=$((TIMEOUT-1))
if [ $TIMEOUT -eq 0 ]; then
echo "timed out"
# error time out
exit 1
fi
done
sleep 3
Here the -o BatchMode=yes means the SSH connection will fail immediately if the key is rejected, instead of failing back to a password prompt. The ConnectionTimeout the timeout for ssh command, so it would not wait for a long time.
Note that for Debian 9/10/11, rc.local would not run by default.
You need to make the script executable, and also enable the service.
chmod +x /etc/rc.local
systemctl start rc-local
systemctl enable rc-local
Metadata
Metadata
Assignees
Labels
No labels