跳板机:
[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 [email protected] ;; 2) ssh [email protected] ;; 3) exit ;; 4) ssh [email protected] ;; 5) ssh [email protected] ;; 6) ssh [email protected] ;; *) echo "input error,input again" esac done
分发公钥:
注意:
- 并发执行{}&
- shell中调用expect,EOF中必须是tab键
- 并发控制:文件句柄中增加$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
标签:tmp,初始化,fifofile,系统,echo,host,expect,bin From: https://www.cnblogs.com/fpcbk/p/18424194