FTP script to login and list files to log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FTP script to login and list files to log file
# 1  
Old 09-05-2009
SOLVED 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:

Code:
#!/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 Smilie

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.

Last edited by voorhees1979; 09-05-2009 at 06:26 PM..
# 2  
Old 09-05-2009
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.

Code:
#!/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.

Last edited by methyl; 09-05-2009 at 05:50 PM.. Reason: sort out cwd
# 3  
Old 09-05-2009
Hi

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

Code:
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:

Code:
#!/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:

Code:
#!/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.

Last edited by voorhees1979; 09-05-2009 at 06:24 PM..
# 4  
Old 09-05-2009
There are two directory list commands in your ftp commands.
Quote:
ls -lat
DIR . ftp.log
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.

Last edited by methyl; 09-05-2009 at 06:22 PM.. Reason: lost line in paste!
# 5  
Old 09-05-2009
Fixed

Thanks Smilie

Code:
#!/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

Last edited by voorhees1979; 09-05-2009 at 06:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with a shell script? List files, delete them and log them

Hello, i'm trying to solve this script. List, one at a time, all files larger than 100K in the /home/username directory tree. Give the user the option to delete or compress the file, then proceed to show the next one. Write to a logfile the names of all deleted files and the deletion times. I... (7 Replies)
Discussion started by: jose2802
7 Replies

2. Shell Programming and Scripting

Doubt in .netrc file for ftp login

Hi, i have a doubt. i am using .netrc file for login to a ftp server. ftp abc.ftp.com suppose i have 2 userid and password for the same server as shown below. which one is it going to read from the .netrc file? cat .netrc machine abc.ftp.com login admin1 password pass1 machine... (13 Replies)
Discussion started by: Little
13 Replies

3. Shell Programming and Scripting

FTP to list files

Hi Guys, Am writing a FTP script to download file from a remote server. There are 3 files in the target directory. eg. bfg234.2 vfg345.1 abc123.1 abc123.2 I need to get the file "abc123.2". The number could be increasing everytime.I need to get the latest file. can this be done ? (1 Reply)
Discussion started by: giri_luck
1 Replies

4. Solaris

FTP log only shows FTP LOGIN FROM entry?

OS: Solaris 9 Configuration /etc/syslog.conf daemon.debug /etc/inetd.conf ftp stream tcp6 nowait root /usr/sbin/in.ftpd in.ftpd -A -l -d Found the ftp.log only generate those entries from other servers/hosts. Can we trace on all ftp entries either from/to the server? ... (6 Replies)
Discussion started by: KhawHL
6 Replies

5. Shell Programming and Scripting

ftp script : list 4 lasts files

Hi, At work we have backups on a ftp. I want to view 4 last files saved (their names, dates, and weight). how can i achieve this goal using simplest way ? Thank's. (3 Replies)
Discussion started by: simon974
3 Replies

6. Shell Programming and Scripting

List files ftp

I list files on server ftp, and i'm a french man then i have files with accents. When i use wget for a mirroring, the files with accent are not encoded correctly, i have some "%CC%81" instead "é" for example... I have idea to cat the file list and pipe for tr awk or sed but unfotunately nothing... (2 Replies)
Discussion started by: protocomm
2 Replies

7. Shell Programming and Scripting

Getting a list of files on an ftp, via shell script...

G'day, I was wanting to write a shell script that checks an ftp server for the presence of new files, then get those files. In so much as the get, this is pretty straight forward, but I cannot work out how to get a list of files to check. Is it possible for a shell script to get the output of... (1 Reply)
Discussion started by: Elric of Grans
1 Replies

8. Shell Programming and Scripting

Pulling a list of files from FTP site in a shell script

Hi, I am writting a shell script which will pull a list files (mentioned in one file 1.txt) from external FTP site (for ex: ftp://abcd.efghijk.com/). The 1.txt is in my local unix directory. I have username and password to connect the external FTP site. Also before I pull the files, I need... (0 Replies)
Discussion started by: spatra
0 Replies

9. Shell Programming and Scripting

Reading list of files into ftp script

How can I go about getting this done? I have tried and failed with a loop before I start the session and after seeing that I am already in the ftp code block and not bash when I am trying to perform this: #FTP Information ftp_server=xx.xxx.xxx.xx ftp_user=xxxx while read line; do... (1 Reply)
Discussion started by: BkontheShell718
1 Replies

10. Shell Programming and Scripting

FTP Script with hidden login name and Password

hi, i need a method to hide the login name and password ....during FTP ....in the script.. thnks (3 Replies)
Discussion started by: scorpiyanz
3 Replies
Login or Register to Ask a Question