歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> CentOS 6 KVM添加網卡橋接口腳本

CentOS 6 KVM添加網卡橋接口腳本

日期:2017/2/28 14:50:45   编辑:Linux教程

腳本任務

1,判斷網卡配置文件名稱為eth*或者是em* ,如dell服務器網卡CentOS5為eth*,而CentOS6為em*。

2,存在幾個網卡就復制幾個原網卡配置文件做為橋配置文件,n的設置的數量是

3,如果超過4網卡則設置更高值,原配置文件是DHCP的改為static,用.*匹配行是存在CentOS5和6參數有引號的問題。

4, 生成虛擬機橋借口br*並一一對應,如br0對應eth0,br1對應eth1等等,並重啟網絡。

#! /bin/sh
mypath="/etc/sysconfig/network-scripts"
for((n=0;n<=4;n++))
do
if [ -f "$mypath/ifcfg-eth$n" ]
then
sed -i "s/^ONBOOT.*/ONBOOT=yes/" $mypath/ifcfg-eth$n
sed -i "s/^BOOTPROTO.*/BOOTPROTO=static/" $mypath/ifcfg-eth$n
cp -a $mypath/ifcfg-eth$n $mypath/ifcfg-br$n
sed -i "/^HWADDR.*/d" $mypath/ifcfg-br$n
sed -i "s/^DEVICE.*/DEVICE=br"$n"/" $mypath/ifcfg-br$n
sed -i "s/^TYPE.*/TYPE=Bridge/" $mypath/ifcfg-br$n
echo "BRIDGE=br$n">>$mypath/ifcfg-eth$n
echo "The device br$n<---->eth$n is add success !"
else
echo "The device eth$n not exist!"
fi
if [ -f "$mypath/ifcfg-em$n" ]
then
sed -i "s/^ONBOOT.*/ONBOOT=yes/" $mypath/ifcfg-em$n
sed -i "s/^BOOTPROTO.*/BOOTPROTO=static/" $mypath/ifcfg-em$n
cp -a $mypath/ifcfg-em$n $mypath/ifcfg-br$n
sed -i "/^HWADDR.*/d" $mypath/ifcfg-br$n
sed -i "s/^DEVICE.*/DEVICE=br"$n"/" $mypath/ifcfg-br$n
sed -i "s/^TYPE.*/TYPE=Bridge/" $mypath/ifcfg-br$n
echo "BRIDGE=br$n">>$mypath/ifcfg-em$n
echo "The device br$n<---->em$n is add success !"
else
echo "The device em$n not exist!"
fi
done
/etc/init.d/network restart

更多CentOS相關信息見CentOS 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=14

Copyright © Linux教程網 All Rights Reserved