这里只写了两个案例。我这里是8网段如果地址不一样修改就好了,需要几个添几个。
根据不同的地址修改主机名,也可以修改${name},注释掉第七行在将第八行的${name}修改成确定的主机名。 之后执行就好。
#!/bin/bash
#change host name scipts
ip=192.168.8.
ifconfig ens33|grep inet|awk '{print $2}'|grep ${ip}5
if test $? -eq 0
then
read -p "Please enter the name that needs to be modified:" name
hostnamectl set-hostname ${name}
# sed -i 'a${ip}.5 ${name}' /etc/hosts
echo "${ip}5 ${name}" >> /etc/hosts
bash
fi
ifconfig ens33|grep inet|awk '{print $2}'|grep ${ip}6
if test $? -eq 0
then
read -p "Please enter the name that needs to be modified:" name
hostnamectl set-hostname ${name}
# sed -i 'a${ip}6 ${name}' /etc/hosts
echo "${ip}6 ${name}" >> /etc/hosts
bash
fi
标签:脚本,grep,name,主机,etc,hosts,ip,bash
From: https://blog.csdn.net/2301_77161927/article/details/137354586