對抗垃圾信!請您點這裡:

我的E-mail:
我的Skype:My status

2007年4月20日

大量帳號建置器 版本1跟版本2

先說好,跟往常一樣.. 到我Pixnet的網誌看會比較不頭痛:P
版本1可以不用寫群組名稱,但是程式碼好醜ˊˋ
版本2必須要有群組名稱,適用於學校(?)

版本1下載
版本2下載

版本1:

#!/usr/bin/env ruby
File.open(ARGV[0]) do |file|
  while a = file.gets
    a = a.chomp.split(/ /)
    print "username => #{a[0]} ", "password => #{a[1]} ", "group => #{a[2]}", "\n"
    a[2].nil? ? `useradd -m #{a[0]}` : `useradd -m -G #{a[2]} #{a[0]}`
    `echo #{a[0]}:#{a[1]} | chpasswd`
  end
end
exec "pwconv"

使用者清單寫法:

  帳號 密碼 群組

版本2:

#!/usr/bin/env ruby
require 'yaml'
YAML.load_file(ARGV[0]).each{ |grp|
  grp.each{ |usr|
    usr.each{ |i|
      info = i.chomp.split(/ /)
      `useradd -m -G #{grp[0]} #{info[0]}`
      `echo #{info[0]}:#{info[1]} | chpasswd`
    }
  }
}
`pwconv`

使用者清單寫法:

grp1:
  - usr1 pwd1
  - usr2 pwd2
grp2:
  - usr3 pwd3
  - usr4 pwd4
grp3:
  - usr5 pwd5
  - usr6 pwd6
使用方式都是:
./account list

程式授權.. 隨便啦

沒有留言: