#!/bin/bash PIDS=`ps -ef| grep ssh |grep 9222 | awk '{print $2}'` if [ -z "$PIDS" ]; then #查看服务是否开了 ssh -C -f -N -g -R 9222:127.0.0.1:22 [email protected] echo "start ssh exporter:`date`" >> /home/deploy/autocheck/log.txt else nc -w 3 -z 42.121.119.100 9222 > /dev/null 2>&1 if [ $? -ne 0 ]; then #查看端口是否通 kill -9 $PIDS ssh -C -f -N -g -R 9222:127.0.0.1:22 [email protected] echo "kill and start ssh exporter:`date`" >> /home/deploy/autocheck/log.txt fi fi
因为ssh 隧道有时会自动关闭,所以需要crontab 添加每分钟检测一次
*/1 * * * * /home/deploy/autocheck/e.sh
此脚本是暴露本地22端口给42.121.119.100的9222,这样通过ssh 42.121.119.100 -p 9222就可以连进本机。还有一步是开通本机到42.121.119.100免密码登陆。