LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-10-2013, 01:18 PM   #1
nauliv
LQ Newbie
 
Registered: Jun 2003
Distribution: RedHat
Posts: 5

Rep: Reputation: 0
Testing for lftp transfer result (in bash script)


Hello Folks!

Hoping you can help me with this one... I have a bash scripts that creates a set of instructions for lftp, which is then passed onto lftp for processing... Here is the script:

echo open 10.10.1.55 -u my_user,my_pass > $LSCRIPT
echo put -E $ARCHIVE >> $LSCRIPT
/usr/bin/lftp -f $LSCRIPT --log=$LOGFILE

This lftp transfer could be failing for several reasons (ftp server unreacheable, password changed, disk full, etc.); and I'm trying to find a way to test for a successful transfer and send an e-mail alert in case of any failure.

Unfortunately I can't seem to find a way to test for that. The error log only contains a list of files transferred but no particular message or exit code I could test for.

I guess if the log file is empty I have a way to test for connection issue, but I cannot rely on the number of lines in the log to find out if all files made it, as I don't know the number of files ahead of time...

The man page mentions a "debug 3" in /etc/lftp.conf which I put in, but there is still nothing in the log file to test for...

Has anyone been confronted with this issue before ? Any suggestion would be great !!

Thanks All !
 
Old 03-10-2013, 01:35 PM   #2
alleyoopster
Member
 
Registered: Jul 2008
Location: Cape Town
Distribution: Debian testing / unstable
Posts: 38

Rep: Reputation: 0
Debug in lftp

debug 3 just restricts the level of debug.

maybe this will help (from man lftp) by entering debug as a command to lftp in the script before it is processed:

Quote:
debug [-o file] level|off

Switch debugging to level or turn it off. Use -o to redirect the debug output to a file.

Last edited by alleyoopster; 03-10-2013 at 01:37 PM. Reason: deleted duplication
 
Old 03-12-2013, 10:41 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


I'm not sure I can address the actual problem posted, but I'd like to comment on the script itself.

To start with, I take it that "$LSCRIPT" is the name of a temporary file? What is "$ARCHIVE"? The file to be transferred? Is it just one file, or can it contain multiple entries (in which case you should be using an array)?


My main concern is that you should really never hard-code usernames and passwords into a script. Not only do you do that here, but you then immediately echo them into another text file, so now you have two copies of them floating around!

One way to (relatively) safely handle passwords in scripts is to store them in a separate file that's only readable to the user running the script. Then have the script source that file to import the values.

e.g. make a file that looks like this, in the user's home directory. Set it so that only the user has read permission for it.
Code:
# This file contains the username and password for
# the lftp command in my script.
my_user=username
my_pass=password
Note that the contents should be in the standard variable-setting syntax.

Then in the script, use something like this:
Code:
if [[ -r "$HOME/passwdfile.txt" ]]; then
    . "$HOME/passwdfile.txt"
else
    echo "Password file unreadable.  Aborting." >&2
    exit 1
fi
This will import the variables named "$my_user" and "$my_pass" for use in your script.


Next, a big warning: QUOTE ALL OF YOUR VARIABLE EXPANSIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell and possible globbing patterns expanded. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Also, since environment variables are generally all upper-case, it's recommended practice to keep your own user variables in lower-case or mixed-case to help differentiate them.


Finally, the external file probably isn't necessary, at least not for the command shown above. At the very least you can use a named pipe or process substitution instead of the file.

Code:
# store the commands in an array variable first, for convenience.
# (each line needs to be quoted so that it gets set as a single entry):
lsscript=(
    "open 10.10.1.55 -u $my_user,$my_pass"
    "put -E $ARCHIVE"
)

# run lftp using printf on the array to create a simulated file.
/user/bin/lftp --log="$LOGFILE" -f <( printf '%s\n' "${lsscript[@]}" )
I imagine though that lftp can read the commands directly from stdin? If so, then you should be able to just use a simple here document instead.

Code:
# run lftp using a heredoc to send it the commands through stdin.
/user/bin/lftp --log="$LOGFILE" <<-SCRIPT
	open 10.10.1.55 -u $my_user,$my_pass
	put -E $ARCHIVE
SCRIPT
Note that using the extra hyphen in the "<<-" means that it will strip off any leading tab characters from the contents. This allows you to indent it for readability without worrying about affecting the contents.

Also notice that this is one of those places where you should NOT quote variables. Everything between the herdoc's end flags remains unparsed by the shell, with the exception of parameter/command expansion and the initial tab feature just mentioned.

Last edited by David the H.; 03-12-2013 at 10:43 AM.
 
  


Reply



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
bash script to lftp mirror and log zimbot Linux - General 6 09-23-2013 07:55 AM
[SOLVED] Silly mistake in lftp bash script probably quotes emmalg Linux - Software 3 02-08-2013 05:12 AM
calling lftp from a bash script; howto pass variables into the command syntax stvy Programming 2 01-09-2013 01:43 PM
[SOLVED] Bash Script - Strange Result with sed and Array. mrm5102 Programming 8 05-18-2012 02:15 PM
[SOLVED] BASH Script. Result of command input into variable bcyork Linux - Newbie 4 12-06-2011 12:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:43 PM.

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
Open Source Consulting | Domain Registration