Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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 testing.zip files if assume. One Thing Both are Linux Servers.

Comment on Urgent Help For Perl Issues
Re: Urgent Help For Perl Issues
by vinoth.ree (Parson) 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.

    Example:

    #!/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");
    Update:

    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!!!!

        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");

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023633]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-09-22 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (208 votes), past polls