SFTP Shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

SFTP Shell script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-20-2012
Arpit Narula Arpit Narula is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 24 May 2012, 11:25 PM EDT
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
SFTP Shell script

Hi All,

I have done the Private - Public keys generation( and sharing) and have written a script for automating the SFTP. Need to make sure if it will work:


Code:
sftp.sh
#!/bin/bash 
. config.ini
sftp $SFTP_USER@$SERVER <<EOF 
cd $SFTP_RDIR
#ls -lrt | grep $SFTP_RFILE | wc -l
get $SFTP_RFILE
bye 
EOF
 
config.ini
export SFTP_USER = user_name
export SFTP_SERVER = remote_server
export SFTP_RDIR = /home/dir/
export SFTP_RFILE = sample.txt

1. Can I use exported varibles in sftp commands.
2. Is there a way to check if file exists before get ing it.

Thanks in Advance

Moderator's Comments:
Please use next time code tags for your code and data

Last edited by vbe; 03-20-2012 at 09:54 AM.. Reason: code tags
Sponsored Links
    #2  
Old 03-20-2012
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 31 October 2014, 2:57 PM EDT
Location: NM
Posts: 10,258
Thanks: 285
Thanked 803 Times in 747 Posts
1. yes.
2. Not like what you would want. Try the ls command that sftp offers


Code:
sftp $SFTP_USER@$SERVER <<EOF | grep -q 'No such file'
cd $SFTP_RDIR
ls $SFTP_RFILE
bye 
EOF
if [ $? -eq 1 ] ; then
  echo 'no such remote file'
else
sftp $SFTP_USER@$SERVER <<EOF 
cd $SFTP_RDIR
get $SFTP_RFILE
bye 
EOF
 
fi

Sponsored Links
    #3  
Old 03-23-2012
Arpit Narula Arpit Narula is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 24 May 2012, 11:25 PM EDT
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks a Lot Jim. That works.

I have another question:
On Server A, I generated the Public Private keys.
and shared the Public Key in /authorised_keys file on Server B.

Now can I do the passwordless communication from Server B(having public key only) to Server A(having Public as well as Private key)
as well....Or it is only possible from Server A to Server B

---------- Post updated 03-23-12 at 04:59 AM ---------- Previous update was 03-22-12 at 06:07 AM ----------

Please anyone.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SFTP-how to log individual sftp command error while executing shell script noobrobot Shell Programming and Scripting 1 10-09-2011 07:17 PM
SFTP connection using shell script Satyak Shell Programming and Scripting 2 05-17-2010 03:15 AM
Reg sftp in shell script vidhya_vec Shell Programming and Scripting 3 11-25-2008 11:30 PM
Shell Script for SFTP rupeshk Shell Programming and Scripting 4 09-27-2008 07:22 PM
SFTP shell script help madankumar Shell Programming and Scripting 1 07-26-2008 08:34 AM



All times are GMT -4. The time now is 10:35 PM.