use strict; use warnings; use Cwd; use File::Copy; use Getopt::Long; use File::Basename; use Net::OpenSSH; my ($conf_file, $environment, $doexec, $exec, $job, $dest_file, $user, $host, $IP, $TARGET_SERVER, $JENKINS_JOB, $wrapper, $src_file, $src_path, $src_dist_path, $src_full_path, $id_file, $ssh, @array, $line); init(); sub init { $JENKINS_JOB = $ENV{'JOB_NAME'}; $conf_file = "/home/ec2-user/SCM/conf/deploy_build.conf"; open (FH, "<", $conf_file) or die "Cannot open < $conf_file: $!"; while () { if ( $_ =~ /\b$JENKINS_JOB\b/ ) { push @array, $_; } else { next; } } foreach $line (@array) { ($job, $src_path, $dest_file, $user, $wrapper) = split(':', $line); $id_file = "/home/ec2-user/.ssh/priv_key"; $ssh = Net::OpenSSH->new($IP, key_path => $id_file, user => $user); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; printf "\n"; if (length $wrapper) { printf "Initiating subroutine for executing wrapper on remote machine...\n"; &exec_wrapper; } else { printf "*** No wrapper specified ****\n"; } } } sub exec_wrapper { my ($stdout, $errput) = $ssh->capture2("~/release/$wrapper"); printf "Output: $stdout\n" if $stdout; die "Error: $errput\n" if $errput; printf "\n\n\n"; }