系统初始化

跳板机:

[fpc@localhost script]# cat jumpserver.sh 
#!/bin/bash
trap '' INT
echo -e "\e[1;31m-----------------------------------------------------"
cat <<-EOF
               1)optical modem                        
               2)master DNS                          
               3)slave DNS                             
               4)router                             
            5)virtual host1                      
               6)virtual host2                     
EOF
echo -e "-----------------------------------------------------\e[0m"
while :
do
echo -ne "\e[1;32mplease input one number: (1,2,3,4,5): \e[0m"
    read -p "" num
    case $num in 
        1)
            ssh fpc@192.168.6.3 
            ;;
        2)
            ssh fpc@192.168.6.66
            ;;
        3)
            exit
            ;;
        4)
            ssh fpc@10.10.10.254
            ;;
        5)
            ssh fpc@10.10.10.11
            ;;
        6)
            ssh fpc@10.10.10.12
            ;;
        *)
            echo "input error,input again"
    esac
done

分发公钥:

注意:

  1. 并发执行{}&
  2. shell中调用expect,EOF中必须是tab键
  3. 并发控制:文件句柄中增加$thead行
#!/bin/bash
thread=10
tmp_fifofile=/tmp/$$.fifo
mkfifo $tmp_fifofile
exec 8<> $tmp_fifofile
rm -f $tmp_fifofile
for i in `seq $thread`
do
        echo >&8
done

password="Fpc11100885"
user="root"
>distributed_hosts
rpm -q expect &>/dev/null
if [ $? -eq 0 ];then
        echo "install expect"
        sudo yum install -y expect
fi
if [ ! -f "~/.ssh/id_rsa" ];then
        ssh-keygen  -P ""  -f ~/.ssh/id_rsa
fi
for host in `cat ./network_hosts`
do
        read -u 8
        {
        ping -c 1 -w 2 $host &>/dev/null
        if [ $? -eq 0 ];then
                echo $host >> distributed_hosts
                /usr/bin/expect <<-EOF
                spawn ssh-copy-id ${user}@$host
                expect {
                "yes/no" {send "yes\r";exp_continue}
                "password" {send "$password\r";exp_continue}
                eof {send_tty "eof"}
                }
                EOF

        fi
        echo >&8
        }&
        wait
        echo "distribute finished"
done

 

posted @ 2024-09-21 16:45  fangpengcheng_方鹏程  阅读(3)  评论(0编辑  收藏  举报