bridge-mode.sh

為了建立 bridge環境,寫了一 script,來讓它開機時啟動。

但後來也得知 network-script目錄裡的 ifcfg等檔可支援 brige

  1. #!/bin/bash
  2. # fixed 2006/05/22 by cross@ssorc.tw
  3. #
  4. # purpose: set up bridge mode for linux.
  5. #
  6. # 1. first need to set up ifcfg-ethx.
  7. # 2. modprobe bridge
  8. # 3. apt-get install bridge-utils
  9. #
  10.  
  11. # define external device.
  12. exif="eth0"
  13.  
  14. # define internal device.
  15. inif="eth1"
  16.  
  17. # define bridge name and ip/mask.
  18. brname="br0"
  19. brip="10.1.1.33"
  20. brmask="255.255.255.0"
  21.  
  22. # define external gateway ip
  23. gw="10.1.1.254"
  24.  
  25. # add brX alias
  26. braliasip="10.1.1.44 10.1.1.55"
  27. braliasmask="255.255.255.0"
  28.  
  29. # source function library
  30. . /etc/rc.d/init.d/functions
  31.  
  32. # ------- check ifcfg-ethx setting - START line -------
  33. cd /etc/sysconfig/network-scripts/
  34. ls ifcfg-eth* | sed "s/:.*//" | uniq | cut -d'h' -f2 |
  35. while read ifdev; do
  36.          if [ -f ifcfg-$exif ] && [ -f ifcfg-$inif ]; then
  37.                  echo "ifdev setting ok!"
  38.          else
  39.                  if [ $ifdev = `echo $exif | cut -d'h' -f2` ]; then
  40.                          echo "warning !! only the ifcfg-eth$ifdev"
  41.                          echo "warning !! plz set up ifcfg-$inif"
  42.     echo ""
  43.                          exit 1
  44.                  else
  45.                          echo "warning !! only the ifcfg-eth$ifdev"
  46.                          echo "warning !! plz set up ifcfg-$exif"
  47.     echo ""
  48.                          exit 1
  49.                  fi
  50.          fi
  51. done
  52. # ------- check ifcfg-ethx setting - END line -------
  53.  
  54. # ------- start to set up bridge mode -------
  55. start() {
  56.          echo "starting bridge mode: "
  57.          ifdown $exif
  58.          ifdown $inif
  59.          brctl addbr $brname
  60.          brctl stp $brname off
  61.          brctl addif $brname $exif
  62.          brctl addif $brname $inif
  63.          ifconfig $exif 0.0.0.0 up
  64.          ifconfig $inif 0.0.0.0 up
  65.          ifconfig $brname $brip netmask $brmask up
  66.          nowgw1=`route | grep default | awk '{print $2}'`
  67.          if [ -z $nowgw1 ]; then
  68.                  route add default gw $gw
  69.          else
  70.                  :
  71.          fi
  72.  
  73.         #
  74.          echo 1 > /proc/sys/net/ipv4/ip_forward
  75.  
  76.         # ------- set up br0:XX -------
  77.          if [ -z $braliasip ]; then
  78.                  :
  79.          else
  80.                  ipcounts=`echo $braliasip | wc -w`
  81.                  for ((n=1; n<=$ipcounts; n++))
  82.                  do
  83.                          ipaddr=`echo $braliasip | awk '{print $'$n'}'`
  84.                          ipalias=`echo $ipaddr | cut -d'.' -f4`
  85.                          ifconfig $brname:$ipalias $ipaddr netmask $braliasmask
  86.                  done
  87.          fi
  88. }
  89.  
  90. stop() {
  91.          echo "stopping bridge mode: "
  92.          brctl delif $brname $exif
  93.          brctl delif $brname $inif
  94.          ifconfig $brname down
  95.          brctl delbr $brname
  96.          ifup $exif
  97.          ifup $inif
  98.          nowgw2=`route | grep default | awk '{print $2}'`
  99.          if [ -z $nowgw2 ]; then
  100.                  route add default gw $gw dev $exif
  101.          else
  102.                  :
  103.          fi
  104. }
  105.  
  106. status() {
  107.          echo "ifocnfig status: "
  108.          ifconfig $exif | grep "inet addr"
  109.          ifconfig $inif | grep "inet addr"
  110.          ifconfig $brname | grep "inet addr"
  111.          echo ""
  112.          echo "routing status: "
  113.          route -n
  114. }
  115.  
  116. case "$1" in
  117.          start)
  118.                  start
  119.                  ;;
  120.          stop)
  121.                  stop
  122.                  ;;
  123.          restart)
  124.                  stop
  125.                  start
  126.                  ;;
  127.          status)
  128.                  status
  129.                  ;;
  130.          *)
  131.                  echo "Usage: $0 {start|stop|status}"
  132.                  exit 1
  133. esac
  134.  
  135. exit $?

評論: 0 | 引用: 0 | 閱讀: 1595 | 列印 | 文件 | 轉發

發表評論
暱 稱: 密 碼:
網 址: E - mail:
驗證碼: 驗證碼圖片 選 項:
頭 像:
內 容:
  • 粗體
  • 斜體
  • 底線
  • 插入圖片
  • 超連結
  • 電子郵件
  • 插入引用
  • 表情符號