一、概述
目前常用的端口转发工具有rinetd、Haproxy、iptables、Socat前面2种只能转发TCP,后面TCP/UDP都可以转发。如果是需要udp转发的话,只能选择iptables、Socat。iptables方面的转口转发配置这里就不多讲了,网上资料一大堆。下面分享一下Socat使用和配置。
![b252b1ad59236f8dbe548d9016000220.png](https://img-blog.csdnimg.cn/img_convert/b252b1ad59236f8dbe548d9016000220.png)
二、安装配置
Socat可以通过rpm包的方式安装,然后配置,下面分享一个更简单的方法,一键部署安装部署脚本。
2.1 系统要求及说明
系统要求:支持CentOS 6+ 、Debian 7+、Ubuntu 14+。
脚本说明:脚本默认开启UDP、TCP转发,带开机自启功能,且一次只能转发单个端口,如果想转发多个端口请重复运行本脚本。
2.2 安装配置
1、脚本安装
wget https://raw.githubusercontent.com/iiiiiii1/Socat/master/socat.sh && bash socat.sh
注:以root用户执行上面的命令
![2b868f2da5100ed38118a42a79265311.png](https://img-blog.csdnimg.cn/img_convert/2b868f2da5100ed38118a42a79265311.png)
2、输入的参数说明:
- 输入本地端口:指定本机未被占用的端口,用户可自定义。
- 输入远程端口:远程主机的真实的端口。
- 远程主机:远程主机的真实的IP地址。
![430c207c64ce8b5886d783116a8ee3f8.png](https://img-blog.csdnimg.cn/img_convert/430c207c64ce8b5886d783116a8ee3f8.png)
3、配置保存位置
配置保存在/etc/rc.local文件中。
[root@localhost128 ~]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /usr/local/inception/bin/Inception --defaults-file=/etc/inc.cnf &
nohup socat TCP4-LISTEN:13022,reuseaddr,fork TCP4:192.168.42.130:22 >> /root/socat.log 2>&1 &
nohup socat -T 600 UDP4-LISTEN:13022,reuseaddr,fork UDP4:192.168.42.130:22 >> /root/socat.log 2>&1 &
![6f04c935d491d53490112728dcc33322.png](https://img-blog.csdnimg.cn/img_convert/6f04c935d491d53490112728dcc33322.png)
2.3 软件卸载
yum remove socat
2.4 使用展示
1、环境说明
- 192.168.42.128:配置socat的机器,监听本地端口:13022
- 192.168.42.130:跳板远程连接的机器。ssh的监听端口为:22
2、配置
![430c207c64ce8b5886d783116a8ee3f8.png](https://img-blog.csdnimg.cn/img_convert/430c207c64ce8b5886d783116a8ee3f8.png)
3、通过跳板连接到远程机器
![a5a39afe8dce367a4eb666d8af7b63a6.png](https://img-blog.csdnimg.cn/img_convert/a5a39afe8dce367a4eb666d8af7b63a6.png)
![e9c4d7d628321a6a71b4d4e05d19eac7.png](https://img-blog.csdnimg.cn/img_convert/e9c4d7d628321a6a71b4d4e05d19eac7.png)
通过上图可以看到连接成功
三、小结
1、socat是继rinetd、Haproxy、iptables之后,端口转发和跳板的又一神器,同时支持tcp和udp。通过此一键配置脚本配置,非常简单便捷。
觉得有用的朋友多帮忙转发哦!后面会分享更多精彩的内容,感兴趣的朋友可以关注下~
![77ca90d78fbda5cf9f1e506d987e996a.gif](https://img-blog.csdnimg.cn/img_convert/77ca90d78fbda5cf9f1e506d987e996a.gif)