运行带有批处理文件的Unix Shell脚本以从WinSCP下载文件 [英] Running Unix shell script with batch file to download files from WinSCP

查看:98
本文介绍了运行带有批处理文件的Unix Shell脚本以从WinSCP下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to download a file from WinSCP through batch file. I am able to download the file if I enter the file name in the batch file.

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

But I need to enter the file name dynamically (i.e., file name has to be entered at run time).

这是我的代码

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是我在批处理文件中提供的文件名.

here Test.log is the file name I am providing in the batch file.

请提出一种动态输入文件名的方法.

Please suggest a way to enter the file name dynamically.

谢谢.

推荐答案

batch 文件使用参数

代码( 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 可能会完全弄乱
  • It's the most straightforward way to do this
  • The argument is referred to as "%~1". Check [SS64]: Command Line arguments (Parameters) for more details
  • The if clause at the beginning is to verify if the argument was provided, if not simply display an error message and exit
  • When dealing with paths, it's always better to dblquote them, as a SPACE in the path might completely mess things up

其他方式:

  • 使用从脚本外部设置的环境变量(而不是参数)
  • 让用户从脚本中输入文件名(通过set /p),就像@ ramu246的注释所指出的那样(我错过了这个:))
  • Use an environment variable (instead of the argument) set from outside the script
  • Let the user input the file name (via set /p) from the script, as pointed by @ramu246 's comment (I missed this one :) )

@ EDIT0 :

@EDIT0:

  • 在查看@MartinPrikryl的评论并进行了一些测试之后,结果发现您的 .bat 文件是完全废话(我也是,因为它是基于您的)
  • 但是,即使无法解决,该修复程序也仍然可以(因为我盲目地应用了它-请检查前面的项目符号)
  • After looking at @MartinPrikryl's comment, and doing some tests, it turns out that your .bat file is total crap (so is mine, since it's based on yours)
  • However, the fix is still OK, even if it doesn't work (because I applied it blindly - check the previous bullet)

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

I did some changes, and below is a version that really works (of course the sensitive data has been altered).

script.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:password@127.0.0.1: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] cfati@127.0.0.1
/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] cfati@127.0.0.1
/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 )才能通过主机的指纹.也可以为open命令传递-hostkey参数( [WinSCP]:打开 ),但我没有成功(我也没有尝试太多)
    • 您会在输出中看到,在1 st 的时间内,它需要用户确认((Y)es, (N)o, C(a)ncel....)才能生成文件,并且下次仅使用它
    • 您遇到了同样的事情(我假设您想跳过 .ini 文件名),但是由于一个错误: Ux / Win 中的dev/null 等效为 nul (单l ),因此 winscp_cfg您的案例中的.ini 是一个名为 null
    • 的文件
    • This time I'm using my own paths
    • The .ini file (winscp_cfg.ini) is required in order to pass the host's fingerprint. It's also possible to pass -hostkey argument for open command ([WinSCP]: open), but I wasn't successful (I didn't try too much either)
      • As you can see in the output, 1st time it requires user confirmation ((Y)es, (N)o, C(a)ncel....), in order to generate the file, and next times it simply uses it
      • Same thing happened for you (I assume that you wanted to skip the .ini file name), but due to a mistake: Ux's /dev/null equivalent in Win is nul (single l), so winscp_cfg.ini for your case was a file called null

      这篇关于运行带有批处理文件的Unix Shell脚本以从WinSCP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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