Listing 15. taritup.pl

#!/usr/bin/perl -w
# taritup.pl - a program to archive a
# web directory
use strict;
use Net::Telnet;
use Net::FTP;
my $host    = shift || 'server.onsight.com';
my $user    = shift || $ENV{USER};
my $webdir  = shift || '/home/httpd/html';
my $tarfile = shift || 'MyTarFile.tar.gz';
my $tardir  = '/tmp';
my($pass);
print 'Enter password: ';
system 'stty -echo';
chop($pass = <STDIN>);
system 'stty echo';
print "\n";
print "logging in telnet session...\n";
my $tn = new Net::Telnet(Host => $host);
die "connect failed: $!" unless defined $tn;
$tn->login($user, $pass)
          or die "login failed: $!";
print "tarring up $webdir...\n";
$tn->cmd("cd $webdir")
          or die "failure: $!";
$tn->cmd("/bin/tar cvzf $tardir/$tarfile *")
          or die "failure: $!";
print "logging in ftp session...\n";
my $ftp = Net::FTP->new($host);
die "could not connect: $!" unless $ftp;
$ftp->login($user,$pass)
          or die "login failed: $!";
print "getting $tardir/$tarfile...\n";
$ftp->cwd($tardir)
          or die "cwd failed: $!";
$ftp->get($tarfile)
          or die "get failed: $!";
print "quiting ftp...\n";
$ftp->quit or die "quit failed: $!";
print "rm $tarfile...\n";
$tn->cmd("cd $tardir")
          or die "failure: $!";
$tn->cmd("/bin/rm $tarfile")
          or die "rm failed: $!";
print "closing telnet...\n";
$tn->close or die "close failed: $!";