#!/bin/bash
#脚本自动替换参数
setcs=(
"ClientAliveInterval"
"ClientAliveCountMax"
)
upsetcs=(
"ClientAliveInterval 60"
"ClientAliveCountMax 5"
)
suzu=${#setcs[*]} ; echo "修改文件总参数个为:$suzu"
# 由于数组角标默认从0开始,数组角标数数到元素个数会多出一个空的,才用小数组个数6==》6-1=0~5
phpini="/etc/ssh/sshd_config"
if [ ! -e $phpini.back ]
then
echo "备份"
cp $phpini $phpini.back
else
echo "备份文件存在:$phpini.back"
fi
for((i=0;i<$suzu;i++))
do
set=${setcs[$i]} ; upset=${upsetcs[$i]}
NR=`cat -n $phpini | grep "$set" | awk -F" " '{print $1}'` ; echo "行信息: $NR"
if [[ ${NR} =~ ^[0-9]+ ]]
then
echo "$set +++++> 参数更新: $upset
-------------------------------------------------------------"
sed -i "$NR a $upset" $phpini
sed -i "$NR d" $phpini
echo "修改后的信息:"; cat $phpini | grep "$upset"
else
echo "$NR 没有获取到行 $setcs 退出修改"
fi
done
systemctl restart sshd
# wget www.eisc.cn/file/shell/sedfile.sh