使用 Python 一鍵配置 SVN 賬號和屬組信息
如果手動維護用戶,一般需要三個步驟:
1.手動添加用戶
2.手動設置屬組
3.通知用戶設置結果
使用腳本後也是三步,但是效率卻大大提升:
1.輸入用戶名
2.輸入要設置的組名
3.按下回車,搞定
這裏面設置用戶和屬組是關鍵點,也是我們需要自動化起來的操作,下面分別給出實現的代碼:
def add_user(user_name): """如果用戶不存在則調用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 + ‘ 已存在,不需要創建‘) return "見之前郵件通知" pwd = ‘ sylan215@‘ + str(random.randrange(1000, 9999, 1)) print(execut_ret(htpasswd_path + pwd_path + user_name + pwd)[1]) return pwd.strip()
這段是創建用戶的代碼,下面做下說明:
cur_file_dir() 函數功能是獲取執行腳本所在路徑;
htpasswd.exe 是從 Apache 目錄拷貝出來的工具,可以在指定文件添加指定的用戶名和密碼信息,命令行使用方法「htpasswd.exe -b [密碼文件] [用戶名] [密碼]」,更多使用說明請 Google;
REP_PATH 是我定義的一個全局變量,是我 SVN 倉庫的根目錄,目錄下會存放用戶和組設置的配置文件;
htpasswd 文件就是上面說的用戶信息存儲文件;
pwd 是我通過隨機數生成的以 sylan215@ 開頭的 13 位密碼;
execut_ret() 函數功能是執行指定程序並返回執行結果的,目前就是執行 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 + " 已經屬於 " + 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
對這個函數的說明:
本函數功能就是讀取組設置文件 groups.conf,檢查當前用戶是否存在於目標組裏面,如果存在直接返回,否則添加用戶到組裏面;
readfiles() 函數功能是一次讀出目標文件的所有內容;
writetofile() 函數功能是把指定內容寫入指定文件;
下面是最後的統一調用函數,以及入口函數實現:
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() 函數是公用的郵件通知函數;
統一處理函數可以處理一個用戶添加多個用戶組的情況;
入口函數可以處理多個用戶同時添加的情況,並且做了無限循環,這樣把窗口掛在服務器上可以隨取隨用了;
上述代碼是基於 Python3.4 驗證通過的,其他版本應該同理;
上述說明是基於 Windows 進行實現的;
上述實現是基於 SVN 自帶的賬號和組管理系統的;
如果是基於 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(‘用戶已創建失敗:‘ + retinfo[1])
print(‘用戶創建成功:‘ + 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 了(如果有同學需要完整代碼,請在公眾號後臺撩我哈)。
使用 Python 一鍵配置 SVN 賬號和屬組信息