Skip to content

Instantly share code, notes, and snippets.

@masasuzu
Last active February 25, 2016 04:10
Show Gist options
  • Save masasuzu/e405b917711be9f7dad0 to your computer and use it in GitHub Desktop.
Save masasuzu/e405b917711be9f7dad0 to your computer and use it in GitHub Desktop.
要はshadowのパスワード部分を埋めれば良いんでしょ?
#!/usr/bin/perl
use strict;
use warnings;
use 5.14.2;
# perl shadow_ohikkoshi.pl OLD_SERVER_SHADOW NEW_SERVER_SHADOW OUTPUT_SHADOW
my ($origin_shadow, $new_shadow, $output_shadow) = @ARGV;
open my $origin, '<', $origin_shadow or die $!;
open my $new, '<', $new_shadow or die $!;
open my $out, '>', $output_shadow or die $!;
my $data = +{};
while (my $line = <$origin>) {
chomp $line;
next unless $line;
my ($user, $password) = split(/:/, $line);
next if $user =~ /mf|root/;
$data->{$user} = $password;
}
close $origin;
while (my $line = <$new>) {
chomp $line;
next unless $line;
my ($user) = split(/:/, $line);
if (exists $data->{$user}) {
say $out ($line =~ s/!/$data->{$user}/r);
}
else {
say $out $line;
}
}
close $new;
close $out;
chmod 0640, $output_shadow;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment