Linux 端口转发

iptables 端口转发

iptables -t nat -A PREROUTING  -d 114.114.114.114 -p tcp -m tcp --dport 3389 -j DNAT --to-destination 10.0.1.123:3389
iptables -t nat -A POSTROUTING -d 10.0.1.123 -p tcp -m tcp --dport 3389 -j SNAT --to-source 10.0.0.254
#外网接口地址114.114.114.114 内网接口地址10.0.0.254,内部服务器10.0.1.123转发端口3389

rinetd

编译安装

wget http://www.boutell.com/rinetd/http/rinetd.tar.gz
tar xzvf rinetd.tar.gz
cd rinetd
mkdir -p /usr/man/man8
make &&make install

配置文件 cat /etc/rinetd.conf

 0.0.0.0 9001 192.168.1.1 3306
 0.0.0.0 9002 192.168.1.2 80
#绑定的地址、绑定的端口、转发的地址 、转发的端口

启动rinetd服务

rinetd -c /etc/rinetd.conf

SSH

ssh转发命令

ssh -N -L 0.0.0.0:1521:10.0.15.131:1521 [email protected] -f
#通过ssh通道,将10.0.15.131的1521端口映射到本机的1521端口,参数-f表示在后台运行

ssh转发脚本

#!/bin/bash
HOST=192.168.21.103
SSH_SERVER="[email protected] -p 24242"
declare -a T
declare -a D

T[0]="-L $HOST:33900:192.168.102.90:3389"
T[1]="-L $HOST:33910:192.168.102.93:3389"



function start()
{
pidwl=$(ps -ef|grep -v grep|grep "$SSH_SERVER"|wc -l)
if [ "X$pidwl" == "X0" ]; then
    nohup /usr/local/bin/sshpass -p 'password' ssh -o ServerAliveInterval=6 -2 -f -nNT ${T[*]} $SSH_SERVER &
    echo "start $SSH_SERVER"
fi

echo `date +%Y%m%d%H%M%S`
}

function stop()
{
    ps -ef|grep -v grep|egrep "ServerAliveInterval"|awk '{print $2}'|xargs kill -9
    echo "stop Transmit"
}


case "$1" in
    start)
    $1
    ;;
    stop)
    $1
    ;;
    restart)
    stop
    start
    ;;
esac

nginx (stream模块)

nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信。Module ngx_stream_core_module cat vhost/port_forword.conf

stream {
    # 添加socket转发的代理
    upstream manager {
        hash $remote_addr consistent;
        # 转发的目的地址和端口
        server 10.0.10.10:3389 max_fails=3 fail_timeout=30s;
    }

    
    server {
    listen 9001;
    proxy_connect_timeout 1s;
    proxy_timeout 3s;
    proxy_pass manager;
    }
}
# 通过nginx 9001端口可以访问内网Windows跳板机。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章