#!/bin/SHCurr_dir=$ (CD $ (dirname$0);pwd) Td_base=`su-Tduser-c"Echo"${td_base}""`functionchange_app_ip () {if[$#-ne3]; Then Echo "The Param is not correct."Exit1 fi if[" $"!="app_ip"]; Then Echo "The IP type is not correct."Exit2 fiLocal Ip_type=$1Local OLD_IP=$2Local NEW_IP=$3Local 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 forIinch${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 forIinch${run_dir} ${mttools} Dofile_list=`Find${i}-name"${old_suffix_ip}"` for file inch${file_list} Dorename ${old_suffix_ip} ${new_suffix_ip} ${file} Done Done }functionchange_db_ip () {if[$#-ne3]; Then Echo "The param number is not correct."Exit1 fi if[" $"!="db_ip"]; Then Echo "The IP type is not correct."Exit2 fiLocal Ip_type=$1Local OLD_IP=$2Local NEW_IP=$3Local 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}| whileRead Line DoChange_type=`Echo "${line}"|Cut-D:-F2' Change_file=`Echo "${line}"|Cut-D' '-F2|Cut-D:-F1' button=`Echo "${line}"|awk '{print $}'` if[ !"${change_file}"]; Then Echo "The file"${change_file}"is not exits."Continuefi 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}functioncheck_ip () {Local check_ip=$1 Echo "${check_ip}"|grep "^[0-9]\{1,3\}\ (\.[ 0-9]\{1,3\}\) \{3\}">/dev/NULL if[$?-ne0]; Then Echo "The IP"${CHECK_IP}"is not a regular IP."return1 fiLocal First=`Echo "${check_ip}"|Cut-D' '-F1' Local second=`Echo "${check_ip}"|Cut-D' '-F2' Local third=`Echo "${check_ip}"|Cut-D' '-F3' Local forth=`Echo "${check_ip}"|Cut-D' '-F4` forNuminch${first} ${second} ${third} ${forth} Do if[${num}-lt0-O ${num}-gt255]; Then Echo "The number ${num} is a regular IP number."return1 fi Done}functionMain () {if[$#-ne3]; Then Echo "The param number is not correct."Exit1 fiLocal Ip_type=$1Local OLD_IP=$2Local NEW_IP=$3check_ip ${old_ip} check_ip ${new_ip}if["${ip_type}"="app_ip"]; Thenchange_app_ip [email protected]elif["${ip_type}"="db_ip"]; Thenchange_db_ip [email protected]Else Echo "The IP type is not correct."Exit2 fi}main [email protected]
Shell script modifies all IP samples in Linux system