利用PHP和linux shell在ubuntu服务器实现自动端口转发


前言:

在用动态ip分配协议的网络环境下远程控制自己的主机最烦的就是ip会经常改变,而安装teamviewer这些软件又没有用rdp那么快速,所以经常开机后第一件事就是把ip地址记下来,然后再从其他机器远程进来。因为手头掌控着一台有固定ip地址的服务器,我想能不能通过这台服务器自动记录自己机器每次获得在ip,然后可以很方便地进行远程控制,只要机器是开着的。以前是在机器上写脚本在开机的时候上传ip地址到服务器的php网页,然后网页把ip存放在一个html文件里,要远程时再查看这个html文件来获取地址。最近我想能不能有更直接的方法,就是当自己机器把ip地址上传到服务器的时候,服务器就增加一条端口转发记录到iptables里,然后就可以通过服务器某个固定的端口来远程机器,这样子只要记住每台机器对应的端口就可以了。

网络结构:

具有固定ip地址的服务器一台,系统是ubuntu,以及同一局域网内通过dhcp获取地址的机器一台,系统为win7.

服务器端脚本

./iptables/iptables.sh:

#!/bin/bash
#This program will receive ipaddress and port and add to iptables
#receive to data: iptables.sh hostname destination
#hostname:name of the host
#destination: ipaddress,can add port at the end
#example:
#iptables.sh ddwrt:1.2.3.4:22
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#get hostname and destination
host=`echo $1 | cut -d":" -f1` #hostname of client
dest=`echo $1 | cut -d":" -f2` #ip of client
port=`echo $1 | cut -d":" -f3` #port of client
dest=$dest:$port

#add the new ip address to table.txt
awk '{if($1=="'$host'")$3="'$dest'";print $0}' table.txt > __tmp__
mv  ./iptables/__tmp__ ./iptables/table.txt

#scan table.txt and add the port to iptables
sudo iptables -t nat -F
while read line
do
 port=`echo $line | awk '{print $2}'`
 dest=`echo $line | awk '{print $3}'`
 ip=`echo $dest|cut -d":" -f1`
if [ "$port" != "port" ]; then
  sudo iptables -t nat -A PREROUTING -d 172.18.180.103 -p tcp -m tcp --dport $port -j DNAT --to-destination $dest
  sudo iptables -t nat -A POSTROUTING -p tcp -m tcp -d $ip -j SNAT --to-source 172.18.180.103
fi
done < ./iptables/table.txtdone < ./iptables/table.txt


在以上代码中,通过命令行参数传进两个参数,机器名字host和目的地址dest,其中host只是个代号,写在一个名字叫table.txt的文件中(下面会给出),而dest包括机器的ip地址和端口,如脚本说明里面的例子:

ddwrt是主机的名字,1.2.3.4:22就是目的地址了,这里是为一台ddwrt路由器做端口转发,以便通过22端口进行ssh。

赋值语句中的$1和$2分别表示命令行获取的第一个和第二个参数,如
./iptables.sh ddwrt 1.2.3.4:22

这里$1就是ddwrt,$2就是1.2.3.4:22

awk和mv两句是把名字叫host的一行记录的目的地址改成新上传的地址

while语句就是扫描/iptables/table.txt把里面的主机名对应的端口和目的地址加到iptables转发表中。

 

配置文件/iptables/table.txt:

name port destination
zebra 2001 172.18.217.192:3389
connatation 2002 172.18.217.3:3389
ddwrt 2004 8.2.3.4:22
raspberry_pi 2005 172.18.214.5:22


上图中就是用文本作为脚本的配置文件,该文件必须存在,否则脚本运行会有问题

访文件分三个字段,第一行是每个字段的说明,分别表示机器名字,机器在服务器上对应的端口,机器的ip地址和要转发到的端口

下面四行就是四条记录,其中名字为zebra和connatation的两机器是win7,转发到3389进行远程桌面,另外两台机器分别是路由器和树莓派,转发到22进行ssh。

服务器商php脚本./test_shell.php:

<?
//file:test_shell.php
//author:XieShundao
//date:2013.10.17
//descript:excute shell with user in root previlage,
//         make port transport from the server to dynamic ip client.
//         need the follow data in get methord:
//         dest: ip address of the client,can include port in the end
//         host: hostname of the client
//example: http://edclub.xicp.net/test_shell.php?host=ddwrt:172.18.217.182:22
//ref: http://blog.csdn.net/houqd2012/article/details/8249124


//add port transport to iptables
//$cmd="sudo iptables -t nat -A PREROUTING -d 222.200.180.103 -p tcp -m tcp --dport ".$_GET["port"]." -j DNAT --to-destination ".$_GET["ip"].":3389";

$cmd="/usr/bin/sudo /var/www/iptables/iptables.sh ".$_GET["host"];
 echo $cmd;
//excute the command
 exec($cmd,$output,$return);
?>


 

这里说明一个文件结构,该文件是放在/var/www文件夹即ubuntu默认的网站根目录下,而前面的table.txt和iptables.sh是放在该文件来下一个叫做iptables的文件夹下,并且在php里用root权限运行脚本,参考了:http://blog.csdn.net/houqd2012/article/details/8249124

然后在各个机器下写脚本程序通过get方式把自己的ip和机器名字上传上来就可以了。不过有一点要注意:如果有使上传的ip写进iptables里,该机器名字必须要在table.txt里。

上传的格式为:

http://your_server/test_shell.php?dest=172.18.3.4:22&host=ddwrt

其中your_server为你服务器的ip或域名

对照table.txt可知ddwrt对应的端口为2004,所以以后要ssh该机器可以直接通过ssh你服务器的2004端口进行操作。

本地机器脚本:

以下是我在win7下写的一个用curl来上传ip地址的bat程序,当然该程序要跟软件curl放在同一文件夹下:

@echo off

rem get ip address of eth0 and store in varialbe ip
  ipconfig |find "IPv4 地址 . . . . . . . . . . . . : 172." >ip.txt
  set /p IP=<ip.txt>nul
  set IP=%IP:~37,16%

  set SERVER="172.18.180.103/test_shell.php?host=ddwrt:%IP%:22"

  echo -------------------------upload the new data-----------------------------
rem upload the new data
  C:\Users\zebra\Desktop\curl-7.17.0\curl  %SERVER%
  echo ..
  echo ---------------------------------end-------------------------------------
pause


其中pause是为了执行完不立即关闭窗口,可以方便调试。

在linux下获取eth0的ip可以用以下方法:

IP=`ifconfig eth0 |grep "inet addr" |cut -d: -f2 |cut -d" " -f1`


 

而上传什么的也类似,在此就不贴出来了。

猜你喜欢

转载自blog.csdn.net/zebra2011/article/details/12856411