Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Runing Remote Command

by neuhru (Novice)
on Apr 11, 2013 at 01:32 UTC ( #1028057=perlquestion: print w/replies, xml ) Need Help??
neuhru has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I can run the command below, enter the password from the command line, and it performs the action I need with no problem.

ssh admin\@10.253.0.1 cli \"enable\" \"run\" \"reload\"

However, I am having problems trying to automate this. How can automate this using a Perl script?

Thanks!

Replies are listed 'Best First'.
Re: Runing Remote Command
by salva (Abbot) on Apr 11, 2013 at 08:31 UTC
    Try Net::OpenSSH (more powerfull, doesn't work on Windows) or Net::SSH::Any:
    use Net::SSH::Any; my $ssh = Net::SSH::Any->new('10.253.0.1', user => 'admin', password => $password); $ssh->error and die "unable to connect to remote machine: " . $ssh->er +ror; my ($out, $err) = $ssh->capture2(cli => '"enable"', '"run"', '"reload" +'); $ssh->error and die "remote command failed: " . $ssh->error; print $out, $err;
Re: Runing Remote Command
by vinoth.ree (Monsignor) on Apr 11, 2013 at 03:40 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.

    Ex:

    #!/usr/bin/perl use Net::SSH::Perl; my $host = "hostname"; my $user = "user"; my $password = "password"; #-- set up a new connection my $ssh = Net::SSH::Perl->new($host); #-- authenticate $ssh->login($user, $pass); #-- execute the command my($stdout, $stderr, $exit) = $ssh->cmd("ls -l /home/$user");

    All is well
Re: Runing Remote Command
by thomas895 (Chaplain) on Apr 11, 2013 at 03:35 UTC

    CPAN is your friend.
    I personally quite like Net::SSH2.

    ~Thomas~ 
    "Excuse me for butting in, but I'm interrupt-driven..."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028057]
Approved by vinoth.ree
help
Chatterbox?
[LanX]: I want the command line in the history
[tye]: -Mouse
[Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file.
[LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy
[Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history
LanX damns the cult of CB ;-)
LanX WTF WTF WTF
[LanX]: please forget my last 3 posts
[LanX]: Yeah option a doesn't go into history
[LanX]: probably I need to teach the app to restart after C-c Kill

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-03-27 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.