我该如何修复我的期望脚本以供SCP下载? [英] How can I fix my expect script for scp download?

查看:57
本文介绍了我该如何修复我的期望脚本以供SCP下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Expect绕过我正在使用的远程服务器的密码身份验证过程,因此我可以批量下载一系列文件,每个文件存储在不同的文件夹中.

I am trying to use expect to bypass the password authentication process for a remote server I'm using, so I can download a series of files in bulk, each of which is stored in a different folder.

使用Expect的原因是远程文件存储在单独的文件夹中,并且我正在访问的服务器不允许我使用ssh密钥访问.

The reason for using expect is the remote files are stored in separate folders, and the server I'm accessing won't allow me ssh key access.

目前,我正在努力使用Expect来下载单个文件,不要介意一次下载多个文件.下面是我的简单脚本:

At present I am struggling to download a single file using expect, never mind numerous ones at once. Below is my simple script:

#!/bin/bash 

#connect to the server using scp
expect <<EOF
spawn scp username@server:file.txt ./
expect "*word:*"
send "password\r"
EOF

我希望将file.txt下载到运行脚本的目录中.但是,运行该脚本时,我会从服务器获取要求密码的文本输出,然后在4-5秒前暂停它会退出到命令提示符.

I expect the file.txt to be downloaded into the directory I run the script in. However, when it runs I instead get the text output from my server asking for the password, followed by a pause of 4-5 seconds before it exits to command prompt.

任何帮助,我们将不胜感激!

Any help with this is greatly appreciated!

推荐答案

  1. 我在您的代码中看到花式"引号.期望不会理解那些作为报价.确保您的编辑器不使用智能引号".
  2. 发送密码后,必须等待下载完成才能退出脚本:

  1. I see "fancy" quotes in your code. Expect will not understand those as quotes. Make sure your editor does not use "smart quotes".
  2. After you send the password, you have to wait for the download to complete before you exit your script:

#!/bin/bash 

#connect to the server using scp
expect <<EOF
    set timeout -1
    spawn scp username@server:file.txt ./
    expect "*word:*"
    send "password\r"
    expect eof
EOF

这篇关于我该如何修复我的期望脚本以供SCP下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆