linux远程批量传送脚本文件并执行一系列指令

该bash脚本(auto_scp.sh)用于从本地主机批量复制文件到多个远程服务器,并使用expect进行SSH身份验证。在远程服务器上,文件的所有者和组会被更改为Ityunwei,并执行install_os7.sh脚本进行系统安装。expect_scpw.sh脚本处理SSH连接,禁用严格的主机键检查,设置密码并执行命令。
摘要由CSDN通过智能技术生成

auto_scp.sh

#!/bin/bash
dir=/home/ability/maintain
localFile=/home/Ityunwei/agent/bin/install_os7.sh
remoteDir=/home/Ityunwei/agent/bin
while read line
do
host=`echo $line| awk '{print $1}'`
password=`echo $line | awk '{print $2}'`
expect <<EOF
set timeout -1
spawn scp -r ${localFile} ${User}@${host}:${remoteDir}
expect {
        "yes/no" {send "yes\r"; exp_continue}
        "password:" {send "${password}\r"}
}
expect 100%
expect eof
EOF
if [ $? -eq 0 ]; then
    echo "${host}成功"
  else
    echo "${host}失败"
fi
sleep 20s
expect expect_scpw.sh $host $password &
sleep 20s
done < $dir/IThost

expect_scpw.sh

 #!/usr/local/bin/expect
 set HOST [lindex $argv 0]
 set PASSWD [lindex $argv 1]
 spawn ssh root@$HOST -o StrictHostKeyChecking=no
 expect  "(yes/no)?" { send "yes\n" }
 expect "*password:" { send "$PASSWD\n" }
 expect "*password:" { send "$PASSWD\n" }
 expect "*#" { send "hostname\n" }
 expect "*#" { send "cd /home/Ityunwei/agent/bin/\n" }
 expect "*#" { send "chown -R Ityunwei:Ityunwei install_os7.sh \n" }
 expect "*#" { send "./install_os7.sh\n" }
 expect "*#" { send "tailf -n1 /etc/rc.local\n" } 
 expect eof
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值