08
2019
05
10:51:25

实现两个机房内网连通



推荐点击下面图片,通过本站淘宝优惠价购买:

image.png

链接:https://www.jianshu.com/p/4c44cff94040 

 这篇文章讲的是两个IDC机房之间内网互相打通,让A机房的所有服务器能连接到B机房所有服务器(至于B机房的所有服务器主动联通A机房的服务器,如果使用IP隧道的方式很好实现,路由策略配置好后就能互通了,而vpn的方式需要B机房一台服务器拨号到A机房的vpn服务器,然后再添加路由策略,当然,以上iptables做源地址转发都是少不了的)。以下文章,将分别用pptp和ip隧道两种方式来说明如何打通两个机房所有服务器的内网,我推荐IP隧道的方式,因为想做双机房互通比VPN要方便。

一、pptp方式

总体的思路:

①通过隧道让A机房隧道服务器能连接到B机房隧道服务器的内网IP。

②通过路由让A的隧道服务器能连通B机房所有服务器的内网。

③A机房的隧道服务器添加源地址转换策略。

④A机房的所有服务器添加网关,网关填写为A的隧道服务器内网地址。

具体步骤:

A机房:eth0(42.62.80.0/28)eth1(192.168.1.0/24)

B机房:eth0(58.215.41.0/28)eth1(192.168.1.0/24)

A机房隧道服务器IP:42.62.80.45192.168.1.45

B机房隧道服务器IP:58.215.41.148192.168.1.148

1、隧道服务器部署

pptp或者openvpn,部署VPN做隧道服务器,本文不具体讲VPN服务器和客户端拨号的部署方式。我vpn拨号成功后客户端会分配到一个192.168.1.21的IP。

2、通过路由让A的隧道服务器能连通B机房所有服务器的内网

pptp的方式,我只实现了A的隧道服务器能连通B机房所有服务器的内网,理论上反过来也是能做到的,不过我没这个需求就没测试。

下面是路由策略,(理论基础就是到目的IP地址通过网关设备(ppp0))

#!/bin/bash

IPList="

192.168.1.149

192.168.1.150

192.168.1.151

192.168.1.152

192.168.1.153

"

for i in $IPList

do

/sbin/routeadd $i dev ppp0

done

路由添加后可以测试从192.168.1.45上测试到以上脚本中内网IP列表的连通性。

3、pptp服务器添加源地址转换策略

#!/bin/bash

#auth dongbo

#LAN="192.168.15.0/24"

LAN="192.168.1.0/24"

#空格分隔IP列表

office="210.22.151.242 116.193.48.163"

#http://linux.chinaitlab.com/command/846185.html

#每次重启这个脚本时,最好清除以前所设的规则

CLEAR_IPT () {

/sbin/iptables -F

/sbin/iptables -X

/sbin/iptables -Z

/sbin/iptables -F -t nat

/sbin/iptables-X -t nat

/sbin/iptables -Z -t nat

/sbin/iptables -P INPUTDROP

/sbin/iptables -P OUTPUTACCEPT

/sbin/iptables -P FORWARD ACCEPT

}

#Iptables need to load module

MOUDLES_ADD () {

echo"1" > /proc/sys/net/ipv4/tcp_syncookies

echo"1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

}

#Secure function

secure () {

/sbin/iptables -A INPUT -i lo -j ACCEPT

/sbin/iptables -A INPUT -s $LAN -j ACCEPT

foroffice_ip in $office;do

/sbin/iptables -A INPUT -s $office_ip -j ACCEPT

done

/sbin/iptables -A INPUT -p icmp --icmp-type echo-request -m limit--limit 1/s --limit-burst 1 -j ACCEPT

/sbin/iptables -A INPUT -p icmp --icmp-type echo-request -j DROP

/sbin/iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

/sbin/iptables -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -jACCEPT

}

ssh() {

#只允许在LAN中使用SSH连接

/sbin/iptables -A INPUT -s $LAN -p tcp --dport 22 -j ACCEPT

/sbin/iptables -A INPUT -s 210.22.151.242 -p tcp --dport 22 -j ACCEPT

/sbin/iptables -A INPUT -p tcp --dport 4811 -j ACCEPT

}

vpn() {

/sbin/iptables -A INPUT-p tcp--dport 1723 -j ACCEPT

}

sdnat() {

/sbin/iptables -t nat -A POSTROUTING -s 192.168.1.44 -j SNAT --to-source192.168.1.21

/sbin/iptables -t nat -A POSTROUTING -s 192.168.1.36 -j SNAT --to-source192.168.1.21

/sbin/iptables -t nat -A POSTROUTING -s 192.168.1.37 -j SNAT --to-source192.168.1.21

/sbin/iptables -t nat -A POSTROUTING -s 192.168.1.38 -j SNAT --to-source192.168.1.21

/sbin/iptables -t nat -A POSTROUTING -s 192.168.1.41 -j SNAT --to-source192.168.1.21

/sbin/iptables -t nat -A POSTROUTING -s 192.168.1.42 -j SNAT --to-source192.168.1.21

/sbin/iptables -t nat -A POSTROUTING -s 192.168.1.43 -j SNAT --to-source192.168.1.21

}

CLEAR_IPT

MOUDLES_ADD

secure

ssh

vpn

sdnat

其中,最重要的就是sdnat这一段,这是将A机房的所有服务器做源地址转发。可能对于源地址转发和目的地址转发有疑问的会问为什么是源地址转发而不是目的地址转发。其实这里就像是公司内网的pc需要访问外网一样,pc设置一个网关IP,在网关设备上做源地址转换,把原来的源地址为pc机的内网地址转换成网关设备的外网地址你才能通过网关设备连接到外网,毕竟外网是靠外网IP来通信的。

4、在A机房其他服务器上配置路由,网关为隧道服务器的内网地址m)?_???

route add -net 192.168.1.149 gw 192.168.1.21

route add -net 192.168.1.150 gw 192.168.1.21

route add -net 192.168.1.151 gw 192.168.1.21

route add -net 192.168.1.152 gw 192.168.1.21

route add -net 192.168.1.153 gw 192.168.1.21

至此,可以测试A机房是否能联通B机房的所有内网IP了。


?二、IP隧道的方式(源地址转发很关键)

总体的思路:

1、通过隧道让A机房隧道服务器与B机房隧道服务器的内网互通。

2、通过源地址转发和路由让A机房隧道服务器和B机房隧道服务器能分别ping通对方机房其他服务器的内网IP。注意,源地址转发和路由都需要配置后才行。

3、A(B)机房的所有服务器添加网关,网关填写为A(B)的隧道服务器内网地址。

具体步骤:

A机房:eth0(42.62.80.0/28)eth1(192.168.1.0/24)

B机房:eth0(58.215.41.0/28)eth1(192.168.1.0/24)

A机房隧道服务器IP:42.62.80.44192.168.1.44

B机房隧道服务器IP:58.215.41.157192.168.1.157

1、隧道服务器部署

A机房隧道服务器

modprobe ip_gre

#卸载模块modprobe -r ip_gre,查看lsmod |grep ip_gre

ip tunnel add tun0 modegre local 42.62.80.44 remote 58.215.41.157

ip link set tun0 up

ifconfig tun0192.168.1.21 netmask 255.255.255.0#虚拟IP

ip route add 192.168.1.22dev tun0

iproute add 192.168.1.157 dev tun0

B机房隧道服务器

modprobe ip_gre

#卸载模块modprobe -r ip_gre,查看lsmod |grep ip_gre

ip tunnel add tun0 modegre local 58.215.41.157 remote 42.62.80.44

ip link set tun0 up

ifconfig tun0192.168.1.22 netmask 255.255.255.0#虚拟IP

ip route add 192.168.1.21dev tun0

ip route add 192.168.1.44 dev tun0

    以上命令执行完毕后,会在两台服务器上启动一个叫tun0的网络。最后两行是分别配置到对方路由,可以测试是否能ping通对方隧道服务器。隧道是没加密的,需要加密可参考一下上面的第二个链。

2、添加源地址转换和路由

A机房隧道服务器路由

#!/bin/bash

IPList="

192.168.1.148

192.168.1.149

192.168.1.150

192.168.1.151

192.168.1.152

192.168.1.153

192.168.1.154

"

add() {

for i in $IPList

do

/sbin/route add $i devtun0

done

}

del() {

for i in $IPList

do

/sbin/route del $i devtun0

done

}

case $1 in

add|del) $1;;

*)echo "Error!!!exec like:$0 add";;

esac

A机房隧道服务器源地址转发

#!/bin/bash

#auth dongbo

#http://linux.chinaitlab.com/command/846185.html

#每次重启这个脚本时,最好清除以前所设的规则

CLEAR_IPT () {

/sbin/iptables -F

/sbin/iptables -X

/sbin/iptables -Z

/sbin/iptables -F -t nat

/sbin/iptables -X -t nat

/sbin/iptables -Z -t nat

/sbin/iptables -PINPUTACCEPT

/sbin/iptables -POUTPUTACCEPT

/sbin/iptables -PFORWARD ACCEPT

#/sbin/iptables -t nat -PPREROUTINGACCEPT

#/sbin/iptables -t nat -PPOSTROUTING ACCEPT

#/sbin/iptables -t nat -POUTPUTACCEPT

}

sdnat() {

#兆维机房服务器通过源地址转发将源地址从192.168.1.36(等)封装为192.168.1.21,IP为192.168.1.21便能联通到无锡机房内网。

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.36 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.37 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.38 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.39 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.40 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.41 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.42 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.45 -j SNAT --to-source 192.168.1.21

#来自无锡机房的数据通过网关192.168.1.22(无锡机房网关)到达兆维机房时将源地址修改为192.168.1.21,这样才能到达需要的目的地址(192.168.1.36等)。ip隧道方式时这一步很关键,否则从隧道服务器上无法ping通另一个机房的除网关外其他服务器内网IP。

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.22 -d 192.168.1.36 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.22 -d 192.168.1.37 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.22 -d 192.168.1.38 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.22 -d 192.168.1.39 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.22 -d 192.168.1.40 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.22 -d 192.168.1.41 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.22 -d 192.168.1.42 -j SNAT --to-source 192.168.1.21

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.22 -d 192.168.1.45 -j SNAT --to-source 192.168.1.21

}

CLEAR_IPT

sdnat

B机房隧道服务器路由

#!/bin/bash

IPList="

192.168.1.36

192.168.1.37

192.168.1.38

192.168.1.39

192.168.1.40

192.168.1.41

192.168.1.42

"

add() {

for i in $IPList

do

/sbin/route add $i devtun0

done

}

del() {

for i in $IPList

do

/sbin/route del $i devtun0

done

}

case $1 in

add|del) $1;;

*)echo "Error!!!exec like:$0 add";;

esac

B机房隧道服务器源地址转发

#!/bin/bash

#auth dongbo

#每次重启这个脚本时,最好清除以前所设的规则

CLEAR_IPT () {

/sbin/iptables -F

/sbin/iptables -X

/sbin/iptables -Z

/sbin/iptables -F -t nat

/sbin/iptables -X -t nat

/sbin/iptables -Z -t nat

/sbin/iptables -PINPUTACCEPT

/sbin/iptables -POUTPUTACCEPT

/sbin/iptables -PFORWARD ACCEPT

#/sbin/iptables -t nat -PPREROUTINGACCEPT

#/sbin/iptables -t nat -PPOSTROUTING ACCEPT

#/sbin/iptables -t nat -POUTPUTACCEPT

}

sdnat() {

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.148 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.149 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.150 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.151 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.152 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.153 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.154 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.21 -d 192.168.1.148 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.21 -d 192.168.1.149 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.21 -d 192.168.1.150 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.21 -d 192.168.1.151 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.21 -d 192.168.1.152 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -APOSTROUTING -s 192.168.1.21 -d 192.168.1.153 -j SNAT --to-source 192.168.1.22

/sbin/iptables -t nat -A POSTROUTING -s192.168.1.21 -d 192.168.1.154 -j SNAT --to-source 192.168.1.22

}

CLEAR_IPT

sdnat

3、A(B)机房其他服务器添加到对方机房的内网网关

A机房其他服务器添加网关(比如在192.168.1.36上执行后才能让192.168.1.36连通到B机房内网)

A机房

#!/bin/bash

IPList="

192.168.1.148

192.168.1.149

192.168.1.150

192.168.1.151

192.168.1.152

192.168.1.153

192.168.1.154

"

add() {

for i in $IPList

do

/sbin/route add $i gw192.168.1.21

done

}

del() {

for i in $IPList

do

/sbin/route del $i gw192.168.1.21

done

}

case $1 in

add|del) $1;;

*)echo "Error!!!exec like:$0 add";;

esac

B机房

#!/bin/bash

IPList="

192.168.1.36

192.168.1.37

192.168.1.38

192.168.1.39

192.168.1.40

192.168.1.41

192.168.1.42

"

add() {

for i in $IPList

do

/sbin/route add $i gw192.168.1.22

done

}

del() {

for i in $IPList

do

/sbin/route del $i gw192.168.1.22

done

}

case $1 in

add|del) $1;;

*)echo "Error!!!exec like:$0 add";;

esac







本文链接:https://hqyman.cn/post/658.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:





休息一下,本站随机推荐观看栏目:


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

您的IP地址是: