Skip to content

Instantly share code, notes, and snippets.

@MishaelRosenthal
Last active August 29, 2015 13:58
Show Gist options
  • Save MishaelRosenthal/9956585 to your computer and use it in GitHub Desktop.
Save MishaelRosenthal/9956585 to your computer and use it in GitHub Desktop.
A (not so good) perl script that sets ssh keys in host.
#!/usr/bin/perl
use Getopt::Long;
#use File::chdir;
use Cwd;
GetOptions ('host=s' => \$host, 'user=s' => \$user)
or die("Usage: perl private_keys.pl --user <user name> --host <host name>\n");
print "user $user, host $host\n";
my $originDir = getcwd;
chdir($ENV{"HOME"});
my $privateKeys = ".ssh/id_dsa.pub";
if (-e $privateKeys) {
print "$privateKeys exists, skipping private key generation.\n";
} else {
print "$privateKeys does not exists, generating new private key.\n";
system("ssh-keygen -t dsa");
system("chmod 600 .ssh/id_dsa");
}
system("scp .ssh/id_dsa.pub $user\@$host:");
system("ssh $host 'mkdir .ssh; cat id_dsa.pub >> .ssh/authorized_keys; chmod 700 .ssh'");
chdir($originDir);
print "private_keys done!!!";
@MishaelRosenthal
Copy link
Author

Usage:
perl private_keys.pl --user -- host

For example:
perl private_keys.pl --user mishaelr --host svpr-aha03.lpdomain.com

In the first time you run it you might need to press enter/yes a few times.
When running it for a new host you will need to enter your password twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment