expect实现文件上传功能

expect实现文件上传功能

生产环境和pc之间总是隔着一道墙,上传和下载都很麻烦,之前用win的时候使用xshell来解决,切到macos之后,我觉得最好的解决方案就是使用expect脚本了。当然还有其他的解决办法
脚本内容如下,欢迎测试使用。

#!/usr/bin/expect
set host [lindex $argv 0]
set file_path [lindex $argv 1]
set file_name [lindex $argv 2]

if { $argc !=3 } {
    puts "必须输入3个参数,1,内网地址,2,文件路径,3,文件名";
    exit -1
}

set TERMSERV ****
set USER *****
set PASSWORD *****
set UATUN ****
set UATPWD ******

#发送文件到跳板机

spawn scp $file_path/$file_name $USER@$TERMSERV:~/

expect {
        "yes/no" {send "yes\r";exp_continue;}
         "*password:*" { send "$PASSWORD\r" }
        }
if { "$host" == "" } {

    interact
}

interact

# 登录跳板机
spawn ssh  -l   $USER $TERMSERV
expect {
        "yes/no" {send "yes\r";exp_continue;}
         "*password:*" { send "$PASSWORD\r" }
        }
if { "$host" == "" } {

  interact
}
# scp 文件到内网服务器
expect "*user@*" {send "scp ~/$file_name	$UATUN@$host:/data/backup/\r"}
expect {
        "yes/no" {send "yes\r";exp_continue;}
        "*password:*" { send "$UATPWD\r" }
        }
expect " *~]$*" {send "exit\r"}
interact

猜你喜欢

转载自blog.csdn.net/sun_ashe/article/details/80288716