View Single Post
  #5   (View Single Post)  
Old 27th April 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

Another approach using a 'netrc' file and a simple regular expression to deal with the spaces between then name of the month and the day.
  • Create a directory e.g. Last4, disallow access by others, and change into it.
    Code:
    $ mkdir Last4
    $ ls -ld Last4/                                                                                                              
    drwxr-xr-x  2 j65nko  j65nko  512 Apr 27 02:06 Last4/
    
    $ chmod o= Last4
    $ ls -ld Last4
    drwxr-x---  2 j65nko  j65nko  512 Apr 27 02:06 Last4
    
    $ cd Last4
  • Create a '.netrc' file with the login information
    Code:
    machine localhost login j65nko password MYSECRETPASSWORD
    
    macdef init
    prompt off
    ls ARTICLES listing
    quit
    Please leave a empty line after the 'quit' command. This line is needed to terminate the 'init' macro definition, which will be run automatically after login.
    For testing on my machine I used a directory ARTICLES
  • Protect the 'netrc' file, else ftp will refuse to use it.

    Code:
    $ chmod u=rw,g=,o= .netrc
    $ ls -l .netrc
    -rw-------  1 j65nko  j65nko  65 Apr 27 02:06 .netrc
  • Use a script like this to retrieve the listing from the ftp server
    Code:
    #!/bin/sh
    
    # 'env' passes the current dir as HOME to 'ftp'
    # ftp uses the HOME environment variable to locate '.netrc' 
    
    env HOME=. ftp localhost
    
    # use a regular expression to accomodate one or more spaces between
    # month and date.
    # There are two blanks followed by a '*' 
    # This means: a space optionally followed by zero or more spaces
    
    grep "$1  *$2" listing
    
    # another way is use 'egrep' and use ' +', ( a space followed by a plus), which stands for one or more spaces
    # egrep "$1  +$2" listing
    
    # rm listing
  • Make the script executable with $ chmod u+x last4

A test:
Code:
$ ./last4 Jul 9

Trying 127.0.0.1...
Connected to localhost.
220 hercules.utp.xnet FTP server ready.
331 Password required for j65nko.
230- OpenBSD 4.7-current (GENERIC) #31780: Sat Apr  3 16:55:32 MDT 2010
230- 
230- Welcome to OpenBSD: The proactively secure Unix-like operating system.
230- 
230- Please use the sendbug(1) utility to report bugs in the system.
230- Before reporting a bug, please try to reproduce it with the latest
230- version of the code.  With bug reports, please try to ensure that
230- enough information to reproduce the problem is enclosed, and if a
230- known fix for it exists, include that as well.
230- 
230 User j65nko logged in.
prompt off
Interactive mode off.
ls ARTICLES listing
150 Opening ASCII mode data connection for '/bin/ls'.
226 Transfer complete.
quit
221 Goodbye.
-rw-r--r--  1 j65nko  j65nko  45629 Jul  9  2007 apsfilter-obsd.xml
-rw-r--r--  1 j65nko  j65nko  10698 Jul  9  2007 new-iso-itis.xml
-rw-r--r--  1 j65nko  j65nko   1667 Jul  9  2007 pf-simple.xml
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote