|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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?
|
|
#2
|
||||
|
||||
|
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). |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
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. |
|
#5
|
||||
|
||||
|
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: $!");
}
|
|
#6
|
|||
|
|||
|
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!! |
|
#7
|
|||
|
|||
|
LFTP (URL)
|
![]() |
| Viewing: Dev Shed Forums > System Administration > FTP Help > Using FTP in a Bash Script |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|