时间:2021-10-01 13:04:32 | 栏目:Shell | 点击:次
作为一名Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名、ip信息、网关等配置。修改成特定的主机名在维护和管理方面也比较方便。如下脚本用途为:修改ip和主机名等相关信息,可以根据实际需求修改,举一反三!
}
#定义hosts函数
############function hosts##############
function Change_hosts ()
{
if
[ ! -d $DIR ];then
mkdir -p $DIR
fi
cp $HOSTS $DIR
read -p "Please insert ip address": IPADDR
host=`echo $IPADDR|sed 's/\./-/g'`
cat $HOSTS |grep 127.0.0.1 |grep "$host"
if
[ $? -ne 0 ];then
sed -i "s/127.0.0.1/127.0.0.1 $host/g" $HOSTS
echo "This hosts change success "
else
echo "This $host IS Exist .........."
fi
}
###########fuction network###############
#定义network函数
function Change_network ()
{
if
[ ! -d $DIR ];then
mkdir -p $DIR
fi
cp $NETWORK $DIR
read -p "Please insert ip address": IPADDR
host=`echo $IPADDR|sed 's/\./-/g'`
grep "$host" $NETWORK
if
[ $? -ne 0 ];then
sed -i "s/^HOSTNAME/#HOSTNAME/g" $NETWORK
echo "NETWORK=$host" >>$NETWORK
else
echo "This $host IS Exist .........."
fi
}
#PS3一般为菜单提示信息#
PS3="Please Select ip or hosts Menu":
#select为菜单选择命令,格式为select $var in ..command.. do .... done
select i in "Change_ip" "Change_hosts" "Change_network"
do
#case 方式,一般用于多种条件下的判断
case $i in
Change_ip )
Change_ip
;;
Change_hosts )
Change_hosts
;;
Change_network )
Change_network
;;
*)
echo
echo "Please Insert $0: Change_ip(1)|Change_hosts(2)|Change_network(3)"
echo
;;
esac
done