无密钥登录的自动脚本实现: vim key.sh 

#!/usr/bin/expect
set timeout 10
set username [lindex $argv 0]  #执行该脚本传入进来的三个参数
set password [lindex $argv 1]
set hostname [lindex $argv 2]
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $username@$hostname   #传递给对端机器进行授权的密钥存放位置
expect {
           #first connect, no public key in ~/.ssh/known_hosts
           "Are you sure you want to continue connecting (yes/no)?" {   #第一次ssh匹配此处逻辑
           send "yes\r"
           expect "password:"
               send "$password\r"
           }
           #already has public key in ~/.ssh/known_hosts
           "password:" {  #第二次匹配此处逻辑
               send "$password\r"
           }
           "Now try logging into the machine" {
               #it has authorized, do nothing!  #已经授权无密钥登录,则匹配此处逻辑,do nothing
           }
        } 
expect eof

chmod 777 key.sh 然后执行下述命令即可。 ./key.sh root 123456 192.168.1.100