LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-14-2012, 02:49 PM   #1
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Rep: Reputation: Disabled
handling failed ftp session


Hi all: In the below code, what is the recommended way of handling a failed connection? I will be opening connections to many servers and need to test and exit if the connection fails.

psuedo-code:
open ftp connection
if connection open - proceed
if connection closed - gracefully exit and proceed to next server

Thanks, Chris.


Code:
function get_host_file_list {
	ftp_param="ftp://$1:$2@$3"
	ftp -inv $ftp_param << EOF
	!mkdir /opt/vra/ftp
	ls . /opt/vra/ftp/$HOSTNAME.txt
EOF
}
 
Old 02-14-2012, 03:26 PM   #2
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, Debian, EasyPeasy, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,436
Blog Entries: 10

Rep: Reputation: 98
I would suggest using a different program such as "wget" that focuses on getting the file(s) you want. Just give it an "FTP URL". If it returns with a status code, that can help you determine the difference between cannot access server vs. file not found. In any case, if the file did not get downloaded, it might be your desire to move on to the next server or next URL regardless.

A plus with "wget" is that if, in the future, you need to get something from an HTTP server, it's just a matter of changing the URL.
 
Old 02-15-2012, 08:16 AM   #3
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Without getting into specifics, there is a lot of processing that goes on that involves generating a list of available files, changing their names, and then downloading into date specific directories. I really need to test if the attempted connection failed or not before continuing the script.
 
Old 02-15-2012, 11:18 AM   #4
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Here is my solution. I basically save the ftp output to a temp file. Once the work is done I test this file; if empty I know the connection failed.

Code:
# Function to open a ftp connection and retrieve a listing of available files
# on the target saved in a txt file.
# Arguments: username, password, host.
function get_host_file_list {
	ftp_param="ftp://$1:$2@$3"
	ftp -inv $ftp_param >/tmp/ftp.output << EOF
	ls . /opt/vra/ftp/$3.txt
	bye
EOF
if [ -s /tmp/ftp.output ]; then
	echo "true"
else
	echo "false"
fi
}

Last edited by cmosentine; 02-15-2012 at 11:50 AM.
 
Old 02-15-2012, 12:11 PM   #5
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, Debian, EasyPeasy, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,436
Blog Entries: 10

Rep: Reputation: 98
Quote:
Originally Posted by cmosentine View Post
Without getting into specifics, there is a lot of processing that goes on that involves generating a list of available files, changing their names, and then downloading into date specific directories. I really need to test if the attempted connection failed or not before continuing the script.
I presume the issue is that if obtaining the list of files is incomplete, the rest of the script operating on that as if it were complete would cause new or more problems, possibly losing or corrupting data. The determination of whether the list is complete or not would not be trivial. You'd have to parse error messages and/or result codes. This is where I've personally abandoned FTP.

Other programs can do a better job of getting a list of files and even transferring those files. If the other host is not limited to just FTP (for example, you have ssh access to the appropriate user with access rights to the files involved), then that opens a wider scope of better tools.

If you are definitely staying with FTP, I probably won't be able to help any further, since I have not used FTP for anything important for years.
 
Old 02-15-2012, 02:13 PM   #6
lithos
Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA, trying desktop Fedora & openSuse
Posts: 855

Rep: Reputation: 133Reputation: 133
or,
you can use lftp which has options to retry failed connections
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
failed session setup with NT_STATUS_LOGON_FAILURE a1danel Linux - Newbie 7 03-23-2012 06:55 AM
PHP Smarty session handling vargadanis Programming 2 12-15-2006 10:37 AM
VNC Session Handling azzurro Linux - Networking 16 09-21-2004 04:43 PM
Session failed to start borrrden Fedora - Installation 1 06-03-2004 06:51 PM
handling ftp?? 77hh77 Linux - Newbie 1 04-10-2003 04:18 PM


All times are GMT -5. The time now is 04:56 AM.

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.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration