Because it is often necessary to operate on a json format file, it is more difficult to edit the text. I want to write a script to operate on the file, and send the modified file directly to the mailbox
The file directory is as follows:
Roughly explain that the json text before each operation is stored in the backup as a backup. The log file stores the operation log. The email is sent as an email. The config file stores the data informationdrwxr-xr-x 2 root root 4096 12 29 / 16:14 backup -rw-r--r-- 1 root root 168 12 29 / 16:10 config.ini -rwx------ 1 root root 2106 12 29 / 16:13 email_server.py drwxr-xr-x 2 root root 4096 12 October 29:16 log -rw-r--r-- 1 root root 454 12 29 / 16:14 shadowsocks.json -rwx------ 1 root root 2857 12 29 / 16:08 shadowsocks.py
The json format of the operation is as follows:
Insert data in comment and password every time. Logic: suppose the new test user takes out the key in comment, sorts it, finds out the maximum 40010, automatically adds 1, generates the data of "40011:test", generates a random password, inserts the data of "40011: ***** in password, and sends the new information to the mailbox.{ "_comment": { "40001": "xiaoming", "40009": "xiaohong", "40010": "1", "40011": "test" }, "method": "aes-256-cfb", "port_password": { "40001": "password10", "40009": "SDSFS#X09", "40010": "pQW26hBYMyNnLgm1", "40011": "k9nYaRNF7GlW2ejE" }, "server": "XXXXXXXX", "timeout": 600 }
Shadowlocks.py source code:
config.ini configuration file:#!/usr/bin/env python #-*- coding: utf-8 -*- import sys import subprocess import os import commands import json import random import string from email_server import main import shutil import time import ConfigParser conf = ConfigParser.ConfigParser() conf.read('config.ini') file_path = conf.get("backup", "file_path") backup_path= conf.get("backup", "backup_path") def open_json(option,user): with open(file_path,'r') as load_f: try: load_dict = json.load(load_f) if(option == "-a"): if user in load_dict['_comment'].values(): print "User name already exists!" return -1 else: return add_user(load_dict,user) elif(option == "-d"): if user in load_dict['_comment'].values(): return delete_user(load_dict,user) else: print "User name does not exist!" return -1 else: print "Available parameters\n -a Add user\n -d delete user" return -1 finally: load_f.close( ) '''delete user''' def delete_user(load_dict,user): keys = [x[0] for x in load_dict['_comment'].items() if user in x[1]] print keys for n in keys: del load_dict['_comment'][n] del load_dict['port_password'][n] print "User name deleted!" return load_dict '''Load user''' def add_user(load_dict,user): items = load_dict['port_password'].items() items.sort() new_port = (int(items[len(items)-1][0])) + 1 salt = ''.join(random.sample(string.ascii_letters + string.digits, 16)) dict1={new_port : salt} dict2={new_port : user} load_dict['port_password'].update(dict1) load_dict['_comment'].update(dict2) print "User name loaded!" main(user,salt,new_port) return load_dict '''Reload json file''' def write_json(load_dict): with open(file_path,"w") as dump_f: try: json.dump(load_dict,dump_f,sort_keys=True,indent=4) finally: dump_f.close() def email_send(): '''pass''' if __name__ == "__main__": log_time=time.strftime("%Y%m%d%H%M%S", time.localtime()) shutil.copyfile(file_path,backup_path + log_time) if len(sys.argv) < 3: print "Usage: python shadowsocks.py [-options] username\n[-option]:\n -a Add user\n -d delete user" sys.exit() option = sys.argv[1] name = sys.argv[2] if(name != ""): load_dict = open_json(option,name) if load_dict != -1 and load_dict !="" : write_json(load_dict) else: print "fail" else: print "Usage: python shadowsocks.py [-options] username\n[-option]:\n -a Add user\n -d delete user"
The email service will not be pasted. There are many online services. The final effect is as shown in the figure:[backup] file_path=shadowsocks.json backup_path =backup/shadowsocks.json [email] mail_host=smtp.***.com mail_user=***** mail_pass=***** mail_postfix=****