Skip to content

Instantly share code, notes, and snippets.

@jeffa
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save jeffa/984758e4678b20680cf0 to your computer and use it in GitHub Desktop.

Select an option

Save jeffa/984758e4678b20680cf0 to your computer and use it in GitHub Desktop.
Bootstrap your ssh public key to a remote server
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Pod::Usage;
GetOptions (
'r|remote=s' => \my $remote,
'u|user=s' => \my $user,
'p|pub=s' => \my $pub,
'h|help' => \my $help,
'man' => \my $verbose,
);
pod2usage( -verbose => 1 ) if $help;
pod2usage( -verbose => 2 ) if $verbose;
pod2usage( 'remote host is required' ) unless $remote;
pod2usage( 'path to pub key file is required' ) unless $pub;
$user ||= $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
open( my $fh, '<', $pub ) || die "Cannot read $pub: $!\n";
my $auth = do { local $/; <$fh> };
print `ssh $user\@$remote 'mkdir -p .ssh; echo "$auth" >> .ssh/authorized_keys'`;
__END__
=head1 NAME
ssh-auth-add - ssh into remote shell and append or create .ssh/authorized_keys
=head1 SYNOPSIS
ssh-auth-add [options]
Options:
--remote hostname or ip address (required)
--pub path to local public key file (required)
--user provide username (current user is default)
--help list usage
--man print man page
=head1 OPTIONS
=over 8
=item B<-remote>
Required. Hostname or IP address of server to connect to.
=item B<-pub>
Required. The full path to your local public key file (i.e. id_rsa.pub)
=item B<-user>
Optional. The name of the user to connect with.
=item B<-help>
Print a brief help message and exits.
=item B<-man>
Prints the manual page and exits.
=back
=head1 DESCRIPTION
B<This program> will ssh into a remote shell and do all essential things necessary to facilitate password-less logins.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment