使用批处理文件运行 Unix shell 脚本以从 WinSCP 下载文件

问题描述 投票:0回答:1

我正在尝试通过批处理文件从 WinSCP 下载文件。如果我在批处理文件中输入文件名,我就可以下载该文件。

但是我需要动态输入文件名(即必须在运行时输入文件名)。

这是我的代码

cd\Users\Desktop\WinSCP
winscp.com /ini=null /script=C:\Users\Desktop\test.txt
open sftp://username:password@hostname/
$ ssh -o "StrictHostKeyChecking=yes" username@hostname
cd /log
get test.log C:\Users\Desktop\Downloading_logs\

这里

Test.log
是我在批处理文件中提供的文件名。

请建议一种动态输入文件名的方法。

提前致谢。

batch-file unix sftp winscp
1个回答
1
投票

使用批处理文件的参数

代码(script_with_arg.bat):

@echo off

setlocal enableextensions

if "%~1" equ "" (
    echo "Please provide a file name"
    goto :eof
)

cd\Users\Desktop\WinSCP
winscp.com /ini=null /script=C:\Users\Desktop\test.txt
open sftp://username:password@hostname/
$ ssh -o "StrictHostKeyChecking=yes" username@hostname
cd /log
get "%~1" C:\Users\Desktop\Downloading_logs\

echo "Done"

注释

  • 这是最直接的方法
  • 该论证被称为
    "%~1"
    。检查 [SS64]:命令行参数(参数) 了解更多详细信息
  • 开头的
    if
    子句是验证是否提供了参数,如果没有,则简单地显示错误消息并退出
  • 处理路径时,最好dblquote它们,因为路径中的SPACE可能会完全搞乱事情

其他方式:

  • 使用从脚本外部设置的环境变量(而不是参数)
  • 让用户从脚本中输入文件名(通过
    set /p
    ),如 @ramu246 的评论所指出的(我错过了这个:))

@EDIT0

  • 查看@MartinPrikryl的评论并进行一些测试后,结果发现您的.bat文件完全是垃圾(我的也是,因为它是基于您的)
  • 但是,修复仍然可以,即使它不起作用(因为我盲目地应用它 - 检查上一个项目符号)

我做了一些更改,下面是一个真正有效的版本(当然敏感数据已被更改)。

脚本.bat:

@echo off

if "%~1" equ "" (
    echo "Please provide a file name"
    goto :eof
)

winscp.com /ini=winscp_cfg.ini /script=winscp_script.txt /parameter "%~1"
echo "Done"

winscp_script.txt:

echo Received argument: "%1%"
open sftp://cfati:[email protected]:22001/
cd /tmp
get "%1%" .\"%1%"
exit

输出

e:\Work\Dev\StackOverflow\q047989047>where winscp
c:\Install\x86\WinSCP\WinSCP\AllVers\WinSCP.com
c:\Install\x86\WinSCP\WinSCP\AllVers\WinSCP.exe

e:\Work\Dev\StackOverflow\q047989047>dir /b
script.bat
winscp_script.txt

e:\Work\Dev\StackOverflow\q047989047>script.bat "test with spaces.log"
Received argument: "test with spaces.log"
Searching for host...
Connecting to host...
Authenticating...
Continue connecting to an unknown server and add its host key to a cache?

The server's host key was not found in the cache. You have no guarantee that the server is the computer you think it is.

The server's Ed25519 key details are:

    Algorithm:  ssh-ed25519 256
    SHA-256:    M/iFTnSbi0k4VEcd8I75GiO7t6gza6RL99Pkh+bz4AQ=
    MD5:        8f:2f:f0:4a:ed:41:aa:e6:61:fa:5d:1f:f4:5b:c0:37

If you trust this host, press Yes. To connect without adding host key to the cache, press No. To abandon the connection press Cancel.
In scripting, you should use a -hostkey switch to configure the expected host key.
(Y)es, (N)o, C(a)ncel (8 s), (C)opy Key, (P)aste key: Yes
Using username "cfati".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Session started.
Active session: [1] [email protected]
/tmp
test with spaces.log      |            6 B |    0.0 KB/s | binary | 100%
"Done"

e:\Work\Dev\StackOverflow\q047989047>dir /b
script.bat
test with spaces.log
winscp_cfg.ini
winscp_script.txt

e:\Work\Dev\StackOverflow\q047989047>del /q "test with spaces.log"

e:\Work\Dev\StackOverflow\q047989047>dir /b
script.bat
winscp_cfg.ini
winscp_script.txt

e:\Work\Dev\StackOverflow\q047989047>script.bat "test with spaces.log"
Received argument: "test with spaces.log"
Searching for host...
Connecting to host...
Authenticating...
Using username "cfati".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Session started.
Active session: [1] [email protected]
/tmp
test with spaces.log      |            6 B |    0.0 KB/s | binary | 100%
"Done"

e:\Work\Dev\StackOverflow\q047989047>dir /b
script.bat
test with spaces.log
winscp_cfg.ini
winscp_script.txt

注释

  • 这次我使用自己的路径
  • 需要 .ini 文件 (winscp_cfg.ini) 才能传递主机指纹。也可以为
    -hostkey
    命令传递
    open
    参数([WinSCP]: open),但我没有成功(我也没有尝试太多)
    • 正如您在输出中看到的,第一次st需要用户确认(
      (Y)es, (N)o, C(a)ncel....
      )才能生成文件,下一次它只需使用它
    • 同样的事情也发生在你身上(我假设你想跳过 .ini 文件名),但由于一个错误: Ux/dev/nullWin 中的等效项是 nul (single l),所以 winscp_cfg.ini 对于您的情况是一个名为 null
    • 的文件
© www.soinside.com 2019 - 2024. All rights reserved.