ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
}
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.