溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用 Python 一鍵配置 SVN 賬號和屬組信息

發(fā)布時間:2020-07-29 07:48:53 來源:網(wǎng)絡 閱讀:1153 作者:sylan215 欄目:編程語言

雖然現(xiàn)在 Git 已經(jīng)很普及,但是我相信用 SVN 的公司仍然不少,那么作為 SVN 配置管理員的話,就不可避免的涉及到賬號維護的問題,今天我們就說說如何通過 Python 腳本實現(xiàn)用戶的快捷維護。

如果手動維護用戶,一般需要三個步驟:
1.手動添加用戶
2.手動設置屬組
3.通知用戶設置結果

使用腳本后也是三步,但是效率卻大大提升:
1.輸入用戶名
2.輸入要設置的組名
3.按下回車,搞定

這里面設置用戶和屬組是關鍵點,也是我們需要自動化起來的操作,下面分別給出實現(xiàn)的代碼:

def add_user(user_name):
    """如果用戶不存在則調(diào)用htpasswd.exe添加用戶"""
    htpasswd_path = cur_file_dir() + '\\bin\\htpasswd.exe -b '
    pwd_path = REP_PATH + '\\htpasswd '
    user_list = readfiles(REP_PATH + '\\htpasswd')
    for line in user_list.split('\n'):
        if ':' in line and user_name.lower() == line.split(':')[0]:
            print('用戶 ' + user_name + ' 已存在,不需要創(chuàng)建')
            return "見之前郵件通知"

    pwd = ' sylan215@' + str(random.randrange(1000, 9999, 1))
    print(execut_ret(htpasswd_path + pwd_path + user_name + pwd)[1])
    return pwd.strip()

這段是創(chuàng)建用戶的代碼,下面做下說明:

cur_file_dir() 函數(shù)功能是獲取執(zhí)行腳本所在路徑;
htpasswd.exe 是從 Apache 目錄拷貝出來的工具,可以在指定文件添加指定的用戶名和密碼信息,命令行使用方法「htpasswd.exe -b [密碼文件] [用戶名] [密碼]」,更多使用說明請 Google;
REP_PATH 是我定義的一個全局變量,是我 SVN 倉庫的根目錄,目錄下會存放用戶和組設置的配置文件;
htpasswd 文件就是上面說的用戶信息存儲文件;
pwd 是我通過隨機數(shù)生成的以 sylan215@ 開頭的 13 位密碼;
execut_ret() 函數(shù)功能是執(zhí)行指定程序并返回執(zhí)行結果的,目前就是執(zhí)行 htpasswd.exe 添加指定用戶信息;

接著我們來看看設置組信息的代碼:

def add_group(user_name, user_grp):
    """添加用戶到指定用戶組"""
    grp_path = REP_PATH + "\\groups.conf"
    grp_context = readfiles(grp_path)
    new_context = ""
    isadd = False
    for line in grp_context.split('\n'):
        if '=' in line and user_grp.lower() == line.split('=')[0].lower():
            if user_name.lower() in line.lower():
                print("用戶 " + user_name + " 已經(jīng)屬于 " + user_grp)
                return False

            if line.split('=')[1] != '':
                new_line = line + "," + user_name
            else:
                new_line = line + user_name
            new_context = grp_context.replace(line, new_line)
            isadd = True
            break

    if isadd:
        writetofile(grp_path, new_context)
        print("用戶 " + user_name + " 成功添加到組 " + user_grp)
        return True
    else:
        print("組設置失敗,請檢查組名是否正確")
        return True

對這個函數(shù)的說明:

本函數(shù)功能就是讀取組設置文件 groups.conf,檢查當前用戶是否存在于目標組里面,如果存在直接返回,否則添加用戶到組里面;
readfiles() 函數(shù)功能是一次讀出目標文件的所有內(nèi)容;
writetofile() 函數(shù)功能是把指定內(nèi)容寫入指定文件;

下面是最后的統(tǒng)一調(diào)用函數(shù),以及入口函數(shù)實現(xiàn):

def useradd(user_name, user_grp):
    """"添加用戶+添加屬組+郵件通知"""
    ret_grp = False
    pwd = add_user(user_name)

    if ',' in user_grp:
        for each_group in user_grp.split(','):
            if add_group(user_name, each_group):
                ret_grp = True
    elif add_group(user_name, user_grp):
        ret_grp = True

    if ret_grp:
        sendcontextmail(user_name, pwd, user_grp)

if __name__ == "__main__":
    while True:
        user_name = input("請輸入用戶名(多個用戶請用英文逗號分隔):")
        user_group = input("請輸入要加入的屬組(多個組請用英文逗號分隔):")
        for usr in user_name.split(','):
            useradd(usr, user_group)

說明:

sendcontextmail() 函數(shù)是公用的郵件通知函數(shù);
統(tǒng)一處理函數(shù)可以處理一個用戶添加多個用戶組的情況;
入口函數(shù)可以處理多個用戶同時添加的情況,并且做了無限循環(huán),這樣把窗口掛在服務器上可以隨取隨用了;
上述代碼是基于 Python3.4 驗證通過的,其他版本應該同理;
上述說明是基于 Windows 進行實現(xiàn)的;
上述實現(xiàn)是基于 SVN 自帶的賬號和組管理系統(tǒng)的;

如果是基于 Windows 的賬號和組設置體系,代碼上比這個簡單:

def useradd(username, usergroup):
    """添加 windows 賬號,并設置屬組"""
    pwd = ' sylan215@' + str(random.randrange(1000, 9999, 1))

    retinfo = execut_ret('net.exe user ' + username + pwd + ' /add')
    if '命令成功完成' not in retinfo[0]:
        print('用戶已創(chuàng)建失敗:' + retinfo[1])

    print('用戶創(chuàng)建成功:' + username)
    print('隨機密碼:' + pwd)

    for groupname in usergroup.split(','):
        retinfo = execut_ret('net.exe localgroup ' +
                             groupname + ' ' + username + ' /add')
        if '命令成功完成' not in retinfo[0]:
            print('設置用戶屬組失?。? + retinfo[1])
        print('用戶已加入屬組:' + groupname)

    sendcontextmail(username, pwd, usergroup)

好了,通過這么少的代碼就可以瞬間搞定用戶配置問題,維護起來是不是 so easy 了(如果有同學需要完整代碼,請在公眾號后臺撩我哈)。

本文原創(chuàng)發(fā)布于公眾號「sylan215」,十年測試老兵的原創(chuàng)干貨,關注我,漲姿勢!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI