[LUGOS] ftp update
Uroš Zajc
zajc_lugos at email.si
Fri Jul 18 16:00:07 CEST 2003
Skipped content of type multipart/alternative-------------- next part --------------
#!/usr/local/bin/perl
use strict;
use Net::FTP;
use File::Copy;
require Fcntl;
# McAfee connection parameters
my $FTPServer = "ftpeur.nai.com";
# mirror: "ftpde.nai.com";
# mirror: "ftp.nai.com";
my $FTPLogin = "anonymous";
my $FTPPassword = "uros.zajc\email.si";
my $FTPDirectory = "/pub/datfiles/english";
# Local configuration
my ($CGPMcAfee,$UpdatesDir,$SignalFile,$arcExt,$unpacker);
if($^O eq 'MSWin32') {
$CGPMcAfee = "C:\\CommuniGatePro\\CGPMcAfee";
$UpdatesDir = "$CGPMcAfee\\Updates";
$SignalFile = "$UpdatesDir\\update.sig";
$arcExt = ".zip";
$unpacker = "pkunzip.exe";
} else {
$CGPMcAfee = "/var/CommuniGate/CGPMcAfee";
$UpdatesDir = "$CGPMcAfee/Updates";
$SignalFile = "$UpdatesDir/update.sig";
$arcExt = ".tar";
$unpacker = "/usr/bin/tar";
}
my $ftp;
my @SrcDirListing;
my @DirListing;
my $FileName;
my @FileID;
my @OldFileID;
my $CurrentVersion;
my $NewVersion;
#=======================================================================
#
# Run a couple of sanity checks
#
die "Unable to execute $unpacker.\n"
unless -x $unpacker;
die "Unable to write to directory: $UpdatesDir.\n"
unless -d $UpdatesDir and -w $UpdatesDir;
die "Unable to write to directory $CGPMcAfee.\n"
unless -d $CGPMcAfee and -w $CGPMcAfee;
die "Unable to write file: $CGPMcAfee/dat_file_id.diz"
unless -w "$CGPMcAfee/dat_file_id.diz" or ! -f "$CGPMcAfee/dat_file_id.diz";
#
# Get the current version number and the current dat_file_id.diz
#
GetFileID();
#
# Connect to the FTP server to find the newest version
print "Connecting to '$FTPServer' via FTP protocol...\n";
my $ftp = Net::FTP->new("$FTPServer", Debug => 0)
or die "Unable to open the connection to '$FTPServer'\n";
$ftp->login("$FTPLogin","$FTPPassword")
or die "Unable to login to server '$FTPServer'\n";
$ftp->cwd("$FTPDirectory")
or die "Unable to change to directory $FTPDirectory\n";
@SrcDirListing = $ftp->ls()
or die "Unable to get directory listing for $FTPDirectory\n";
#
# Look for the one file on the server we're interested in.
#
#@DirListing = grep { /^dat-\d+\.tar$/ } @SrcDirListing;
foreach(@SrcDirListing) {
if(/^dat-\d+(\.(tar|zip))$/) {
push(@DirListing, $_) if($1 eq $arcExt);
}
}
if($#DirListing < 0) {
die "Unable to locate the archive of DAT files\n";
}
if($#DirListing > 0) {
# die "Too many tarballs available. Please download DAT files manually\n";
@DirListing = reverse sort @DirListing;
}
#
# Grab the first file name which is either ony or the latest.
#
$FileName = $DirListing[0];
#
# Find the version number on the remote
# server based on the file name.
#
$NewVersion = ($FileName =~ /dat-(\d+)\.(tar|zip)/)[0];
#print "new version: $NewVersion; current version: $CurrentVersion\n";
if ($NewVersion > $CurrentVersion) {
#
# Download and install new DAT Files
#
chdir("$UpdatesDir")
or die "Unable to change to $UpdatesDir: $!\n";
$ftp->binary();
print "Downloading '$FileName', please wait...\n";
$ftp->get("$FileName")
or die "Unable to get file $FTPDirectory/$FileName: $!\n";
if($^O eq 'MSWin32') {
system("$unpacker $FileName") == 0
or die "Unable to unzip file $FileName: $?\n";
} else {
system("$unpacker -xf $FileName") == 0
or die "Unable to untar file $FileName: $?\n";
}
copy("file_id.diz", "$CGPMcAfee/dat_file_id.diz")
or die "Unable to install the dat_file_id.diz file: $!\n";
sysopen SIGFILE, $SignalFile, Fcntl::O_CREAT ()
or die "Unable to write plugin signal file $SignalFile: $!\n";
close SIGFILE;
# Save the old File ID info from McAfee
push(@OldFileID, at FileID);
# Get the new File ID info that was just installed
GetFileID();
print "\n",
"New DAT files were loaded.\n\n",
" New file ID info is:\n",
" -----\n";
foreach (@FileID) { print " $_"; }
print " -----\n";
print "\n",
" Old file ID info was:\n",
" -----\n";
foreach (@OldFileID) { print " $_"; }
print " -----\n";
} else {
#
# No new DAT files, so just print out old dat_file_id.diz info
#
print "\n",
"No new DAT files available.\n\n",
" Current file ID info is:\n",
" -----\n";
foreach (@FileID) { print " $_"; }
print " -----\n";
}
$ftp->quit();
exit;
#=======================================================================
sub GetFileID() {
my $VersionLine;
if ( -f "$CGPMcAfee/dat_file_id.diz" ) {
open(FILE, "< $CGPMcAfee/dat_file_id.diz")
or die "can't open $CGPMcAfee/dat_file_id.diz: $!";
@FileID = <FILE>;
close(FILE);
# Strip out the carriage returns
foreach (@FileID) { tr/\r//d; }
# We assume that the version number is the first thing
# in parentheses on the last line in the file.
chomp($VersionLine = $FileID[$#FileID]);
$CurrentVersion = ($VersionLine =~ /\((\d+)\)/)[0];
} else {
$CurrentVersion = 0;
}
return;
}
#=======================================================================
#END
More information about the lugos-list
mailing list