Aloha monks! I was recently stuck with trying to solve a problem for a friend; he was using rsync, specifically, File::Rsync, to rsync files over to and from a remote host; however, he couldn't perform any authentication because he was not able to use the command line, due to some restriction. So I spoke with him and we came up with the idea that what he could do would be to simply just key the remote host with his public key, and this is the script I wrote to take care of that:
#!/usr/bin/env perl use strict; use warnings; use Net::SSH::Perl; my ($username, $hostname, $password) = @ARGV; my $die = 0; if(!defined $username) { print "It seems you forgot to provide a username.\n"; $die = 1; } if(!defined $hostname) { print "It seems you forgot to provide a hostname.\n"; $die= 1; } if(!defined $password) { print "It seems that you forgot to provide a password.\n"; $die = 1; } if($die) { print "Usage: key <username> <hostname> <password>\n"; exit; } my $filename; if(-f $ENV{"HOME"}."/.ssh/id_rsa.pub") { $filename = "id_rsa.pub"; } elsif(-f $ENV{"HOME"}."/.ssh/id_dsa.pub") { $filename = "id_dsa.pub"; } else { print "I couldn't find an SSH public key generated. Try running + ssh-keygen then run me again."; exit } my $key = &slurp($ENV{"HOME"}."/.ssh/$filename"); my $ssh = Net::SSH::Perl->new($hostname); $ssh->login($username,$password); $ssh->cmd("mkdir ~/.ssh"); my ($out,$err,$exit) = $ssh->cmd("cat ~/.ssh/authorized_keys"); my $suc; $suc = has_key($key,$out); if($suc) { print "$username\@$hostname is already keyed with your key.\n" +; exit; } $ssh->cmd("cat >> ~/.ssh/authorized_keys",$key); ($out,$err,$exit) = $ssh->cmd("cat ~/.ssh/authorized_keys"); if(has_key($key,$out)) { print "Your public key has been successfully added; try runnin +g ssh $username\@$hostname to see if it works.\n"; } else { print "Your key could not be added.\n"; print "Try connecting manually, then adding the contents of ". +$ENV{"HOME"}."/.ssh/$filename manually to /home/$username/.ssh/author +ized_keys\n"; exit; } print "\n"; sub slurp { my $fname = shift; open my $FILE, '<', $fname; local $/; my $key = <$FILE>; close $FILE; return $key; } sub has_key { my ($key, $keys) = @_; chomp($key); my $suc = 0; my @keys = split /\n/,$keys; my $i = 1; foreach my $k (@keys) { chomp($k); if($key eq $k) { $suc = 1; last; } else { $suc = 0; } $i++; } return $suc; }
Hopefully someone else will find this as interesting as I did.

In reply to SSH Keyer for passwordless logins. by Sir mXe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.