分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

使用rinetd做端口转发

发布时间:2023-09-06 02:19责任编辑:彭小芳关键词:暂无标签
最近项目上遇到一个需要代理做多个端口转发的需求,考虑到需求只是做tcp的网络转发没有业务需求用nginx比较繁琐,就使用了rinetd,整理记录一下。

?一、安装rinetd软件
1、wget http://www.boutell.com/rinetd/http/rinetd.tar.gz ??#下载安装文件;
2、tar zvxf rinetd.tar.gz ???#解压下载到的文件;
3、cd rinetd/ ???#进入到解压目录;
4、sed -i ‘s/65536/65535/g‘ rinetd.c #将rinetd.c文件中65536修改为65535;
5、mkdir /usr/man ???#创建目录;
6、make&&make install ???#编译安装;
7、编译安装完成后就可以使用rinetd命令了。

二、修改配置文件配置端口映射的规则
1、修改配置文件,需要在/etc目录下创建rinetd.conf文件,并在文件中填写端口映射的规则:
配置文件映射规则格式如下
bindaddress ????????bindport ????????connectaddress ???????connectport
绑定的地址 ???????绑定的端口 ??????????连接的地址 ??????????连接的端口

[Source Address] ???[Source Port] ???[Destination Address] ???[Destination Port]
源地址 ???????????????????源端口 ???????????????目的地址 ??????????????????????目的端口

2、配置规则举例,比如将10.10.10.10的80端口映射为20.20.20.20的90端口则规则为:
10.10.10.10 80 20.20.20.20 90

3、创建配置文件并填写规则:
vim /etc/rinetd.conf ???#然后输入如下规则,保存退出即可;
10.10.10.10 80 20.20.20.20 90

三、启动rinetd服务:
rinetd -c /etc/rinetd.conf

四、配置开机启动
1、简单方法:
把“rinetd -c /etc/rinetd.conf”这条命令加到/etc/rc.local里面就可以开机自动运行

2、配置服务脚本:
vim /etc/init.d/rinetdServer ???#然后输如以下内容;

#!/bin/bash# The next lines are for chkconfig on RedHat systems.# chkconfig: 2345 86 10# description: Starts and stops xxx Server # The next lines are for chkconfig on SuSE systems.# /etc/init.d/rinetdServer#### BEGIN INIT INFO# Provides:xxxx# Short-Description: Starts and stops rinetd Server # Description: Starts and stops rinetd Server ### END INIT INFOrinetd_pid=`ps -ef | grep rinetd.conf | grep -v grep | awk ‘{print $2}‘`proc_count=`ps -ef | grep rinetd.conf | grep -v grep | wc -l`proc_run="rinetd server is running......"proc_stop="rinetd server is stopped!"rinetd_conf=/etc/rinetd.confcase $1 in ???start) ?????????rinetd -c $rinetd_conf ??????????;; ???stop) ???????kill -9 $rinetd_pid ??????????;; ??restart) ??????????kill -9 $rinetd_pid ?????rinetd -c $rinetd_conf ???????????;; ????status) ??????????[ $proc_count -eq 1 ] && echo $proc_run || echo $proc_stop ????????????;; ?????????*) echo "$0 {start|stop|restart|status}" ????????????exit 4 ????????????;;esac

五、增加脚本与进程检查
1、vim /opt/shell/rinetdCheck  #添加如下代码,保存;

#!/bin/bashsource /etc/profileproc_count=`ps -ef | grep rinetd.conf | grep -v grep | wc -l`start_file=/etc/init.d/rinetdServer[ $proc_count -ne 1 ] && $start_file start

2、crontab -e配置计划任务每5分钟运行一次脚本
/5 * /opt/shell/rinetdCheck >/dev/null 2>&1

使用rinetd做端口转发

原文地址:http://blog.51cto.com/rongshu/2308789

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved