shell脚本修改Linux系统中所有IP样例

Posted 雅虎跳跳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 [email protected]
    elif [ "${ip_type}" = "db_ip" ];then
        change_db_ip [email protected]
    else
        echo "The ip type is not correct."
        exit 2
    fi

}

main [email protected]

 

以上是关于shell脚本修改Linux系统中所有IP样例的主要内容,如果未能解决你的问题,请参考以下文章

linux shell 用啥命令可以修改脚本中全部日期为当前系统日期?

linux下shell简单修改ip的脚本

如何在Linux shell中获取本机主机名

Linux系统shell脚本之打印系统的IP信息

Linux系统防CC攻击自动拉黑IP增强版Shell脚本

使用Shell批量修改Linux服务器的IP --- shell百例