DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 25th April 2010
Simon Simon is offline
Port Guard
 
Join Date: Jan 2010
Posts: 30
Thanked 0 Times in 0 Posts
Default ask for a shell script

Hi,
Is it possible to have a shell script for :
I want to view on ftp (in our network) the 4 lasts files saved (see their dates and weight) ?

thank's for your help.
Reply With Quote
  #2   (View Single Post)  
Old 25th April 2010
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

If you use an ftp client that can be scripted well enough and outputs the necessary date information, just filter the ftp clients directory listing through a program that can sort it appropriately, then filter that scripts output to tail -n 4.

both a shell alias or standalone script could wrap that for faster usage.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #3   (View Single Post)  
Old 26th April 2010
Simon Simon is offline
Port Guard
 
Join Date: Jan 2010
Posts: 30
Thanked 0 Times in 0 Posts
Default

I tried this script verifynas:
When i launch : sh verifynas Apr 22, it works fine.
But if i launch it with: sh verifynas Apr 6, it doesn't work, problem with spaces
Is it possible to do a "ls" sort by date and keep only the 4 lasts lines ?
Or correct my script to it works ??

#!/bin/sh
clear &&
ftp -n ip_server << END | grep $1" "$2
cd backup_folder
ls
quit
END
Reply With Quote
  #4   (View Single Post)  
Old 26th April 2010
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Not reliably but some ftp daemons may have the ability to. The 'ls' and 'dir' commands in the ftp client, are basically just commands that tells the client to ask the server for a directory listing; I'm not sure if the extra (usually ls -l style) information is required.


Here's a test that finds the last 4 files in a directory, *if* they were created in the present or previous month:

Code:
Terry@dixie$ ftp -a ftp.freebsd.org << END  | grep -E "`date -v -1m +%b`|`date +%b`" | tail -n 4 | sort                                                        
        cd pub/FreeBSD
        ls
        quit
END                    
-rw-r--r--    1 110      1002        51703 Apr 26 05:32 dir.sizes
-rw-r--r--    1 110      1002     25830058 Apr 26 05:07 ls-lR.gz
drwxrwxr-x    7 110      1002          512 Apr 05 18:37 snapshots
drwxrwxr-x   10 110      1002          512 Mar 14 09:29 ports
Terry@dixie$
The grep and sort commands should really be replaced by a filter script, that understands how to collate/sort the entries into the order of months, e.g. Jan before Feb, March before Apr, etc. My suggestion would be Perl or AWK.

There's probably a few Perl modules on CPAN that can, and virtually every script language that supports arrays could pull off the task. (I would probably use AWK and a hand written look up table, but I'm to busy to check CPAN right now)
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #5   (View Single Post)  
Old 26th April 2010
Simon Simon is offline
Port Guard
 
Join Date: Jan 2010
Posts: 30
Thanked 0 Times in 0 Posts
Default Works !

Hi,

First, speak french sorry for my english.

To test the script i done :
script_nas 22 Apr : print all backup dated Apr 22
What do you think about ?
Thank' to your reply.

Here my script_nas :

Code:
#!/bin/sh
clear &&
echo "Recherche à la date de "$1 $2 
if [ $1 -le 9  ]
then
ftp -n nas << END | grep $2"  "$1 #2 spaces
user quest password
cd JIN
ls
quit
END
else
ftp -n nas << END | grep $2" "$1 #1 space
user quest password
cd JIN
ls
quit
END
fi

Last edited by vermaden; 26th April 2010 at 10:43 AM. Reason: Use [ code ] tags.
Reply With Quote
  #6   (View Single Post)  
Old 27th April 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,190
Thanked 182 Times in 149 Posts
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
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script with zenity bsdnewbie999 OpenBSD General 5 24th April 2009 02:37 AM
shell script compare md5 sum bsdnewbie999 Programming 1 11th April 2009 02:20 PM
incrementing within a shell script? spiderpig Programming 5 29th September 2008 08:12 PM
Shell Script. bsdnewbie999 Programming 21 15th July 2008 07:54 AM
shell script with input c0mrade Programming 5 13th July 2008 04:33 AM


All times are GMT. The time now is 04:16 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick