ssh-copy-id.sh

    使用sshpass命令:

    1. #!/bin/bash
    2. if [ ! -f ~/.ssh/id_rsa ];then
    3. ssh-keygen -f /root/.ssh/id_rsa -N ""
    4. else
    5. echo "id_rsa has created ..."
    6. fi
    7. user=root
    8. export SSHPASS=root123
    9. for net in {21..250};do
    10. {
    11. sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $user@192.100.3.$net >/dev/null 2>&1
    12. }&
    13. done

    使用expect命令:

    1. #!/bin/bash
    2. if [ ! -f ~/.ssh/id_rsa ];then
    3. ssh-keygen -f /root/.ssh/id_rsa -N ""
    4. else
    5. echo "id_rsa has created ..."
    6. fi
    7. user="root"
    8. passwd="root123"
    9. for net in {21..250};do
    10. {
    11. expect <<-EOF
    12. set timeout 10
    13. spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user@192.100.3.$net
    14. expect {
    15. "yes/no" { send "yes\n";exp_continue }
    16. "password" { send "$passwd\n" }
    17. }
    18. expect "password" { send "$passwd\n" }
    19. EOF
    20. }&
    21. done