LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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
 
Thread Tools Search this Thread
Old 06-30-2009, 07:45 AM   #1
solar05
LQ Newbie
 
Registered: Jun 2009
Posts: 9
Thanked: 0
Bash and sftp


[Log in to get rid of this advertisement]
Hello, I need to copy every hour some files from local directory to remote vis sftp.
I open 1 session for all files and copy them to remote host with mput.
But if the copying would be wrong - for example connection would be lost. How I can know abouit it in the Bash.

PHP Code:
#!/bin/bash
OUTDIR="/outdir"
TARGET_HOST=localhost
TARGET_USER
=root
TARGET_DIR
="/indir"
DATE_LOG=`date +%R-%d-%m-%Y`
FTPLOG="/log/$DATE_LOG.log"
SLEEP_INTERVAL=60
while [ ]; do
sftp -/dev/fd/0 $TARGET_USER@$TARGET_HOST>>$FTPLOG 2>&1<<EOT
lcd $OUTDIR
mput 
*.zip
chmod 666 
*.zip
exit
EOT
sleep $SLEEP_INTERVAL
done 
solar05 is offline     Reply With Quote
Old 06-30-2009, 08:14 AM   #2
nuwen52
Member
 
Registered: Feb 2009
Distribution: CentOS 5, Gentoo, FreeBSD, Fedora, Mint
Posts: 155
Thanked: 23
First, in timed things like this, you might consider building this as a cron job. It can make the script less complex and gets rid of the need of another programming running all of the time.

Now then, to answer your actual question. After:
sftp -b /dev/fd/0 $TARGET_USER@$TARGET_HOST>>$FTPLOG 2>&1<<EOT
try checking the exit status of sftp. Like:
Code:
RET=$?
if [ $RET -eq 0 ]
then
    it worked
else
    it didn't
fi
"$?" is the return code for the previously executed process. Usually, if it's zero, everything is fine.

Last edited by nuwen52; 06-30-2009 at 08:17 AM..
nuwen52 is offline     Reply With Quote
Old 07-01-2009, 12:44 AM   #3
solar05
LQ Newbie
 
Registered: Jun 2009
Posts: 9
Thanked: 0

Original Poster
Quote:
Originally Posted by nuwen52 View Post
First, in timed things like this, you might consider building this as a cron job. It can make the script less complex and gets rid of the need of another programming running all of the time.

Now then, to answer your actual question. After:
sftp -b /dev/fd/0 $TARGET_USER@$TARGET_HOST>>$FTPLOG 2>&1<<EOT
try checking the exit status of sftp. Like:
Code:
RET=$?
if [ $RET -eq 0 ]
then
    it worked
else
    it didn't
fi
"$?" is the return code for the previously executed process. Usually, if it's zero, everything is fine.
Yes, it may be use when the connection will fail, but if while during
sftp session working on remote computer and there script could'nt copy some files or some permissions incorrect. And that its why sftp will give
Code:
RET=$?
TRUE but files didn't copy to remote host. How I can see that all files copyed successfully?
solar05 is offline     Reply With Quote
Old 07-01-2009, 01:37 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 8,110
Thanked: 401
If you want to know the status of the copy and chmod on a file-by-file basis, you'll need to replace the while loop with

for file in *.zip
sftp ....
lcd ..
put $file

ie perform the copy, then exit and check the status, then do another sftp cxn and run the chmod cmd and check it.
chrism01 is online now     Reply With Quote
Old 07-01-2009, 02:28 AM   #5
solar05
LQ Newbie
 
Registered: Jun 2009
Posts: 9
Thanked: 0

Original Poster
Quote:
Originally Posted by chrism01 View Post
If you want to know the status of the copy and chmod on a file-by-file basis, you'll need to replace the while loop with

for file in *.zip
sftp ....
lcd ..
put $file

ie perform the copy, then exit and check the status, then do another sftp cxn and run the chmod cmd and check it.
Yes it's good desicion, but I want to copy all files in 1 sftp session, and don't use per each file each sftp session
solar05 is offline     Reply With Quote
Old 07-01-2009, 02:36 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 8,110
Thanked: 401
Then you can't do it... in SFTP
Look, you can either send & chmod all the files in one go, then use an ssh session to do a remote 'ls -l' (assuming you can ssh in), or you check each operation as you go.
Your choice.

Another way that might do what you want with one session would be to use one of the Perl SSH modules. I think you'd be able to do an scp within the ssh session, as well as the chmod cmd, and check as you go.
chrism01 is online now     Reply With Quote

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
sftp chili811 Programming 1 02-25-2009 11:20 AM
how to create sftp user only in red hat 4 not ftp user ..only sftp user princeu28 Linux - Newbie 1 10-14-2008 09:10 AM
cannot login into SFTP server using Net::SFTP cccc Programming 1 10-31-2007 07:23 AM
How do I use sftp to upload my web site? (no sftp tar command) johnMG Linux - Networking 6 06-21-2005 10:14 PM
Files truncated by sftp/sftp-server at 65kb gato Linux - Networking 1 12-18-2003 11:29 AM


All times are GMT -5. The time now is 06:02 PM.

Main Menu
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.
Advertisement
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Click Here to receive a complimentary subscription courtesy of LQ.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration