-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkeygen.pl
More file actions
executable file
·58 lines (46 loc) · 1.08 KB
/
keygen.pl
File metadata and controls
executable file
·58 lines (46 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl
use strict;
use warnings;
sub run(@);
sub tryrun(@);
my $sshDir = "$ENV{HOME}/.ssh";
my $host = `n9`;
chomp $host;
#makes the keys on the host, and appends to local .pub {local=>remote}
sub keygen($){
my $user = shift;
my $group = $user eq 'user' ? 'users' : $user;
run 'ssh', "$user\@$host", "
set -x
mkdir -p ~/.ssh
chmod go-w ~/.ssh
chown $user.$group ~/
rm ~/.ssh/id_rsa
rm ~/.ssh/*.pub
ssh-keygen -t rsa -N \"\" -q -f ~/.ssh/id_rsa
";
run "ssh $user\@$host 'cat ~/.ssh/id_rsa.pub' >> $sshDir/$host.pub";
}
#copies the local pub keys and authorizes them {remote=>local}
sub keyCopy($){
run "scp $sshDir/*.pub $_[0]\@$host:~/.ssh";
run 'ssh', "$_[0]\@$host", "cat ~/.ssh/*.pub > ~/.ssh/authorized_keys";
}
sub main(@){
die "Usage: $0\n" if @_ > 0;
tryrun 'rm', "$sshDir/$host.pub";
keygen 'root';
keygen 'user';
keyCopy 'root';
keyCopy 'user';
run "cat $sshDir/*.pub > $sshDir/authorized_keys";
}
sub run(@){
tryrun @_;
die "@_ failed\n" if $? != 0;
}
sub tryrun(@){
print "@_\n";
system @_;
}
&main(@ARGV)