系統初始化

fangpengcheng_方鹏程發表於2024-10-26

跳板機:

[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

相關文章