close

Solais/linux系統管理中,針對帳號系統到期日的設定,
通常可以用GUI 畫面來設定某年月某日到期
但有時候遇到需要用自動化機制,比如大量開帳號,
想要通通設定這些帳號都在180天後過期
這個模組就幫得上來囉!
遇到大量要開帳號,就用程式呼叫這個模組就好囉! 一點都不需要動到原本的開帳號程式喔!

模組使用方法:
假設模組名稱存成setexpire
   # setexpire --account  user1 --livedays 180  
       -> 就會把user1 這個帳號設為從今天之後180天後到期!
       -> 當燃也可以短參數來達成
   # setexpire -a user1 -d 180 --> 一樣的產出,只是設定參數比較短。

# setexpire --help  --> 當然我也有加入Help 的功能喔!不用記指令,隨時Help 一下就OK!
Usage: setexpire -a [accountname] -d [livedays]

Set Account live-days since today , Program Author : ES/Fwwang

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -a ACCOUNT, --account=ACCOUNT
                        Which account do you want to set expire day
  -d LIVEDAYS, --livedays=LIVEDAYS
                        The livedays means :It will expire after xx days since
                        today


以下為 Script ( 引用者請註明本出處)

#!/usr/bin/env python

from glob import *
import re
import time
import datetime
import optparse
import sys
import os

shadowfile="/etc/shadow"

def main():
        p = optparse.OptionParser(description="Set Account live-days since today , Program Author : ES/Fwwang ",
      version="%prog Version:1.0 , 2009/05/08",
      usage="%prog -a [accountname] -d [livedays]")
    p.add_option('--account', '-a',help="Which account do you want to set expire day ")

    p.add_option('--livedays','-d',help="The livedays means :It will expire after xx days since today")
    options, arguments = p.parse_args()
    
    
    account=options.account
    
    if  len(arguments) >= 1:
        p.print_usage()
        sys.exit(0)
    if    options.account == None :
        p.print_usage()
        sys.exit(0)
    if    options.livedays == None :
        p.print_usage()
        sys.exit(0)
    
    
    livedays=int(options.livedays)        
        

    today = datetime.date.today()
    startday = datetime.date(1970,1,1)
    rangetmp= today - startday
    rangedays = int(rangetmp.days)
    expiredays = str(rangedays+livedays)


    fileList = glob(shadowfile)
    p1 = re.compile(r'^%s:' %account, )
    p2 = re.compile(r':[0-9]+:$', )
    p3 = re.compile(r'::$', )
    r1 = account
    r2 = ":%s:" % expiredays
    
    cfg = open('/etc/shadow.new', 'wu')
    for filename in fileList:
        for line in file(filename):
            m1=p1.match(line)
            m2=p2.search(line)
            m3=p3.search(line)
            

            if m1 == None:
                print >>cfg,p1.sub(r1,line),
            else:
    
                if m2 == None:
                    
                    print >>cfg,p3.sub(r2,line),
                else:
                    print >>cfg,p2.sub(r2,line),

    cfg.close()
    os.remove('/etc/shadow')
    os.rename("/etc/shadow.new","/etc/shadow")
    print "The account %s will expire after  %s Days" %(account ,livedays)

if __name__ == '__main__':
        main()

arrow
arrow
    全站熱搜

    fevin 發表在 痞客邦 留言(0) 人氣()