版权声明:请随意转载 https://blog.csdn.net/cheng1804/article/details/81912138
#!/usr/bin/env bash
rpm -q expect &>/dev/null
if [ $? -ne 0 ];then
yum -y install expect tcl tclx tcl-devel &>/dev/null
fi
if [ ! -f ~/.ssh/id_rsa.pub ];then
ssh-keygen -P "" -f ~/.ssh/id_rsa
fi
> up.txt
mkfifo /tmp/$$.fifo
exec 8<>/tmp/$$.fifo
rm -rf /tmp/$$.fifo
for i in `seq 30`
do
echo >&8
done
for i in `seq 254`
do
read -u 8
{
ip=192.168.122.$i
ip1=`ip a |grep 'inet ' |grep 'scope global dynamic'|awk '{print $2}'|awk -F'/' '{print $1}'`
if [[ $ip == "192.168.122.1" ]] || [[ $ip == "192.168.122.71" ]];then
continue
fi
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo $ip >> up.txt
(
/usr/bin/expect <<-EOF
set timeout 10
spawn ssh-copy-id $ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "centos\r" }
}
expect eof
EOF
) >/dev/null 2>&1
fi
echo >&8
}&
done
wait
exec 8>&-
grep '127' /etc/hosts > /tmp/1.txt
grep ':1' /etc/hosts >> /tmp/1.txt
>/etc/hosts
i=1
for j in `cat up.txt`
do
read -p "修改第"$i"台主机名: " name
let i=$i+1
ssh $j hostnamectl set-hostname $name
echo "$j $name " >>/etc/hosts
done
cat /tmp/1.txt >> /etc/hosts
for i in `cat up.txt`
do
ssh $i systemctl stop firewalld
ssh $i systemctl disable firewalld &>/dev/null
ssh $i setenforce 0 &>/dev/null
ssh $i sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/conf &>/dev/null
scp /etc/hosts $i:/etc/hosts
done
wait
echo "finish"