rinetd -- internet ``redirection server''
Redirects TCP connections from one IP address and port to another. rinetd is a single-process server which handles any number of connections to the address/port pairs specified in the file /etc/rinetd.conf
. Since rinetd runs as a single process using nonblocking I/O, it is able to redirect a large number of connections without a severe impact on the machine. This makes it practical to run TCP services on machines inside an IP masquerading firewall. rinetd does not redirect FTP, because FTP requires more than one socket.
代码有点古老,但很短小、高效,对于解决这种问题来说是非常完美的。
下载地址: http://www.boutell.com/rinetd/
解压后,make && make install 就可以了
程序路径 /usr/sbin/rinetd
需要配置文件 /etc/rinetd.conf
配置文件格式很简单:
[Source Address] [Source Port] [Destination Address] [Destination Port]
在每一单独的行中指定每个要转发的端口。源地址和目的地址都可以是
主机名或IP 地址,IP 地址0.0.0.0 将rinetd 绑定到任何可用的本地IP
地址上:
0.0.0.0 8080 www.aslibra.com 80
0.0.0.0 3306 192.168.1.77 3306
0.0.0.0 88 127.0.0.1 80
直接运行程序就可以了看到占用端口服务了。
上面是把访问本机的8080端口的服务都迁移到www.aslibra.com的机器上,把访问本地的数据库的转移到192.168.1.77上,把访问本地88端口的指向80端口服务。
可是发现这个只是做了个代理的功能,比如最后一条得到的访问者IP:
REMOTE_ADDR 192.168.1.77
也就是没法获取到用户ip,这个比较可惜,但是对于IP访问的服务做迁移还是不错的,比如数据库服务,可以在保留很多程序的连接方式不变的情况下,直接切换数据库,也就是在主数据库出问题了,可以立刻用这种方法转换到备份数据库。
所以,服务器主备模式的情况还是可以备一个的,这样保证准时切换,可以做shell程序判断端口服务是否正常,如果实在不正常或者无法服务,就自动切换到备份的,多方便呀~
引用
当你的服务器要重新编号或物理位置变化而改变IP地址时你就能切实感受到这个工具的好处了,因为服务看上去就像保留在原始IP 地址(即便它们实际上完全是来自其他网络)上。如果仅仅是绑定大于1024的端口,那么rinetd就不需要以root账户来运行。这种小工具很值得放在身边,以便随时调用TCP 端口间接寻址。