use*_*459 13 linux shell command-line sftp expect
我需要知道如何设置一个自动连接到远程服务器并更改目录并将该目录中的所有文件到本地的 cron 作业
我想我必须使用 sftp,但我在一些 shell 脚本中看到了一些名为“spawn”的命令,我很困惑这将做什么以及有什么用?
spawn sftp user@ipaddress
cd xxx/inbox
mget *
Run Code Online (Sandbox Code Playgroud)
这会在下载远程目录的上下文中工作吗?
pab*_*ouk 13
在您的情况下,spawn很可能是一个允许交互式程序操作自动化的期望脚本语言的命令。在这种情况下,spawn从expect 脚本运行外部命令。您的脚本示例缺少shebang序列(第一行以 开头#!),指示expect解释器,因此,expect在直接执行时不会被解释。
密码认证sftp仅限于交互方式;要sftp以交互模式进行控制,您可以使用以下期望脚本示例:
#!/usr/bin/env expect
set timeout 20 # max. 20 seconds waiting for the server response
set user username
set pass your-pass
set host the-host-address
set dir server-dir
spawn sftp $user@$host
expect assword:
send "$pass\r"
expect sftp>
send "cd $dir\r"
expect sftp>
send "mget *\r"
expect sftp>
send "exit\r"
expect eof
Run Code Online (Sandbox Code Playgroud)
另一种可能性是使用更安全的公钥身份验证(请参阅设置 SFTP 以使用公钥身份验证)。在这种情况下,您可以sftp直接在批处理模式下使用:
#!/bin/sh
user=username
host=the-host-address
dir=server-dir
sftp -b - "$user@$host" <<+++EOF+++
cd "$dir"
mget *
exit
+++EOF+++
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101564 次 |
| 最近记录: |