Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Urgent Help For Perl Issues

by arvindninestars (Initiate)
on Mar 15, 2013 at 07:13 UTC ( #1023633=perlquestion: print w/replies, xml ) Need Help??
arvindninestars has asked for the wisdom of the Perl Monks concerning the following question:

HI, Using Perl script how to execute linux commands in remote machines. Example: I have two servers, need to login remote server and execute unzip command. testing.tar.gz or files if assume. One Thing Both are Linux Servers.

Replies are listed 'Best First'.
Re: Urgent Help For Perl Issues
by vinoth.ree (Monsignor) on Mar 15, 2013 at 07:48 UTC

    You can execute commands on remote machines from a Perl script using the Net::SSH::Perl module.

    This module allows you to execute a command remotely and receive the STDOUT, STDERR, and exit status of that remote command.

    One big advantage of Net::SSH::Perl over other methods is that you can automate the login process, that way you can write fully automated perl scripts, no console interaction is required in order to authenticate in the remote machine.


    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Perl; my $host = ""; my $user = ""; my $password = ""; my $ssh = Net::SSH::Perl->new($host); $ssh->login($user, $pass); my($stdout, $stderr, $exit) = $ssh->cmd("your command");

    Execution of Net::SSH::Perl commands can be quite slow if you don't have the Math::BigInt::GMP module; so be sure that you have that module installed in order to avoid the slowness problem. Make sure that in both the Linux servers ssh enabled and server listens on port number 22.

      Hi Vinoth, Great its working fine. But one thing if i login into server its landing into default path.But if i need to untar into different path after landing, what can i do? Example : My landing path /home/systems after that need to change "testing" directory and untar. Please help!!!!

        If you choose to use this familiarise yourself with ssh, and the Net::SSH::Perl documentation, the README shows you where sample/tutorial scripts live within the distribution.

        Just use the cd command with your path and navigate to your path and do untar

        my($stdout, $stderr, $exit) = $ssh->cmd("cd $path"); ($stdout, $stderr, $exit) = $ssh->cmd("untar tar.gz");

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023633]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-07-22 10:55 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (454 votes). Check out past polls.