问题
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).
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.
Please suggest a way to enter the file name dynamically.
Thanks in advance.
回答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 - 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
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 :) )
@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)
- 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\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
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 foropen
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
- As you can see in the output, 1st time it requires user confirmation (
来源:https://stackoverflow.com/questions/47989047/running-unix-shell-script-with-batch-file-to-download-files-from-winscp