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" <<EOF
TYPE=Bridge
BOOTPROTO=static
DEVICE=br0
ONBOOT=yes
DNS1=114.114.114.114
IPADDR=$ip
PREFIX=
NETMASK=255.255.255.0
GATEWAY=$gateway
EOF cat > "$interface_file1" <<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=$interface1
DEVICE=$interface1
ONBOOT=yes
IPADDR=0.0.0.0
PREFIX=
BRIDGE=br0
EOF cat > "$interface_file2" <<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=$interface2
DEVICE=$interface2
ONBOOT=yes
IPADDR=0.0.0.0
PREFIX=
BRIDGE=br0
EOF systemctl restart network #检查ip地址是否设置成功
res=`ip addr show br0 |grep -c "$ip"`
if [ "$res" -eq ];then
echo "ip地址设置失败" >&
return
fi #检查默认路由是不是gateway
res=`ip route |grep -c "default\s*via\s*$gateway\s*dev\s*br0"`
if [ "$res" -eq ];then
echo "网关设置失败" >&
return
fi
return
} set_bridge

执行格式:

./set_bridge.sh -i 192.168.4.72 -g 192.168.4.1

结果:

[root@localhost ~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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<link>
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<UP,BROADCAST,RUNNING,MULTICAST> 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<UP,BROADCAST,MULTICAST> 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<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
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

上一篇:老调重弹:对kvo的封装思路


下一篇:React问答小demo