使用rinetd做端口轉發

  最近項目上遇到一個需要代理做多個端口轉發的需求,考慮到需求只是做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 INFO

rinetd_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.conf

case $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/bash

source /etc/profile

proc_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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章