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

时间:2022-01-05 02:19:41

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.

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

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

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

Here is my code

这是我的代码

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\

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

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

Please suggest a way to enter the file name dynamically.

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

Thanks in advance.

提前致谢。

1 个解决方案

#1


1  

Use an argument for the batch file

使用批处理文件的参数

Code (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"

Notes:

  • 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
  • 该参数被称为“%~1”。检查[SS64]:命令行参数(参数)以获取更多详细信息

  • The if clause at the beginning is to verify if the argument was provided, if not simply display an error message and exit
  • 开头的if子句是验证参数是否已提供,如果不是简单地显示错误消息并退出

  • When dealing with paths, it's always better to dblquote them, as a SPACE in the path might completely mess things up
  • 在处理路径时,dblquote它们总是更好,因为路径中的SPACE可能会完全弄乱

Other ways:

  • 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 :) )
  • 让用户从脚本中输入文件名(通过set / p),如@ ramu246的评论(我错过了这个:) :)

@EDIT0:

  • 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)
  • 在看了@ MartinPrikryl的评论并做了一些测试之后,结果证明你的.bat文件是完全废话(因此它是我的,因为它是基于你的)

  • 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

Output:

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

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

e:\Work\Dev\*\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\*\q047989047>dir /b
script.bat
test with spaces.log
winscp_cfg.ini
winscp_script.txt

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

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

e:\Work\Dev\*\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\*\q047989047>dir /b
script.bat
test with spaces.log
winscp_cfg.ini
winscp_script.txt

Notes:

  • 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
    • 正如您在输出中看到的,第一次需要用户确认((Y)es,(N)o,C(a)ncel ....),以便生成文件,下次它只需使用它

    • 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
    • 同样的事情发生在你身上(我假设你想跳过.ini文件名),但是由于一个错误:Win中的Ux / dev / null等价物是nul(单个l),所以你的案例中的winscp_cfg.ini是一个文件名为null

  • 需要.ini文件(winscp_cfg.ini)才能传递主机的指纹。也可以为open命令传递-hostkey参数([WinSCP]:open),但是我没有成功(我也没有尝试太多)正如你在输出中看到的,第一次它需要用户确认( (Y)es,(N)o,C(a)ncel ....),为了生成文件,下次它只是使用它同样的事情发生在你身上(我假设你想跳过它。 ini文件名),但是由于一个错误:Win中的Ux / dev / null等价是nul(单个l),所以你的case的winscp_cfg.ini是一个名为null的文件

#1


1  

Use an argument for the batch file

使用批处理文件的参数

Code (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"

Notes:

  • 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
  • 该参数被称为“%~1”。检查[SS64]:命令行参数(参数)以获取更多详细信息

  • The if clause at the beginning is to verify if the argument was provided, if not simply display an error message and exit
  • 开头的if子句是验证参数是否已提供,如果不是简单地显示错误消息并退出

  • When dealing with paths, it's always better to dblquote them, as a SPACE in the path might completely mess things up
  • 在处理路径时,dblquote它们总是更好,因为路径中的SPACE可能会完全弄乱

Other ways:

  • 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 :) )
  • 让用户从脚本中输入文件名(通过set / p),如@ ramu246的评论(我错过了这个:) :)

@EDIT0:

  • 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)
  • 在看了@ MartinPrikryl的评论并做了一些测试之后,结果证明你的.bat文件是完全废话(因此它是我的,因为它是基于你的)

  • 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

Output:

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

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

e:\Work\Dev\*\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\*\q047989047>dir /b
script.bat
test with spaces.log
winscp_cfg.ini
winscp_script.txt

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

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

e:\Work\Dev\*\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\*\q047989047>dir /b
script.bat
test with spaces.log
winscp_cfg.ini
winscp_script.txt

Notes:

  • 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
    • 正如您在输出中看到的,第一次需要用户确认((Y)es,(N)o,C(a)ncel ....),以便生成文件,下次它只需使用它

    • 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
    • 同样的事情发生在你身上(我假设你想跳过.ini文件名),但是由于一个错误:Win中的Ux / dev / null等价物是nul(单个l),所以你的案例中的winscp_cfg.ini是一个文件名为null

  • 需要.ini文件(winscp_cfg.ini)才能传递主机的指纹。也可以为open命令传递-hostkey参数([WinSCP]:open),但是我没有成功(我也没有尝试太多)正如你在输出中看到的,第一次它需要用户确认( (Y)es,(N)o,C(a)ncel ....),为了生成文件,下次它只是使用它同样的事情发生在你身上(我假设你想跳过它。 ini文件名),但是由于一个错误:Win中的Ux / dev / null等价是nul(单个l),所以你的case的winscp_cfg.ini是一个名为null的文件