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]
时间: 2024-08-05 00:52:15

shell脚本修改Linux系统中所有IP样例的相关文章

用shell脚本实现linux系统上wifi模式(STA和soft AP)的转换

转载请注明出处:http://blog.csdn.net/hellomxj1/ 功能:在linux系统上实现wifi STA与AP功能的转换 实现成果:1.添加wifi密码账户add_wifi_account: 2.wifi两种模式启动的脚本wifi_start: 3.帮助信息README_WIFI_START: 具体实现过程如下: 添加wifi密码账户add_wifi_account 1 #!/bin/sh 2 3 echo "Add Wifi Account ..." 4 5 if

获得Unix/Linux系统中的IP、MAC地址等信息

获得Unix/Linux系统中的IP.MAC地址等信息 中高级  |  2010-07-13 16:03  |  分类:①C语言. Unix/Linux. 网络编程 ②手册  |  4,471 次阅读 作者:diaoyf  |  文章来源:http://programmerdigest.cn 实际环境和特殊需求往往会将简单问题复杂化,比如计算机IP地址,对于一个连接中socket,可以直接获得本端和对端的IP.端口信息.但在一些特殊场合我们可能需要更多的信息,比如系统中有几块网卡,他们的Mac地

Shell脚本查看linux系统性能瓶颈(转)

Shell脚本查看linux系统性能瓶颈(转自:http://blog.51cto.com/lizhenliang/1687612) [[email protected] ~]# cat show_sys_info.sh #!/bin/bash # os_check() { if [ -e /etc/redhat-release ]; then REDHAT=`cat /etc/redhat-release |cut -d' ' -f1` else DEBIAN=`cat /etc/issue

设置在 Ubuntu 12.04 中使用root 账号进行登录与修改 Linux 系统中的计算机名

3.设置在 Ubuntu 12.04 中使用root 账号进行登录 1.先设定一个 root 的密码,sudo passwd root 2.root 登录,su root 3.备份一下 lightgdm cp -p /etc/lightdm/lightdm.conf /etc/lightdm/lightdm.conf.bak 4.编辑 lightdm.conf sudo gedit /etc/lightdm/lightdm.conf 5.加: greeter-show-manual-login=

用shell脚本监控linux系统 自动发送邮件

此脚本可以做一个定时的检测,超出设定的值,即往邮箱发送警告 脚本用到bc,sendmail,163邮箱, yum install bc #!/bin/bash #System Monitoring Script while [ 1 ] do #本机需开启postfix或sendmail服务. #报警邮件地址设置 [email protected] [email protected] #设置脚本运行间隔时间.单位(秒). RUNTIME=900 #内存使用率监控设置,单位 (%) MEMTHRE=

使用Shell脚本对Linux系统和进程资源进行监控

ShellLinux脚本 摘要:Shell语言对于接触Linux的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.本文我们以Bash做为实例总结了使用Shell对系统和进程资源进行监控的一些内容,希望对您能有帮助. Shell语言对于接触Linux的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命 令并把它送入内核去执行.实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核.它没有一般编程语言的“

使用 shell 脚本对 Linux 系统和进程资源进行监控

Shell 简介 Shell 语言对于接触 LINUX 的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.实际上 Shell 是一个命令解释器,它解释由用户输入的命令并且把它们送到内核.它没有一般编程语言的“编译 - 链接 - 运行”过程.不仅如此,Shell 有自己的编程语言用于对命令的编辑,它允许用户编写由 shell 命令组成的程序.Shell 编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用

echo -e 在SHELL脚本和命令行中表现不同一例问题排查

一开发同事反馈,在SHELL脚本中使用echo -e 将结果输出到文件中,表现与预期不同,具体如下:[echo 的man如下所示] 开发同事在脚本中是这样调用他写的脚本的: # vim a.sh sh b.sh [b.sh中包含echo -e操作] 操作系统:ubuntu14.04 脚本内容[示例]: # cat test01.sh #!/bin/bash echo -e "aa\nbb" > /tmp/test.log 执行后,得到的/tmp/test.log 的内容为 # s

Shell脚本查看linux系统性能瓶颈(转)

linux服务器敲命令反应慢,网站访问慢,到底什么情况?根据本人的经验,主要原因可能是系统资源到达瓶颈,已经无法处理更多请求.在有监控系统情况下,可以直接通过WEB页面可视化看出是CPU瓶颈?硬盘瓶颈?还是网络瓶颈?如果公司服务器较少或者云服务器,就有可能没有一套监控系统,这时就要登陆到服务器,一条一条的敲命令,查找分析性能瓶颈.命令这么多,咋记得住啊!就算记得住,输入也费劲,于是就有了这个脚本,为了以后自己使用,另外也想分享给博友,学shell朋友能从中得到一丢丢启发.写的比较仓促,内容有点粗