zoukankan      html  css  js  c++  java
  • shell脚本修改Linux系统中所有IP样例

    #!/bin/sh
    
    CURR_DIR=$(cd $(dirname $0);pwd)
    TD_BASE=`su - tduser -c "echo "${TD_BASE}""`
    
    function change_app_ip()
    {
        if [ $# -ne 3 ];then
            echo "The param is not correct."
            exit 1
        fi
        
        if [ "$1" != "app_ip" ];then
            echo "The ip type is not correct."
            exit 2
        fi
            
        local ip_type=$1
        local old_ip=$2
        local new_ip=$3
        
        local grep_old_ip=$(echo "${old_ip}"|sed "s/./\./g")
        local old_suffix_ip=$(echo "${old_ip}"|tr '.' '_')
        local new_suffix_ip=$(echo "${new_ip}"|tr '.' '_')
        
        #定义需要替换IP的文件夹和文件
        local run_dir="${TD_BASE}/run"
        local mttools_dir="${TD_BASE}/mttools"
        local firewall="/etc/sysconfig/SuSEfirewall2"
        
        #替换文件中的IP和下划线的IP
        for i in ${run_dir} ${mttools} ${firewall}
        do
            grep -lr --exclude-dir=var ${old_ip} ${i}|xargs sed -i "s/${grep_old_ip}/${new_ip}/g"
            grep -lr --exclude-dir=var ${old_suffix_ip} ${i}|xargs sed -i "s/${old_suffix_ip}/${new_suffix_ip}/g"
        done
        
        #替换文件名字中含有下划线的IP
        for i in ${run_dir} ${mttools}
        do
            file_list=`find ${i} -name "${old_suffix_ip}"`
            for file in ${file_list}
            do
                rename ${old_suffix_ip} ${new_suffix_ip} ${file}
            done
        done
        
    }
    
    function change_db_ip()
    {
        if [ $# -ne 3 ];then
            echo "The param number is not correct."
            exit 1
        fi
        
        if [ "$1" != "db_ip" ];then
            echo "The ip type is not correct."
            exit 2
        fi
            
        local ip_type=$1
        local old_ip=$2
        local new_ip=$3
        
        local grep_old_ip=$(echo "${old_ip}"|sed "s/./\./g")
        local ip_list="ip_list.dat"
        
        cat ${CURR_DIR}/${ip_list}|grep -v "^#"|grep -v "^ $*"|grep ${ip_type}|while read line
        do 
            change_type=`echo "${line}"|cut -d : -f 2`
            change_file=`echo "${line}"|cut -d ' ' -f 2|cut -d : -f 1`
            button=`echo "${line}"|awk '{print $1}'`
    
            if [ ! "${change_file}" ];then
                echo "The file "${change_file}" is not exits."
                continue
            fi
    
            if [ "${button}" = "db_ip" ];then
                if [ "${change_type}" = "*" ];then
                    grep -lr --exclude-dir=var ${old_ip} ${change_file}|xargs sed -i "s/${grep_old_ip}/${new_ip}/g"
                    grep -lr --exclude-dir=var ${old_ip} ${change_file}|xargs sed -i "s/${old_suffix_ip}/${new_suffix_ip}/g"
                elif [ "${change_type}}" != "" ];then
                    grep -lr --exclude-dir=var ${old_ip} ${change_file}|xargs sed -i "s/${grep_old_ip}/${new_ip}/"
                    grep -lr --exclude-dir=var ${old_ip} ${change_file}|xargs sed -i "s/${old_suffix_ip}/${new_suffix_ip}/"
                fi
            fi
        done
    }
    
    function check_ip()
    {
        local check_ip=$1
        echo "${check_ip}"|grep "^[0-9]{1,3}(.[0-9]{1,3}){3}">/dev/null
        if [ $? -ne 0 ];then
            echo "The ip "${check_ip}" is not a regular ip."
            return 1
        fi
        
        local first=`echo "${check_ip}"|cut -d ' ' -f 1`
        local second=`echo "${check_ip}"|cut -d ' ' -f 2`
        local third=`echo "${check_ip}"|cut -d ' ' -f 3`
        local forth=`echo "${check_ip}"|cut -d ' ' -f 4`
        
        for num in ${first} ${second} ${third} ${forth}
        do
            if [ ${num} -lt 0 -o ${num} -gt 255 ];then
                echo "The number ${num} is not a regular IP number."
                return 1
            fi
        done
    }
    
    function main()
    {
        if [ $# -ne 3 ];then 
            echo "The param  number is not correct."
            exit 1
        fi
        
        local ip_type=$1
        local old_ip=$2
        local new_ip=$3
        
        check_ip ${old_ip}
        check_ip ${new_ip}
        
        if [ "${ip_type}" = "app_ip" ];then
            change_app_ip $@
        elif [ "${ip_type}" = "db_ip" ];then
            change_db_ip $@
        else
            echo "The ip type is not correct."
            exit 2
        fi
    
    }
    
    main $@
  • 相关阅读:
    PHP array_intersect_uassoc
    PHP array_intersect_key
    PHP array_intersect_assoc
    PHP array_flip
    PHP array_filter
    PHP array_fill
    PHP array_fill_keys
    Android4.0-Fragment框架实现方式剖析
    Fragment 生命周期
    WebView
  • 原文地址:https://www.cnblogs.com/yahutiaotiao/p/8030459.html
Copyright © 2011-2022 走看看