通过公网IP主机建立ssh隧道

环境描述

  • hostA: 有公网IP的linux主机
  • hostB: 私有路由器后端无公网IPlinux主机,能够ssh连接到hostA
  • hostC: 个人pc机

隧道创建步骤

  • step1 在hostB上执行如下开通ssh tunnel
#!/bin/bash
createTunnel() {
    /usr/bin/ssh -f -N -R 10022:localhost:22 -L19922:HostA:22 HostA
    if [[ $? -eq 0 ]]; then
        echo Tunnel to HostA created successfully
    else
        echo An error occurred creating a tunnel to HostA. Return code: $?
    fi
}
/usr/bin/ssh -p 19922 localhost ls > /dev/null
if [[ $? -ne 0 ]]; then
    echo Creating new tunnel connection to HostA
    createTunnel
fi
  • step2 在hostC上连接到hostB
ssh -t HostA 'ssh localhost -p 10022'

references

posted @ 2016-07-06 10:26  forilen  阅读(1477)  评论(0编辑  收藏  举报