hanxi / blog

涵曦的博客
https://blog.hanxi.cc
56 stars 5 forks source link

使用 ssh 借助外网机器实现内网穿透实现远程登录 #8

Open hanxi opened 9 years ago

hanxi commented 9 years ago

在内网机器上执行下面的命令

ssh -p22 -f -N -R 0.0.0.0:2222:localhost:22 root@127.0.0.1
ssh -p2222 root@localhost

crontab 每分钟执行一次 auto_remote.sh

*/1 * * * * bash /root/auto_remote.sh >>/dev/null 2>&1

auto_remote.sh

#/bin/bash

user=root
ip=*.*.*.*   # 改成你的外网 IP
port=23111
cport=`echo $((RANDOM%5000+5000))`
mport=22
cmd="ssh -p$port -f -N -R $cport:localhost:$mport $user@$ip"
reg="ssh -p$port -f -N -R [0-9]\{4\}:localhost:$mport $user@$ip"
isrun=`ps -ef | grep "$reg" | grep -v grep`
if [ "$isrun"x == "x" ]; then
    echo "notrun"
    $cmd
    echo "$cmd"
else
    echo "isrun"
    echo "$isrun"
fi
hanxi commented 6 years ago
autossh -M 20001 \
-fN -o "PubkeyAuthentication=yes" \
-o "StrictHostKeyChecking=false" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" \
-R a_a_a_a:20000:localhost:22 \
-p 8383 remote_user@a_a_a_a

https://blog.windrunner.me/sa/reverse-ssh.html