Skip to content

Instantly share code, notes, and snippets.

@mattrose
Created June 29, 2009 15:39
Show Gist options
  • Save mattrose/137657 to your computer and use it in GitHub Desktop.
Save mattrose/137657 to your computer and use it in GitHub Desktop.
p1 = open("passwd").read.split("\n")
s1 = open("shadow").read.split("\n")
s1.delete_if { |ent| ent.split(":")[1] !~ /^\$1\$/ }
shash = Hash[*s1.collect { |e| [e.split(":",2)[0],e.split(":",2)[1]]}.flatten]
phash = Hash[*p1.collect { |e| [e.split(":",2)[0],e.split(":",2)[1]]}.flatten]
newpasswd = open("/tmp/passwd").read
phash.each { |k,v|
unless newpasswd.split("\n").include?(k + ":" + v)
if shash.has_key?(k)
newpasswd << k + ":" + v + "\n"
end
end
}
newshadow = open("/tmp/shadow").read.split("\n")
s1.each { |e|
unless newshadow.include?(e)
newshadow << e
end
}
puts newshadow.join("\n")
open("/tmp/shadow",'a') {|f| f.write(newshadow.join("\n")) ; f.write("\n")}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment