FTP script to login and list files to log file

Hi Guys

I did a forum search for "ftp scripts" Looked at 8 pages and didnt see anything that would help. Most seem to be logging into a ftp server and transfering files.

What I need to do is login to a FTP server. Goto a folder and list it so it showes newest files first. It would be nice to set it to show only the last 10 newest but it doesnt matter if it's all. The said FTP server is an Amiga server for abandonware games so sometimes there can be alot.

The script I came out with is as follows:

#!/bin/bash
lftp -p 21 -u anon,anon site/amiga
ls -lat &> /home/voorhees/scripts/ftp.log
exit
leafpad /home/voorhees/scripts/ftp.log

But as soon as anyone see's this they will know it won't work :slight_smile:

What happens is is it logs into the server fine it goes to the amiga folder then just sits there.

It doesnt even matter if it doesnt log to a file as the script runs this in a terminal so I can actually see the output anyway. I would prefer it to log the txt for the last 10 newest entries then open the log file afterwards.

Any help would be great. Thanks for reading.

Untested. I have to assume that the "lftp" line posted is correct and that the "-i" (don't ask for confirmation) is allowed in "lftp".
Here is the essence of what to do:
Redirect an input stream to ftp containing what you would have typed. "EOF" terminates that stream. The ftp program can output a directory listing to a file named in the second parameter of a DIR command. The full stop in the DIR line is mandatory.

#!/bin/bash
cd /home/voorhees/scripts
lftp -i -p 21 -u anon,anon site/amiga <<EOF
DIR . ftp.log
QUIT
EOF
#
leafpad ftp.log

The order and format of the directory listing depends on settings on the remote server. If you are lucky you could use unix "head" or "tail" on the log, otherwise it is very difficult to sort by timestamp.

Hi

Many Thanks for the reply. Seems lftp does not like -i

lftp: invalid option -- 'i'

It doesnt have to be lftp. its just the program I grew fond off. As it is it logs the contents from my own home dir. ie leafpad opens up with the dir from /home/voorhees

Thanks again for any help

EDIT:

This will do:

#!/bin/bash
cd /home/voorhees/scripts
lftp -p 21 -u anon,anon site/amiga <<EOF
ls -lat
DIR . ftp.log
QUIT
EOF
#
leafpad ftp.log

Logs in does ls -lat and shows new files on the terminal, but wont log them to the file. still shows home folder contents.

---------- Post updated at 04:13 PM ---------- Previous update was at 03:47 PM ----------

Ok

Almost solved:

#!/bin/bash
cd /home/voorhees/scripts
lftp -p 21 -u anon,anon site/amiga <<EOF
ls -lat | tee amiga.txt
QUIT
EOF
leafpad /home/voorhees/scripts/amiga.txt

This does exactly what I want. BUT the terminal stays open with the used commands. Anyway to close the terminal before leafpad opens? I tried exit below QUIT but then leafpad wouldnt open.

There are two directory list commands in your ftp commands.

If the second command one on its own does not write to "ftp.log", then we must assume that the feature does not work in "lftp".

In this attempt we'll try to redirect any output to the log file.

#!/bin/bash
(
cd /home/voorhees/scripts
lftp -p 1857 -u jimbo,james annon.homeunix.org/xvid <<EOF
ls -lat
quit
EOF
) > ftp.log
#
leafpad ftp.log

Note: I have changed the ftp commands to lower case because some unixes don't like upper case.

Fixed

Thanks :slight_smile:

#!/bin/bash
cd /home/voorhees/scripts
lftp -p 21 -u anon,anon site/amiga <<EOF
ls -lat | tee amiga.txt
QUIT
EOF
leafpad /home/voorhees/scripts/amiga.txt
rm /home/voorhees/scripts/amiga.txt
killall roxterm

Copies all info to a txt file. Opens it up full screen in leafpad so you can see the newest first. Closing leafpad deletes the .txt file and closes roxterm.

Thanks Again