linux 靜態ip設置shell版本

      參照網上設置linux 靜態ip的教程,爲了練習相應shell腳本,本人特爲之編寫了一個自動化設置靜態ip的腳本,相應設置信息均以shell命令完成,如需改動也可以再在腳本里改動相應內容,具體shell代碼如下:

 

  1. #! /bin/sh - 
  2.  
  3. # the shell used for redhat linux or redhat-based linux os 
  4. cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOF 
  5. DEVICE=eth0 
  6. BOOTPROTO=static 
  7. ONBOOT=yes 
  8. IPADDR=192.168.94.133 
  9. NETMASK=255.255.255.0 
  10. GATEWAY=192.168.94.1 
  11. EOF 
  12.  
  13. if [ $? -ne 0 ]  
  14. then 
  15.     echo "Error!May don't have the privileges" 
  16.     exit 1 
  17. fi 
  18.  
  19. /sbin/ifdown eth0 
  20. /sbin/ifup eth0 
  21.  
  22. if [ $? -ne 0 ]  
  23. then 
  24.     echo "Error!May cannot find eth0 or canot drive!" 
  25.     exit 2 
  26. fi 
  27.  
  28. #強制設置權限取消 
  29. chattr -i /etc/resolv.conf 
  30. cat > /etc/resolv.conf << EOF 
  31. domain localdomain 
  32. search localdomain 
  33. nameserver 192.168.94.1 
  34. nameserver 202.96.134.133 
  35. EOF 
  36. chattr +i /etc/resolv.conf 
  37.  
  38. /etc/init.d/network restart 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章