linux 双网卡双网桥,centos 7 双网卡建网桥脚本实现

#!/bin/bash

interface1=`ls /sys/class/net|grep en|awk ‘NR==1{print}‘`

interface2=`ls /sys/class/net|grep en|awk ‘NR==2{print}‘`

interface_file1="/etc/sysconfig/network-scripts/ifcfg-$interface1"

interface_file2="/etc/sysconfig/network-scripts/ifcfg-$interface2"

br0_file="/etc/sysconfig/network-scripts/ifcfg-br0"

echo $interface_file1

echo $interface_file2

echo br0_file

while getopts "i:g:" opt; do

case $opt in

i)

ip=$OPTARG

;;

g)

gateway=$OPTARG

;;

\?)

;;

esac

done

echo $ip

echo $gateway

function set_bridge(){

#写网卡配置文件

cat > "$br0_file" <

TYPE=Bridge

BOOTPROTO=static

DEVICE=br0

ONBOOT=yes

DNS1=114.114.114.114

IPADDR=$ip

PREFIX=24

NETMASK=255.255.255.0

GATEWAY=$gateway

EOF

cat > "$interface_file1" <

TYPE=Ethernet

BOOTPROTO=static

NAME=$interface1

DEVICE=$interface1

ONBOOT=yes

IPADDR=0.0.0.0

PREFIX=24

BRIDGE=br0

EOF

cat > "$interface_file2" <

TYPE=Ethernet

BOOTPROTO=static

NAME=$interface2

DEVICE=$interface2

ONBOOT=yes

IPADDR=0.0.0.0

PREFIX=24

BRIDGE=br0

EOF

systemctl restart network

#检查ip地址是否设置成功

res=`ip addr show br0 |grep -c "$ip"`

if [ "$res" -eq 0 ];then

echo "ip地址设置失败" >&2

return 254

fi

#检查默认路由是不是gateway

res=`ip route |grep -c "default\s*via\s*$gateway\s*dev\s*br0"`

if [ "$res" -eq 0 ];then

echo "网关设置失败" >&2

return 253

fi

return 0

}

set_bridge

执行格式:

./set_bridge.sh -i 192.168.4.72 -g 192.168.4.1

结果:

[[email protected] ~]# ifconfig

br0: flags=4163 mtu 1500

inet 192.168.4.72 netmask 255.255.255.0 broadcast 192.168.4.255

inet6 fe80::20e:c6ff:fec5:e66f prefixlen 64 scopeid 0x20

ether 00:0e:c6:c5:e6:6f txqueuelen 0 (Ethernet)

RX packets 949676 bytes 188597922 (179.8 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 317931 bytes 71904040 (68.5 MiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

enp0s20u5: flags=4163 mtu 1500

ether 00:0e:c6:c5:e6:6f txqueuelen 1000 (Ethernet)

RX packets 1072283 bytes 200438143 (191.1 MiB)

RX errors 0 dropped 114 overruns 0 frame 0

TX packets 317930 bytes 74447998 (70.9 MiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

enp3s0: flags=4099 mtu 1500

ether d0:17:c2:8c:47:7d txqueuelen 1000 (Ethernet)

RX packets 0 bytes 0 (0.0 B)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 0 bytes 0 (0.0 B)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536

inet 127.0.0.1 netmask 255.0.0.0

inet6 ::1 prefixlen 128 scopeid 0x10

loop txqueuelen 0 (Local Loopback)

RX packets 196 bytes 29677 (28.9 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 196 bytes 29677 (28.9 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

原文:http://www.cnblogs.com/weifeng1463/p/7549951.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值