20090603 netsshperl with keys - plembo/onemoretech GitHub Wiki
title: Net::SSH::Perl with Keys link: https://onemoretech.wordpress.com/2009/06/03/netsshperl-with-keys/ author: lembobro description: post_id: 312 created: 2009/06/03 17:38:58 created_gmt: 2009/06/03 17:38:58 comment_status: open post_name: netsshperl-with-keys status: publish post_type: post
Net::SSH::Perl with Keys
Sometimes passwords just don't cut it. I've got a project where I need to connect over ssh to a bunch of servers with various user names and passwords and do some stuff. The big problem is that maintenance of those pesky passwords has gotten away from the team and I can't rely on them being consistent from box to box. Not only that, but no one seems to be able to agree on what the pw should be from one month to the next. My solution? Seed all those boxes with authorized_keys files and use them to make the ssh connection with my script. Part one of this is to figure out how to do a Net::SSH::Perl connection with keys. Here's my code:
#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Perl;
my $HOME = $ENV{'HOME'};
my $user = "myuser";
my $target = "host.example.com";
my @idfiles = ("$HOME/.ssh/id_rsa", "$HOME/.ssh/id_rsa.pub");
my %params = (protocol => '2',
interactive => '0',
identity_files =>@idfiles,
);
my $ssh = Net::SSH::Perl->new($target, user =>$user, %params);
$ssh->login();
my $cmd = "ls -l";
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
print $stdout, "n";
__END__;
The above works in my testing. Now I just need to refine it so I can use a variety of user names and keys to make my connections. Code shown above was partially lifted from a post by Patrick Hartemann back in 2006.
Copyright 2004-2019 Phil Lembo