Chinaunix首页 | 论坛 | 博客
  • 博客访问: 401940
  • 博文数量: 112
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 800
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-29 13:41
文章分类

全部博文(112)

文章存档

2020年(1)

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: 系统运维

2016-09-06 20:28:44



点击(此处)折叠或打开

  1. #!/bin/bash

  2. FILE_IP="/tmp/change_ip.csv"
  3. LOCAL_IP_CONFIG=($(/sbin/ifconfig |awk -F"\n" '$1~/^[^ ]/{print $1}'|awk '$1 !~ /lo/{print $1}'))
  4. #或ifconfig |awk -F"\n" '$1~/^[^ ]/{print $1}'|awk 'NR<=2{print $1}'
  5. IP_WAN_CONFIG="/etc/sysconfig/network-scripts/ifcfg-${LOCAL_IP_CONFIG[0]}"
  6. IP_LAN_CONFIG="/etc/sysconfig/network-scripts/ifcfg-${LOCAL_IP_CONFIG[1]}"
  7. OLD_LAN_IP=$(cat ${IP_LAN_CONFIG}|awk -F"=" '$1~/IPADDR/{print $2}')
  8. OLD_WAN_IP=$(cat ${IP_WAN_CONFIG}|awk -F"=" '$1~/IPADDR/{print $2}')
  9. OLD_WAN_MASK=$(awk -F"=" '$1~/NETMASK/{print $2}' ${IP_WAN_CONFIG})
  10. OLD_WAN_GATEWAY=$(awk -F"=" '$1~/GATEWAY/{print $2}' ${IP_WAN_CONFIG})
  11. #
  12. echo "local ip config file is ${LOCAL_IP_CONFIG[*]}"
  13. #
  14. if [[ -f ${FILE_IP} ]];then
  15.    echo "${FILE_IP} is exist."
  16. else
  17.    echo "${FILE_IP} is not exist."
  18.    exit 1
  19. fi
  20. #
  21. change_ip ()
  22. {
  23. while read line
  24.   do
  25.   local NEW_WAN_IP=`echo $line |awk -F"," '{print $3}'`
  26.   local NEW_WAN_MASK=`echo $line |awk -F"," '{print $4}'`
  27.   local NEW_WAN_GATEWAY=`echo $line |awk -F"," '{print $5}'`
  28.   local NEW_LAN_IP=`echo $line |awk -F"," '{print $6}'`
  29.   local FILE_WAN_IP=$(echo $line |awk -F"," '{print $1}')
  30.   local FILE_LAN_IP=$(echo $line |awk -F"," '{print $2}')

  31.     if [[ "$OLD_WAN_IP" = "$FILE_WAN_IP" ]] && [[ "$OLD_LAN_IP" = "$FILE_LAN_IP" ]];then
  32.       echo "local wan ip:${OLD_WAN_IP} is found..."
  33.       echo "local lan ip:${OLD_LAN_IP} is found..."
  34.       sed "s/${OLD_WAN_IP}/${NEW_WAN_IP}/" -i ${IP_WAN_CONFIG}
  35.       sed "s/${OLD_WAN_MASK}/${NEW_WAN_MASK}/" -i ${IP_WAN_CONFIG}
  36.       sed "s/${OLD_WAN_GATEWAY}/${NEW_WAN_GATEWAY}/" -i ${IP_WAN_CONFIG}
  37.       sed "s/${OLD_LAN_IP}/${NEW_LAN_IP}/" -i ${IP_LAN_CONFIG}
  38.       break
  39. # /sbin/reboot
  40.     else
  41.       continue
  42.       echo "local ip is no found."
  43.     fi
  44.   done < "${FILE_IP}"
  45. }
  46. #
  47. if [[ -f ${IP_WAN_CONFIG} ]] && [[ -f ${IP_LAN_CONFIG} ]];then
  48.    echo "local ip config is ${LOCAL_IP_CONFIG[*]}"
  49.    change_ip
  50. else
  51.    echo "local ip config is not ${LOCAL_IP_CONFIG}"
  52.    exit 1
  53. fi




change_ip.csv内容如下:
old wan old lan new wan netmask gateway new lan
192.168.8.26 10.0.0.26 192.168.8.131 255.255.255.128 192.168.8.129 10.0.1.131
192.168.8.27 10.0.0.27 192.168.8.132 255.255.255.128 192.168.8.129 10.0.1.132
192.168.8.28 10.0.0.28 192.168.8.133 255.255.255.128 192.168.8.129 10.0.1.133
192.168.8.29 10.0.0.29 192.168.8.134 255.255.255.128 192.168.8.129 10.0.1.134

阅读(1549) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~