手机版
你好,游客 登录 注册
背景:
阅读新闻

CentOS 6 KVM添加网卡桥接口脚本

[日期:2013-03-26] 来源:Linux社区  作者:songxj [字体:大 中 小]

脚本任务

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

linux
相关资讯       KVM  CentOS 6 KVM 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款