由于近来功夫网发力,在客户那又封了22端口。令到在客户那无法使用ssh代理。所以想使用Linux的端口重定向来解决这个问题。我的VPS操作系统是debian。
搜索redir的安装包:
apt-cache search redir
redir - Redirect TCP connections

安装redir:
apt-get install redir
debian:/tmp# which redir/usr/bin/redir

重定向前的端口情况:

debian:/tmp# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.0.5:22 0.0.0.0:* LISTEN 1166/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1273/exim4
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2059/0
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 5443/1
tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 5667/2

redir把主机192.168.0.5的22端口重定向到了本地的80端口

debian:/tmp# redir --lport=80 --cport=22 --caddr=192.168.0.5 --debug
target is 192.168.0.5
target IP address is 192.168.0.5
target port is 22
local IP is default
top of accept loop
peer IP is 192.168.0.15
peer socket is 58651
top of accept loop
connected to 192.168.0.5
Entering copyloop() - timeout is 0
Leaving main copyloop
copyloop - sockets shutdown and closed
copyloop - connect time: 7 seconds
copyloop - transfer in: 1652 bytes
copyloop - transfer out: 1081 bytes

redir端口重定向后的端口情况

debian:~# netstat -anpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5698/redir
tcp 0 0 192.168.0.5:22 0.0.0.0:* LISTEN 1166/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1273/exim4
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2059/0
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 5443/1
tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 6022/2

将redir放到后台运行把主机192.168.0.5的22端口重定向到了本地的80端口,把日志写到syslog里面 标签为redirSSH

debian:/tmp# redir --lport=80 --cport=22 --caddr=192.168.0.5 --syslog --name=redirSSH &
[1] 5698

grep一下syslog的日志:

debian:~# grep "redirSSH" /var/log/syslog
Mar 19 23:05:49 debian redirSSH[5710]: accepted connect from 192.168.0.15
Mar 19 23:05:49 debian redirSSH[5710]: connecting 192.168.0.15/34862 to 192.168.0.5/22
Mar 19 23:05:49 debian redirSSH[5710]: disconnect 0 secs, 50 in 153 out
Mar 19 23:05:50 debian redirSSH[5713]: accepted connect from 192.168.0.15
Mar 19 23:05:50 debian redirSSH[5713]: connecting 192.168.0.15/24175 to 192.168.0.5/22
Mar 19 23:05:50 debian redirSSH[5713]: disconnect 0 secs, 50 in 281 out
Mar 19 23:06:01 debian redirSSH[5717]: accepted connect from 192.168.0.15
Mar 19 23:06:01 debian redirSSH[5717]: connecting 192.168.0.15/23993 to 192.168.0.5/22
Mar 19 23:06:01 debian redirSSH[5717]: disconnect 0 secs, 50 in 281 out