LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-30-2009, 07:45 AM   #1
solar05
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Rep: Reputation: 0
Bash and sftp


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 
 
Old 06-30-2009, 08:14 AM   #2
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
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.
 
Old 07-01-2009, 12:44 AM   #3
solar05
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
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?
 
Old 07-01-2009, 01:37 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.
 
Old 07-01-2009, 02:28 AM   #5
solar05
LQ Newbie
 
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 07-01-2009, 02:36 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.
 
  


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 08:57 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration