FTP Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsSystem AdministrationFTP Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 6th, 2001, 12:51 PM
jtaylor77 jtaylor77 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: Plainwell, MI
Posts: 5 jtaylor77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Using FTP in a Bash Script

I need to write a script that will connct to an FTP server, cd to the correct directory and get a file. Don't figure that this would be that difficult however, I am apparently incapable of doing this task. Could some body show some pity on an idiot and tell me how to do this?

Reply With Quote
  #2  
Old December 6th, 2001, 12:59 PM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
Well, I could GIVE you a script that does something similar with perl. It's not a bash script.

It should work as long as you have Net::FTP installed for perl.

If the server you're downloading from has ssh, you should probably use rsync or scp, it's much more secure than SSH (no plain text username/passwords like ftp).

Reply With Quote
  #3  
Old December 6th, 2001, 01:48 PM
jtaylor77 jtaylor77 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: Plainwell, MI
Posts: 5 jtaylor77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks

That would be great. My email address is jtaylor@eqtima.com. I can download that net:ftp perl file. I don't know PERL but I have someone else who does so I should be able to get that working. Thank you.

Reply With Quote
  #4  
Old December 6th, 2001, 05:20 PM
freebsd freebsd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 5 freebsd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
If there is no web server involved, you also can write a shell script + ~/.netrc file.

Here is how the .netrc file may look like:

machine ftp.nai.com
login anonymous
password user@anonymous.net
macdef init
cd pub/antivirus/datfiles/4.x
bin
prompt
mget dat-*.tar
close
bye

And your shell script:

#!/bin/sh

# stateful firewall need to set this
FTP_PASSIVE_MODE=YES; export FTP_PASSIVE_MODE
echo "Lets FTP"
ftp ftp.nai.com
echo "FTP completed"

BTW, that .netrc file is what I am using to update my virus scanner database via cron for qmail.

Reply With Quote
  #5  
Old December 7th, 2001, 08:33 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
Well, this script doesn't have anything to do with a webserver. I run this daily as a cron job and it takes care of creating my backup file of my webserver for me. It also ftps the backup file to our backup server and emails me if any part of the process fails. This should be pretty easy for a person with moderate perl skills to adapt. This script is really nothing special, but it does it's job effectively.

To change it to "get" a file, you just need to change $ftp->put() to $ftp->get() and edit the other variables as you see fit.

I wrote this a year and a half ago, so forgive me if some of the syntax is a little wordy.

Code:
#!/usr/bin/perl -w
use strict;
use Net::FTP;
my $server='your IP or domain;
my $user="username";
my $password="password";
my $dir="your directory";
my $file="/your/file";
my $success="true";
my $filetran="true";

#This dumps the mysql tables for backup
`mysqldump -u username --password=password -Aal > /var/lib/mysql/backup.sql`;

# This should be on one line.
`tar cvf /tmp/malegal.tar /home /www /var/lib/mysql /etc/httpd/conf /etc/httpd/logs; gzip --force --best /tmp/malegal.tar`;

my $ftp=Net::FTP->new($server) or $success="false";
$ftp->login($user, $password) or $success="false";
$ftp->pasv() or $success="false";
$ftp->binary() or $success="false";
$ftp->cwd($dir) or $success='false';
$ftp->put($file) or $filetran="false";
$ftp->quit;

my $reason;
my $test="true";

if ($success eq "false"){$reason .="Unsuccessful login\n\n";
        $test="false";}
if ($filetran eq "false"){$reason .="Unsuccessful file transfer\n\n";
        $test="false";}

if ($test eq "false") {
        my $subject="Backup of Server Failed !";
        my $to='you@domain.com';

        BEGIN {
                $ENV{PATH}="/bin:/usr/bin";
                delete @ENV{qw(IFS CDPATH ENV BASH_ENV) };
                }
        open MAIL,"| /usr/lib/sendmail -t -i -F'Your Website' -f'web\@domain.com'" or dienice("Couldn't open sendmail $!");
        print MAIL "To: $to\n";
        print MAIL 'Reply-To: web\@domain.com\n';
        print MAIL "Subject: $subject\n\n";
        print MAIL $reason;
        close MAIL or die("Error closing sendmail: $!");
}

Reply With Quote
  #6  
Old January 30th, 2004, 10:11 AM
zoniguana zoniguana is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 1 zoniguana User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
addendum to the automation

I have this script saved under a regular user's name, and set up to get a file from an FTP server instead of putting to the server.

Running perl script.pl in the root's crontab won't work. I imagine if I give more permissions that will change, but, for now, I have the user grabbing the file, and the root stuffing the contents of that file into the mySQL database... Seems to be working smoothly, for now, though I will probably try to lock it down a bit more for security... ;-)

Cheers!
and Thank You for the script! Works great!!

Reply With Quote
  #7  
Old May 14th, 2004, 04:08 PM
Nnyan Nnyan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 2 Nnyan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 40 sec
Reputation Power: 0
LFTP (URL)

Reply With Quote
Reply

Viewing: Dev Shed ForumsSystem AdministrationFTP Help > Using FTP in a Bash Script


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway