1、創建 iplist.txt 文件,內容如下:
111.0.112.214
111.0.112.215
111.0.112.216
111.0.112.217
111.0.112.218
2、創建shell 腳本
#!/bin/bash
logfile=logs.log
ip_list='iplist.txt'
from_file=/root/workfiles
to_file=/usr/local/
user=root
passwd=123456
echo "--------(begin)--------" >$logfile
n=`cat $ip_list | wc -l` # 分發的ip數量
for (( i=1; i<=$n; i++ ))
do
ip=`cat $ip_list|head -$i|tail -1`
echo $ip
# 自動交互
/usr/bin/expect <<EOF
spawn scp -r $from_file $user@$ip:$to_file
expect "yes/no" {send "yes\n;exp_untinue"}
expect "password" {send "$passwd\n"}
expect eof
EOF
if [ $? -eq 0 ];then
echo "$ip:成功" >>$logfile
else
echo "$ip:失敗" >>$logfile
fi
echo '--------(end)--------' >>$logfile
done