shell脚本使用expect给当前网段所有主机分发任意文件

安装expect
yum -y install expect

脚本1

#!/bin/bash
read -ep "请输入ip:" ip
read -ep "请输入密码:" passwd
read -ep "请输入文件绝对路径:" file

/usr/bin/expect <<EOF

spawn scp $file $ip:/root/
expect "yes" {send "yes\n;exp_untinue"}
expect "password" {send "$passwd\n"}
expect "100%" {send "echo "$ip:成功"}
expect eof
EOF  

脚本2

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

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

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

#!/bin/bash
echo "拷贝情况如下:" > /root/test.log
n=`cat /root/iplist.txt | wc -l` #分发的ip数量
for (( i=1; i<=$n; i++ ))
do
	passwd=`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 <<EOF
spawn scp /root/1.txt $ip:/root/
expect "yes/no" {send "yes\n;exp_untinue"}
expect "password" {send "$passwd\n"}
expect eof
EOF
if [ $? -eq 0 ];then
   echo "$ip:成功" >>/root/test.log
else
   echo "$ip:失败" >>/root/test.log

fi        
done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值