sure: I wrote this as a push script to move files from one machine to one or
more machines.  This script works best if Keys are exchanged, but will also
work(Prompting for password) without.

Also at http://www.gargiullo.com/scripting/index.cfm

#!/usr/bin/perl -w

##########
# Original script written by MGargiullo for pushing from dev to beta and
live
#
# 3-20-2002 - MRG - Version 1.0
#
##########

# Config section
open(LOG, ">>/var/pushed/push.log");
$lserv = "xxx.xxx.xxx.xxx or hostname for Production servers, create a list
(server1, server2)";
$bserv = "xxx.xxx.xxx.xxx or hostname for BETA server";
# End Config


$live = 0;
$SCP='/usr/bin/scp -C';
$cwd=`pwd`;
chomp ($cwd);
$user=`whoami`;
chomp ($user);
$finished =0;
$recursive = 0;
$now = `date`;

# Get options
while ($ARGV[0] =~ /^\-/)

    if ($ARGV[0] eq '-l') {
        if ($user eq
ke'){ 
        $live = 1; 
        }
        shift; 
    }
    if ($ARGV[0] eq '-r') { 
        $recursive = 1; 
        shift; 
    }
} 

# Exit with error if no options or files specified
if (!$ARGV[0]) { 
    print "usage: push [options] <file> [<file>...]\n"; 
    exit(0); 
}

$| = 1;

# Group files by directories
foreach $file (@ARGV) {
        $directory{$file} = &dirname($file);
        }

foreach $dir (values %directory) {
        $unique_dirs{$dir} = 1;
        }
foreach $dir (keys %unique_dirs) {
    push @filenames_unsorted, [ grep(($directory{$_} eq $dir), @ARGV) ]; 
    @filenames = sort { &dirname($$a[0]) cmp &dirname($$b[0]) } @filenames_unsorted; 
        }

# Set current Dir
$remote_dir=$cwd;

# set list of servers to push to
if ($live == 1) {
        $rhosts = "$bserv,$lserv";
} else {
        $rhosts = "$bserv";
}

# Build array of hosts to push to
@hosts = split(',', $rhosts);

# Start looping through hosts to push to
foreach $host (@hosts) {
        # iterate over direcotries (sorted)
        foreach $filegroup (@filenames) {
                
                # Execute command once per directory
                if ($recursive) { 
                     $cmd = "$SCP -r "; 
                 } 
                 else { 
                     $cmd = "$SCP "; 
                 } 
                
                # Specify to draw error if occurs
                $dir = "" . &dirname($$filegroup[0]); 
                
                if($#$filegroup == 0) { 
            $cmd .= "$$filegroup[0] $user\@$host:$remote_dir/$$filegroup[0]\n"; 
        } 
        else { 
            foreach $file (sort { $a cmp $b } @$filegroup) { 
                $cmd .= "$file "; 
            } 
            $cmd .= "$user\@$host:$remote_dir/$dir"; 
        }
                
                # Display Host
                print "\n$host  \n";
                # Push File and capture eror if any
                $error_code = system $cmd;
                
                # Display error if push fails
                if ($error_code != 0) {
                        print "PUSH FAILED ( Tried to push like so: $cmd )\n\n";
                        $finished =1;
                }
                else {
                        foreach $file (sort { $a cmp $b } @$filegroup){ 
                                print LOG "$user pushed $file to $host on $now\n";
                        }
                }
                
        }
}

if ($finished) {
        print "The push was not successful.  Error above.\n";
}

exit $finished;

sub dirname {
        my $file = shift;
        my $dir = ".";
        $dir = substr($file, 0, rindex($file, '/')+1) if $file =~ m:/:;
        return $dir;
}
close(LOG);


-----Original Message-----
From: Jeff Liu [mailto:[EMAIL PROTECTED]]
Sent: Monday, April 01, 2002 11:44 AM
To: Beginners
Subject: scp inside perl



Hi all,

Is there a way to do scp inside perl script?

Thanks,
Jeff Liu

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to