Created
December 8, 2012 07:32
-
-
Save jackl0phty/4239143 to your computer and use it in GitHub Desktop.
Using Perl to Execute Remote Commands.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
############################################################################### | |
# This script is responsible for making a secure connection via SSH to # | |
# server1 and executing the command ls. # | |
# This script is also responsible for making a secure connection via SSH to # | |
# server1 and then SCP the file test.t # | |
############################################################################### | |
# Written by Gerald L. Hevener Jr., M.S. # | |
############################################################################### | |
# Licensed under the Apache License, Version 2.0 (the "License"), # | |
# For any questions regarding the license of this software, please refer to # | |
# the actual license at http://www.apache.org/licenses/LICENSE-2.0.txt. # | |
############################################################################### | |
# DISCLAIMER OF WARRENTY # | |
# BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR # | |
# THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN # | |
# OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES # | |
# PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED # | |
# OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF # | |
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO # | |
# THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE # | |
# SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, # | |
# REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR # | |
# AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY # | |
# MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, # | |
# BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, # | |
# OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE # | |
# SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED # | |
# INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIESOR A FAILURE OF THE # | |
# SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER # | |
# PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. # | |
############################################################################### | |
#import required modules | |
use strict; | |
use Net::SCP qw(scp iscp); | |
use Net::SSH qw(ssh); | |
use Log::Dispatch::Syslog; | |
#declare local variables | |
my $scp; | |
my $host = "server1.domain.com"; | |
my $user = "user1"; | |
my $remotedir = "/home/user1/"; | |
my $file = "test.txt"; | |
my $cmd = "/bin/ls"; | |
####################Log::Dispatch::Syslog####################################### | |
# Define our pid for use in the log message | |
my $pid = getppid(); | |
# Define our logfile object | |
my $logfile = Log::Dispatch::Syslog->new( name => 'logfile', | |
min_level => 'info', | |
ident => "running_list_cmd[$pid]" ); | |
####################Log::Dispatch::Syslog####################################### | |
######first connect to $host via Net::SSH and run /bin/ls########### | |
$logfile->log( level => 'info', message => "Connecting to $host as $user and running /bin/ls ..." ); | |
ssh("$user\@$host", $cmd); | |
$logfile->log( level => 'info', message => "ls completed successfully!" ); | |
######first connect to $host via Net::SSH and copy file $file########### | |
#initialize Net::SCP object and send credentials | |
$scp = Net::SCP->new($host); | |
#notify user we're logging into $host | |
print "Logging into $host ...\n"; | |
#write "connected to $host" to $file | |
$logfile->log( level => 'info', message => "Connected to $host successfully." ); | |
#log into $host as $user | |
$scp->login($user) or die $scp->{errstr}; | |
#write "connected to $host" to $file | |
$logfile->log( level => 'info', message => "Logged into $host successfully." ); | |
#notify user of changing working directory to $remotedir | |
print "Chaging working directory to $remotedir\n"; | |
#change working directory to $remotedir | |
$scp->cwd($remotedir) or die $scp->{errstr}; | |
#Write Changed working directory (CWD) to $remotedir | |
$logfile->log( level => 'info', message => "CWD to $remotedir successfully." ); | |
#display file size of $file | |
$scp->size($file) or die $scp->{errstr}; | |
#notify user scp of $file has started | |
print "SCPing $remotedir$file from $host ...\n"; | |
#scp $file from $host | |
$scp->get($file) or die $scp->{errstr}; | |
#notify user scp of $file from $host was successful | |
print "$remotedir$file copied from $host successfully!\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment