expect 脚本来实现自动将本地文件打包上传到远程服务器

博主也是最近在学习这个shell expect 如果有问题的话欢迎指正!

我用了两种方法来实现: 用到的重要linux命令有(cat、grep、tar、awk、ssh)

一、通过配置文件+shell脚本+expect脚本(shell脚本来获取配置文件信息然后将获得的参数传递给expect脚本)
废话不多说上代码:

poperties.cfg(配置文件)

username=XXX
ip=XXX.XXX.XXX.XXX
passwd=XXXXX
localpath=/usr/local/shell/expect/(本地文件的路径)
localfile=test.txt(要打包上传的本地文件)
remotepath=./JWT(要上传到远程服务器的文件路径)
tarfile=test.tar.gz(要压缩成的压缩包名字)

use.sh(shell脚本文件)

#!/bin/bash
username=`cat poperties.cfg|grep ^username|awk -F '=' '{print $2}'`
ip=`cat poperties.cfg|grep ^ip|awk -F '=' '{print $2}'`
passwd=`cat poperties.cfg|grep ^passwd|awk -F '=' '{print $2}'`
localpath=`cat poperties.cfg|grep ^localpath|awk -F '=' '{print $2}'`
localfile=`cat poperties.cfg|grep ^localfile|awk -F '=' '{print $2}'`
remotepath=`cat poperties.cfg|grep ^remotepath|awk -F '=' '{print $2}'`
tarfile=`cat poperties.cfg|grep ^tarfile|awk -F '=' '{print $2}'`
expect upload1.sh $username $ip $passwd $localpath $localfile $remotepath $tarfile

upload1.sh (expect脚本)

#!/bin/expect
set username [lindex $argv 0]
set ip [lindex $argv 1]
set passwd [lindex $argv 2]
set localpath [lindex $argv 3]
set localfile [lindex $argv 4]
set remotepath [lindex $argv 5]
set tarfile [lindex $argv 6]
set timeout 30
spawn tar -czvf ${tarfile} ${localfile}
spawn scp ${localpath}${tarfile} ${username}@${ip}:${remotepath}
expect {
        "(yes/no?)"
        {
                send "yes\r"
        }
        "password:"
        {
                send "$passwd\r"
        }
}
spawn ssh ${username}@${ip} "tar -xzvf ${remotepath}/${tarfile} -C ${remotepath}"
expect {
        "(yes/no?)"
       {
               send "yes\r"
       }
        "password:"
        {
               send "$passwd\r"
        }
}

expect eof
 

二、通过配置文件+expect脚本来实现
properties.cfg(配置文件)

username=XXX
ip=XXX.XXX.XXX.XXX
passwd=XXXXX
localpath=/usr/local/shell/expect/(本地文件的路径)
localfile=test.txt(要打包上传的本地文件)
remotepath=./JWT(要上传到远程服务器的文件路径)
tarfile=test.tar.gz(要压缩成的压缩包名字)

upload.sh(expect脚本)

#!/bin/expect
set username [exec cat properties.cfg | grep ^username | awk -F "=" "{print \$2}"]
set ip [exec cat properties.cfg | grep ^ip | awk -F "=" "{print \$2}"]
set passwd [exec cat properties.cfg | grep ^passwd | awk -F "=" "{print \$2}"]
set localpath [exec cat properties.cfg | grep ^localpath | awk -F "=" "{print \$2}"]
set localfile [exec cat properties.cfg | grep ^localfile | awk -F "=" "{print \$2}"]
set remotepath [exec cat properties.cfg | grep ^remotepath | awk -F "=" "{print \$2}"]
set tarfile [exec cat properties.cfg | grep ^tarfile | awk -F "=" "{print \$2}"]
set timeout 30
#spawn scp $source $target
spawn tar -czvf ${tarfile} ${localfile}
spawn scp ${localpath}${tarfile} ${username}@${ip}:${remotepath}
expect {
        "(yes/no?)"
        {
                send "yes\r"
        }
        "password:"
        {
                send "$passwd\r"
        }
}
spawn ssh ${username}@${ip} "tar -xzvf ${remotepath}/${tarfile} -C ${remotepath}"
expect {
        "(yes/no?)"
       {
               send "yes\r"
        }
        "password:"
        {
               send "$passwd\r"
        }
}

expect eof
————————————————
版权声明:本文为CSDN博主「走路带疯的人zzZ」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36457702/article/details/83068785

猜你喜欢

转载自blog.csdn.net/renlonggg/article/details/104857063