Dearest monks,
I have been trying for some time now to create a script, using Net::SSH::Expect, to send the contents of the local id_rsa.pub file to another host's authorized_keys file. I thinkg I have managed to exec the command on the remote host properly, but I do not know how to send the password (which I must do once), stored in a variable, when it is is requested.
This is my current code:
#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Expect;
print "Device IP: ";
my $deviceIP = <STDIN>;
chomp $deviceIP;
my $user = "gafaman";
print "User password: ";
my $pass = <STDIN>;
chomp $pass;
my $epass = "beholder";
my $login_timeout = 60;
use POSIX qw(strftime);
my $datestring = strftime "%F", localtime;
my $ssh = Net::SSH::Expect->new(
host => $deviceIP,
user => $user,
password => $pass,
raw_pty => 1,
no_terminal => 0,
log_stdout => 1
);
$ssh->login();
#When the line below is executed, it ends with a "Password: " prompt
$ssh->exec("cat \~\/\.ssh\/id_rsa.pub|ssh \'-o StrictHostKeyChecking=n
+o\' bkpuser\@10.11.12.13 \'cat \>\> \.ssh\/authorized_keys \&\& echo
+\"KeyCopied\"\'",2);
$ssh->waitfor('Password:\s*', 5) or die "prompt 'Password' not found a
+fter 5 seconds";
$ssh->send("$epass");
$ssh->close();
Could you please lend me some wisdom?
Thanks!
Gafaman
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.