LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices



Reply
 
Search this Thread
Old 09-02-2011, 02:17 AM   #1
pavan06
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

#!/bin/sh
# sample automatic ftp script to dump a file
USER=<user_name>
PASSWORD=<pwd>
HOST=<server_name>
TGT_DIR=$BASE_DIR/FromCheetah_automation
COA_DIR=$BASE_DIR/FromCheetah_bkp/archive
LDR_DIR=$BASE_DIR/FromCheetah_bkp/archive/Loader_Files
UNSUB_DIR=$BASE_DIR/FromCheetah_bkp/archive/Unsubs_Files
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
$PASSOWRD
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
quit
EOF
 
Old 09-02-2011, 02:30 AM   #2
hi2arun
Member
 
Registered: Apr 2010
Distribution: Fedora
Posts: 109
Blog Entries: 4

Rep: Reputation: 33
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
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Ubuntu
Posts: 199

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
SL00b
Member
 
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
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
I'd like to emphasize this approach for security reasons
Quote:
All three can be used with key exchange authorization, eliminating the need for a password.
 
Old 09-06-2011, 09:06 PM   #6
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,197

Rep: Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642
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:
http://linuxproblem.org/art_9.html
 
  


Reply


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


All times are GMT -5. The time now is 01:53 AM.

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