#!/usr/bin/perl
sub help { local $_=<<"#EOT";s/^# ?//mg; print }
# program: give-ssh-access.pl
# Give ssh access by copying public key into authorized_keys file
# (c) 2011-17 Vlado Keselj http://web.cs.dal.ca/~vlado
#
# Usage: give-ssh-access.pl [-i privf] ssh-pub-key userid\@smwhere.org
#    -i privf use private key file for authentication
#EOT

use strict;
use vars qw($VERSION $opt_i);
$VERSION = "1.2";
use Getopt::Std;
getopts("i:");

if ($#ARGV != 1) { &help; exit; }
my $key = shift; my $acc = shift;
if ($opt_i) { $opt_i = " -i $opt_i" }

&c("scp$opt_i $key $acc:tmp1");
&c("ssh$opt_i $acc 'mkdir -p .ssh; cat tmp1 >> .ssh/authorized_keys; ".
   "/bin/rm tmp1; chmod 700 .ssh; chmod 600 .ssh/authorized_keys'");

sub c {
    my $com = shift;
    print "$com\n";
    system($com);
}

