检查是否安装expect which expect 如果没有安装, yum install expect -y vi distribute_file.sh #!/usr/bin/expect -f #chmod 777 distribute_file.sh password= path=$1 topath=$2 while read -r ipline do expect<<-END spawn scp $path root@${ipline}:${topath} expect { "(yes/no)?" {send "yes\r"; exp_continue} "${ipline}'s password:" {send "${password}\r"} "Permission denied" { send_user "[exec echo "\nError: Password is wrong\n"]"; exit} } expect eof exit END done < ipfile.txt 同目录下创建 ipfile.txt 内容为: #批量添加目标主机ip 脚本使用方法: sh distribute_file.sh 所传文件的绝对路径 文件传入目标主机绝对路径