Skip to content

Instantly share code, notes, and snippets.

@fakessh
Created September 23, 2012 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakessh/3768413 to your computer and use it in GitHub Desktop.
Save fakessh/3768413 to your computer and use it in GitHub Desktop.
sshscpwintonux
#========================================================
# But : Connection SSH + commandes (scp ou autre)
# Args : Reference d'un hash
# Retourne : rien
# Besoin : modules Net::SSH2
#==========================
sub CommandSSHFromWindows {
unless ( scalar(@_) == 1 ) {
my $usage = <<'FIN_USAGE';
Usage:
my %DataSSH = (
host => "IP serveur ou Host serveur",
login => "login",
password => "xxxxxx",
cmd => ["perl /home/toto/test.pl", "ls /usr"],
scp_put => [
[ "Fichier local", "Fichier destination"],
[ "Fichier local"], # Depot dans le rep courant du user
],
scp_get => [
[ "Fichier destination", "Fichier local"],
[ "Fichier destination"], # Depot dans le rep local courant
],
verbose => 1, # ou 0
);
CommandSSHFromWindows( \%DataSSH);
FIN_USAGE
die($usage);
}
my ($RefDataConnection) = @_;
$|++;
# parameters
my $host = $RefDataConnection->{host};
my $login = $RefDataConnection->{login};
my $password = $RefDataConnection->{password};
my $Refcmd = $RefDataConnection->{cmd};
my $Verbose = $RefDataConnection->{verbose} || 0;
require File::Basename;
require Net::SSH2;
my $ssh2 = Net::SSH2->new();
$ssh2->connect($host);
my ( $code, $error_name, $error_string ) = $ssh2->error();
if ($code) {
print "[WARNING] Unable to connect to host : $host\n\n";
return;
}
unless ( $ssh2->auth_password( $login, $password ) ) {
$ssh2->disconnect();
print "[WARNING] Unable to login.\n"
. "Check your login ($login) or your password\n\n";
return;
}
foreach my $cmd ( @{$Refcmd} ) {
if ( $Verbose == 1 ) {
print "\n- $cmd\n";
}
my $channel = $ssh2->channel();
$channel->blocking(1);
$channel->exec($cmd);
while ( $channel->read( my $buffer, 1024 ) ) {
if ( $Verbose == 1 ) {
print $buffer;
}
}
$channel->close;
}
# SCP si necessaire
# Put
if ( exists $RefDataConnection->{scp_put} ) {
foreach my $RefCoupleSCP_put ( @{ $RefDataConnection->{scp_put} } ) {
unless ( -e $RefCoupleSCP_put->[0] ) {
print "\n$RefCoupleSCP_put->[0] n'existe pas \n";
next;
}
my $message = "Transfert de $RefCoupleSCP_put->[0] vers $host";
if ( $Verbose == 1 ) {
print "\n$message ... ";
}
$ssh2->scp_put( $RefCoupleSCP_put->[0], $RefCoupleSCP_put->[1] )
or die $ssh2->error();
print "fini\n" if ( $Verbose == 1 );
}
}
# Get
if ( exists $RefDataConnection->{scp_get} ) {
foreach my $RefCoupleSCP_get ( @{ $RefDataConnection->{scp_get} } ) {
my $NomFichier = File::Basename::basename( $RefCoupleSCP_get->[0] );
if ( $Verbose == 1 ) {
}
$ssh2->scp_get( $RefCoupleSCP_get->[0], $RefCoupleSCP_get->[1] )
or die $ssh2->error();
print " : fini\n" if ( $Verbose == 1 );
}
}
$ssh2->disconnect();
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment