Skip to content

Instantly share code, notes, and snippets.

@glarizza
Created December 3, 2013 23:12
Show Gist options
  • Save glarizza/7779412 to your computer and use it in GitHub Desktop.
Save glarizza/7779412 to your computer and use it in GitHub Desktop.
require 'yaml'
require 'pry'
passwd_file = File.readlines('/etc/passwd')
@yaml_hash = {}
passwd_file.each do |line|
line_array = line.split(':')
username = line_array[0]
realname = line_array[4]
homedir = line_array[5]
uid = line_array[2]
homedir_size = 'Directory does not exist'
if realname == ''
realname = 'Not present in file'
end
if uid.to_i > 10
if File.exists?(homedir)
homedir_size = %x{du -sh #{homedir} 2>/dev/null }.split.first
end
@yaml_hash[username] = {}
@yaml_hash[username]['realname'] = realname
@yaml_hash[username]['homedir'] = homedir
@yaml_hash[username]['uid'] = uid
@yaml_hash[username]['homedir_size'] = homedir_size
end
end
File.open('/tmp/passwd.yaml', 'w') do |f|
f.write(@yaml_hash.to_yaml)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment