-1

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
ramu246
  • 51
  • 1
  • 8
  • 1
    The batch file is just nonsense. The `open`, `cd` and `get` should be in the `test.txt`, not in the batch file. What is `$ ssh`? – Martin Prikryl Dec 28 '17 at 07:48

1 Answers1

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 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
CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • Thanks a lot for your quick response. i also tried this option set /p id="Enter syslog name: " echo %id% 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 %id% C:\Users\Desktop\Downloading_logs\ it worked for me – ramu246 Dec 27 '17 at 10:24
  • That's one other way. Don't know how did I forget about this one. – CristiFati Dec 27 '17 at 10:33
  • Thanks a lot for your suggestion – ramu246 Dec 27 '17 at 11:55
  • Bear in mind, that although it might be the easiest way, the `set /p` approach it's not scalable, if you would need to run the script many times upon one file (or a list of files) entering the file name every time will easily become annoying (I'd say 20 times per day), while if running from console, just pressing the *Up arrow* on the keyboard and you'll have the previous command run. Also, if you want to automate stuff, user input is a NO GO. So, I'd suggest to go with the argument (you could create a wrapper that waits for user input, but the core implementation should be parametrized). – CristiFati Jan 23 '18 at 23:38
  • Hello all...I have another question related to this. The above script is running in my machine where it uses my ID and password. I don't want to share this bat files to my team. but they make use of this batch file to get the files. something like from their machine, they have to provide the server name and syslog file name, then they should able to downlaod the file I have one option encryption method. Is there any other method to do this – ramu246 Mar 14 '18 at 09:05
  • You mean you don't want to share the *winscp* script. It's understood you don't want to share your password with other people, and you don't have to. Just let everyone copy it on their machines and fill their credentials. Or you could enable passwordless ssh on the *Lnx* machine, and create a restricted user to share with your colleagues. But without the limitations/reasons I can't tell you more. Also i think it could be possible due to some *PowerShell* script to let the users access the script on your computer (without getting to its content), but that's a totally different question. – CristiFati Mar 19 '18 at 21:49
  • so let me re-phrase the question. A bat file which uses my credentials running in my machine. To run this Bat file, it requires additional software and server permission. That software is only available in my machine. we can't install that in all machines. Also the server permission we should not provide to all members in this case, how can other members utilize this Bat file being kept that bat file in my machine. (I can't use encryption method as it still requires software. ) please suggest any other way – ramu246 Apr 30 '18 at 09:13
  • Ok, this is a requirement that doesn't have anything to do with the original question. I should have replied to your previous comment: if you have another question, please submit one. Because, as I said the 2 questions (even if they are 2 parts of your bigger problem) are unrelated. – CristiFati Apr 30 '18 at 17:39