I want to run a batch file which can connect automatically to SFTP Server running on Linux and can download files from Linux remote side to my local side. Besides that, this file will be scheduled to run automatically on every month. But, I'm facing a problem to connect it automatically? Below is one of my example of testing it:

set username=micro
set password=test
set r_dir=/home/micro/output
set l_dir=c:/temp
set filename=CDR-2005-05.csv
set tempSftpScript=script.batch

echo user %username%_%password%>>%tempSftpScript%
echo cd %r_dir%>>%tempSftpScript%
echo lcd %l_dir%>>%tempSftpScript%
echo get %filename%>>%tempSftpScript%
rem echo bye>>%tempSftpScript%

CALL c:\cygwin\bin\sftp.exe -b:%tempSftpScript%

if I just run the following code:

CALL c:\cygwin\bin\sftp.exe

Then, it can connect automatically but also need to enter password manually.


Is there any way to connect to SFTP and download file automatically from Linux?


Your reply will be appreciated.

Thank You in advance,
Teing

Dani AI

Generated

Short answer: run a scheduled Windows job that uses SSH key (public-key) authentication or a supported scripting tool (WinSCP/psftp) rather than trying to feed an interactive password into sftp. ’s batch approach and the Cygwin sftp batch file will not accept an interactive password when run unattended, and ’s cron suggestion only helps if the remote Linux box can be modified (which it cannot here). ’s later follow-up has the same root cause.

Recommended, reliable flow (high level):

  • Create an SSH key pair on the Windows machine (OpenSSH under Cygwin/Windows or PuTTYgen). Prefer protecting the private key with a passphrase and using an agent (ssh-agent or Pageant) for unattended jobs.
  • Install the public key on the Linux account (append to ~/.ssh/authorized_keys and set correct permissions).
  • Test a non-interactive SSH/SFTP login from the Windows account. Once that works, run a scripted transfer and schedule it with Windows Task Scheduler to run monthly.

Example commands (OpenSSH/Cygwin style):

ssh-keygen -t rsa -b 2048 -f "%USERPROFILE%\.ssh\id_rsa"   # create key pair
scp "%USERPROFILE%\.ssh\id_rsa.pub" user@host:~/temp_pub
ssh user@host "mkdir -p ~/.ssh && cat ~/temp_pub >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys && rm ~/temp_pub"

If a GUI/script tool is preferred, WinSCP works well. Example WinSCP script:

# script.txt
open sftp://username@host/ -privatekey="C:\keys\id_rsa.ppk"
get /home/username/output/CDR-2005-05.csv C:\temp\
exit

Call from a batch file:

winscp.com /script=C:\path\script.txt /log=C:\path\winscp.log

Schedule that batch with Task Scheduler.

Troubleshooting & cautions:

  • Do not store plaintext passwords in scripts. That is insecure.
  • If key login still prompts for a password, check permissions on the remote ~/.ssh and authorized_keys, and check sshd_config (PubkeyAuthentication). Use ssh -v for verbose debug.
  • As a last resort, expect/psftp -pw can automate a password but are brittle and insecure; prefer keys.

Recommended Answers

All 3 Replies

You could write a shell script and have it run via the cron :)

To: Zachery

Thank you for your reply and suggestion. You suggested to write shell scripts & run it via the cron job.

For your information, I have no access permission to the remote server Linux to create cron job due to the server is from the other party. But my task is just want to access to the server and download an csv file to my local side.

I am using windows platform, can i do all this task and run it by shell script?

I'm not very good at Linux, or is it possible to run by batch files?

And, Because of the schedule task is same as cron job, I just think to run the schedule task which will run the shell script/batch file.


Pls: is it possible to give me sample of program which can perform this tasks?


Thanks very much & Apologize for all the disturb & mistake that I have done.

Teing

I am trying to setup the same process and having the same issue! Have you discovered a solution?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.