Link to home
Start Free TrialLog in
Avatar of jajen
jajenFlag for United States of America

asked on

Create a way to Download SFTP Files

I have an FTP program that allows me to schedule times for that runs scripts so we connect to FTP sites and download documents into folders that are created that day.  We recently had a change with one of the sites so that it is no longer just FTP it is encrypted and using WinSCP.  This works if someone manually connects and finds the files and downloads them, but I am tasked with making this happen again automatically.  I have run into the problem with my existing program not having a way to enter the passphrase for the key that is required after the initial login name and password.  I have read about scripts that could be created and used within WINSCP but none that address my problem.  I have looked at other software packages but found none so far that will work.  Does anyone have a script that will allow the login with the username, password and then after screen comes up asking for passphrase it is entered so that I can try to salvage the pieces of my existing script which creates a new folder each day using the date as the folder name and downloading the files from the site.  I am not a script person which is part of my problem, but I can understand the basics so if someone could share this info or perhaps let me know if there is a program that will do this it would be greatly appreciated.
Avatar of mankowitz
mankowitz
Flag of United States of America image

start with

winscp.com /log=ftpcmd.log /command ^
    "open sftp://ftp_user:password@ftp.MyFTPSite.com -hostkey=""whatever""" ^
    "get c:\patjh\to\file" ^
    "exit"
Avatar of jajen

ASKER

I tried following your info and when I ran it I saw the cmd screen for half a second and then nothing - so I am pretty sure it did not work.  Here is redacted script I used-

winscp.com /log=ftpcmd.log /command
     "open sftp://ftp_XX:XXX@-XXX -hostkey=" XX" -privatekey="C:\WinSCP\XXKeys\XXppk""" ^

     exit
look in the log file "ftpcmd.log" or run from the command line to see the results.
You need to create a connection first using the winscp gui and save the session with password. Then create a script where you refer to that session. No need for entering the password anymore. The winscp scripting guide is here: https://winscp.net/eng/docs/scripting
Avatar of jajen

ASKER

I have tried various ways to create a script that works using the winscp scripting guide but no luck.  I think it may be because there is actually a screen that pops up after the username and password is entered and it looks at the path to the key, you then have to enter in the passphrase.  I have generated winscp script after doing the process manually and still no luck.  This may be because I am a novice at creating scripts so any additional help would be greatly appreciated.
Avatar of jajen

ASKER

I have tried a few different SFTP solutions and still cannot get this to work, I have had some people tell me that when there is a passphrase a password is not needed but that is not true for this site.  I have tried using the generate session code in WinSCP but it is not capturing where the passphrase must be entered, I believe this is because this is a new window that pops up after already authenticating to the site with username and password.  I have attached a doc to this question showing that after the initial login and password are accepted there is one window that pops up randomly and then the other that asks for the passphrase.  I am told we can get the random window to stop so the big issue is how to get the passphrase to work. You cannot get to this point without a password and cannot get in without passphrase.
I'll dig up an example for you later today, from when I used this a while ago.
Avatar of jajen

ASKER

that would be great - I just tries using winscp console and tried using the steps and it accepted username and password but did not even give a prompt or chance for passphrase just authentication error  I have attached
What I had wat this (couldn't find the files anymore):

- Create an sftp connection (your_sftp_connecion) in Winscp gui, save the password
- Create a script (txt file - yourscript.scp) that contains:

open your_sftp_connecion
cd /your/remote/directory
get *.*
exit

- Call winscp from commandline (cmd.exe) to get the file(s):
winscp /script=yourscript.scp /log=yourscript.log

Make sure that winscp.com, winscp.exe and winscp.ini are in the folder where you have the script. the winscp.ini contains the (encrypted) password for your_sftp_connecion. I believe we also exchanged ssh keys with the remote site to prevent password popups. Not sure about that though. Can you test an see whether you run into issues?
ASKER CERTIFIED SOLUTION
Avatar of jajen
jajen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial