The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script for how to connect to a remote server by using ssh millan Shell Programming and Scripting 1 01-24-2009 10:17 AM
SFTP Shell Script from a Unix server to another kumarm Shell Programming and Scripting 1 08-18-2008 10:21 AM
Pull a file from a remote server through a shell script sashankkrk UNIX for Dummies Questions & Answers 2 04-23-2008 05:13 PM
SFTP:error while creating folders on remote server vgs Shell Programming and Scripting 0 08-01-2007 05:01 PM
Get the most recent file from a remote server anujairaj UNIX for Dummies Questions & Answers 5 09-27-2006 11:31 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-20-2009
toobrown1 toobrown1 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 9
Use cURL in shell script and get most recent file from remote server using SFTP

I have been trying to implement the following shell script --

sftp to remote server
get list of files in the directory
sftp get the most recent listed file
exit

This script will be scheduled to be executed everyday using CRON or CONTROL-M and the need is to have absolutely no human interaction. This should be executed without having the need to have someone answer system prompts (eg: enter username and password, accept certificate etc.)

I was told that this could be achieved easily using cURL in the sh script. Can someone confirm that this cannot be done without cURL. Can someone help me write this script.
  #2 (permalink)  
Old 08-23-2009
KenJackson KenJackson is offline
Registered User
  
 

Join Date: May 2008
Location: Maryland, USA
Posts: 111
If you are running this script without human interaction, then you need to establish some criteria for what files to get.
Are you always retrieving files from the same directory on the remote server?
Are you always fetching the same number of files? Or all files modified since the last script?

Are you retrieving files from a public server? That is, do you need to supply a username and password, or can anyone retrieve the files.

If it's not a public server, and I assume it's not, do you have secure shell (SSH) configured? I would do that first. If you copy your public key from your PC to the server, you can use ssh, scp and sftp without a password. In a script, scp will be more convenient than sftp.

I would also checkout rsync. It's probably what you really want.
  #3 (permalink)  
Old 08-24-2009
toobrown1 toobrown1 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 9
Hi Ken, Thanks for the response. Let me elaborate my situation;

1. Are you always fetching the same number of files? -- always getting one file.
2. criteria for what files to get -- The most recent one i.e. the one with the most recent time stamp. File name will keep changing in an undefinable pattern.
3. Are you retrieving files from a public server? -- no, need to provide username and password.
4. ssh, scp and sftp without a password. In a script, scp will be more convenient than sftp. -- the server limits itself to sftp.

I will check out rsync. Thanks. Based on details above, any suggestions?
  #4 (permalink)  
Old 08-24-2009
KenJackson KenJackson is offline
Registered User
  
 

Join Date: May 2008
Location: Maryland, USA
Posts: 111
Maybe something like this:

Code:
#!/bin/sh
SERVER=example.com
DIR=some_directory
FILE="$(echo ls -t | sftp -b- $SERVER:$DIR | head -n1)"
echo get "$FILE" | sftp -b- $SERVER:$DIR

These individual commands worked for me with no username or password because I have SSH setup to the server. If you don't, I assume you'll have to add user@. I'm not sure how the password is passed.

Command ls -t lists the files in time order, so the most recent file is first. Note that if there are subdirectories that might be modified, one could end up as more recent than any file, which isn't what you want.

Switch -b- tells sftp to work in batch mode and accept commands from stdin, which is the ls command we just piped to it.

Command head -n1 runs on the local machine to catch just the name of the first file.

The syntax FILE="$(...)" runs a command on the local machine and captures the output into a variable named FILE.

Even if the server really doesn't allow ssh, you may still be able to setup SSH keys so that sftp works without a password. It's secure because you can only access it if you log into your local machine.
  #5 (permalink)  
Old 08-27-2009
toobrown1 toobrown1 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 9
Thank you very much. I will try this out and let you know how it went!
  #6 (permalink)  
Old 09-10-2009
toobrown1 toobrown1 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 9
Ken,

Since I am moving to a dir when I do the sftp, the FILE variable is getting populated with "Changing directory..." message. Can you help me with a workaround., please?

Amit
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0