shell脚本 自动批量分发文件

1.安装expect

yum -y install expect

2.创建iplist.txt文件(要分发的IP地址)

192.168.163.131192.168.163.134

3.创建user.txt(里面包含密码)

33989863398986

4.创建脚本 test.sh,test.log
test.sh

#!/bin/bashecho "拷贝情况如下:" > /root/test.logn=`cat /root/iplist.txt | wc -l` #分发的ip数量for (( i=1; i<=$n; i++ ))dopasswd=`cat /root/user.txt|head -$i|tail -1`#第i个IP地址的密码ip=`cat /root/iplist.txt|head -$i|tail -1`#第i个IP地址echo $ip##自动交互/usr/bin/expect <<EOFspawn scp /root/1.txt $ip:/root/expect "yes/no" {send "yes\n;exp_untinue"}expect "password" {send "$passwd\n"}expect eofEOFif [ $? -eq 0 ];thenecho "$ip:成功" >>/root/test.logelseecho "$ip:失败" >>/root/test.logfi done
(0)

相关推荐