Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-02-2011, 02:17 AM   #1
LQ Newbie
Registered: Sep 2011
Posts: 1

Rep: Reputation: Disabled
Passwordless sftp in shell script

I want copy files from one linux to other linux machine (local) using sftp daily. So i have written a shell script to perform this but it prompts password when executed though i have hardcoded the pwd in the script. I found that by exchanging keys i perform this but please let me know is there any other solution

Find the script below

# sample automatic ftp script to dump a file
dt=`date +%Y%m%d`
fldt=`date --date "$dt 1 days ago" +%Y%m%d`
lgdt=`date +%Y%m`
echo "today is $dt"
echo "File dat is $fldt"
sftp $USER@$HOST << EOF
cd working_dir
mget iid_keys_$fldt*.dat.txt $TGT_DIR >> $LOG_DIR/emailftp_log_$lgdt.log
mget data_$fldt*.dat.txt $TGT_DIR >> $LOG_DIR/emailftp_log_$lgdt.log
Old 09-02-2011, 02:30 AM   #2
Registered: Apr 2010
Distribution: Fedora
Posts: 109
Blog Entries: 4

Rep: Reputation: 34
Hi -

Use 'expect' in shell script to handle password and any other interactive stuffs.

'Expect' will simplify the task for you.
1 members found this post helpful.
Old 09-02-2011, 02:33 AM   #3
Registered: Mar 2007
Location: Spain
Distribution: Debian
Posts: 202

Rep: Reputation: 36
You can also use lftp, it will allow you access to ftp, ftps, sftp and it will do the job for you. I find it very usefull.
Old 09-02-2011, 09:33 AM   #4
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
Other methods to perform this task involve scp and rsync. Of all the methods discussed here so far, rsync will get you the best performance. Both scp and rsync are easier to use in a cron job than ftp/sftp, too. The commands will fit on a single line.

All three can be used with key exchange authorization, eliminating the need for a password.
Old 09-04-2011, 08:49 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,340

Rep: Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745
I'd like to emphasize this approach for security reasons
All three can be used with key exchange authorization, eliminating the need for a password.
Old 09-06-2011, 09:06 PM   #6
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
also sftp is for human prompts (e.g.- please enter user name, please enter password, please enter local file, please enter remote file, ...)
scp is for automated machines(e.g.- scp user1@server1:/path/to/source user2@server2:/path/to/dest)

ssh pke:


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script using sftp soumyacs Linux - Newbie 4 11-30-2009 04:51 AM
Automating sftp using a shell script bhandu Linux - Newbie 3 07-17-2007 02:56 AM
Passwordless sftp to many remote hosts ahmad_abdulghany Linux - Networking 7 04-04-2007 07:18 PM
SFTP in shell script jantman Linux - Software 3 11-27-2006 11:12 PM
sftp over shell script naughtymutt Linux - General 1 08-20-2004 06:56 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:36 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration