#!/usr/bin/perl
# scp_expect.pl
# Meryll Larkin
# December 27, 2018
# corrections November 4, 2019 - out_path
use Net::SCP::Expect;
# for hidden password
use Term::ReadKey;
use strict;
my ($user, $pwd) = &get_login;
my $scpfile = $ARGV[0] || &get_filename("file to scp");
my $infile = $ARGV[1] || &get_filename("Server list");
my $out_path = $ARGV[2] || &get_destination;
chomp $scpfile;
chomp $infile;
chomp $out_path;
my $pid = $$;
my $outfile = "outfile_for_scp_" . ${pid} . '.txt';
open (INPUT, $infile) || die "Unable to open $infile " . $! . "\n";
open (OUTPUT, ">>$outfile") || die "Unable to open $outfile " . $! . "\n";
while () {
chomp (my $line = $_);
# ignore lines that begin with comment character
next if ($line =~ /^\s*#/);
# ignore empty lines
next if ($line !~ /\w/);
my ($hozt, $ipaddr) = split('\s+',$line);
my $dest = $hozt . ':' . $out_path;
print "Copying file ${scpfile} to $dest\n";
my $scpe = Net::SCP::Expect->new(user=>$user,password=>$pwd);
$scpe->scp(${scpfile}, $dest);
}
close INPUT;
print "Done\n";
exit (0);
# *********************************
sub get_filename {
my $filetype = shift;
print "$filetype filename? ";
my $filename = ;
return $filename;
}
# *********************************
sub get_destination {
print "Path on destination server? ";
my $dest_path = ;
return $dest_path;
}
# *********************************
sub get_login {
print "Username: ";
chomp (my $user = );
print "Hidden password: ";
ReadMode('noecho'); # don't echo
chomp (my $pwd = );
ReadMode(0); # back to normal
print "\n";
return ($user,$pwd);
}
# *********************************