level 1
fjh1997
楼主
#!/bin/sh
#检测是否建立反向隧道
icount=`ps -w|grep "ssh -Nfg"|grep -v grep|wc -l`
if [ $icount == 0 ];then
logger -t "ssh_remote" "ssh remote restart!"
#若没有,内网与114.114.114.114建立反向ssh隧道
ssh -Nfg -K 120 -R *:666:192.168.2.1:22 114.114.114.114 -p 26259 -l root
fi
echo 'starting....'
#读取当前客户端公网IP
Cip=`cat Cip.log | awk '{print $1;}' `
echo $Cip
#指定VPS公网IP
ip="114.114.114.114"
#返回内网路由公网IP
result=`ssh root@$ip -p 22 'echo $SSH_CLIENT' | awk '{print $1;}' `
echo $result
#比较当前IP与先前获取IP
if [ "$Cip" == "$result" ];then
echo "no kill"
else
echo "kill"
id=`ssh root@$ip -p 26259 "ps -w " | grep "ssh -Nfg" | grep -v grep | awk '{print $1;}' `
echo $id
#杀死隧道进程
ssh root@$ip -p 22 "kill "$id
#
echo "$result" > ./Cip.log
fi
echo 'finish....'
2018年08月09日 15点08分
1
#检测是否建立反向隧道
icount=`ps -w|grep "ssh -Nfg"|grep -v grep|wc -l`
if [ $icount == 0 ];then
logger -t "ssh_remote" "ssh remote restart!"
#若没有,内网与114.114.114.114建立反向ssh隧道
ssh -Nfg -K 120 -R *:666:192.168.2.1:22 114.114.114.114 -p 26259 -l root
fi
echo 'starting....'
#读取当前客户端公网IP
Cip=`cat Cip.log | awk '{print $1;}' `
echo $Cip
#指定VPS公网IP
ip="114.114.114.114"
#返回内网路由公网IP
result=`ssh root@$ip -p 22 'echo $SSH_CLIENT' | awk '{print $1;}' `
echo $result
#比较当前IP与先前获取IP
if [ "$Cip" == "$result" ];then
echo "no kill"
else
echo "kill"
id=`ssh root@$ip -p 26259 "ps -w " | grep "ssh -Nfg" | grep -v grep | awk '{print $1;}' `
echo $id
#杀死隧道进程
ssh root@$ip -p 22 "kill "$id
#
echo "$result" > ./Cip.log
fi
echo 'finish....'