def SEND_MAIL(k,sender,password): ''' send mail ''' subject = 'python email test' smtpserver = 'smtp.163.com' msg = MIMEMultipart('alternative') msg['Subject'] = u"共享文件服务器" company_name = k[0].split('_')[0] user = k[1]['username'] Pass = k[1]['passwd'] if 'vpn' in k[1]: html = u"""\ 您好,给你新开的VPN账号和密码为:$user,$vpn_pass。VPN连接方法见附件,不会连接VPN的联系网管设定使用。 您需要的文件系统账号跟密码为:$user,$Pass。 文件系统使用方法:使用前必须连接上VPN,打开计算机在地址栏输入\\192.168.1.240,然后进入$company_name目录。 以后文件系统的权限修改请联系[email protected]! xxx-xxx """ html = Template(html) if 'vpn' in k[1]: html = html.substitute(user=k[1]['username'],Pass=k[1]['passwd'],vpn_pass=k[1]['vpn']['vpn_pass'],company_name=k[0].split('_')[0]) try: attach_file = gain_profilePath('/mail attachment')+userconf()['file name'] att = MIMEText(open(attach_file, 'rb').read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="file"' msg.attach(att) part2 = MIMEText(html, 'html','utf-8') msg.attach(part2) except Exception,e: error_info = e log_record(error_info) exit(0)
def userconf(): try: config = yaml.load(file(gain_profilePath('/etc/userconf.yaml'), 'r')) return config except Exception,e: error_info = e log_record(error_info) exit(0)
def del_account(i): if i[1]['op'] == 'del': try: os.system('userdel '+i[1]['username']) except Exception,e: error_info = i[1]['username']+" Delete accounts abnormal! \n" log_record(error_info) exit(0)
def YAML_RECO(): ''' load yaml doc ''' try: config = yaml.load(file(gain_profilePath('/etc/name.yaml'), 'r')) return config log_record("name.yaml open success!") except Exception,e: error_info = e log_record(error_info) exit(0)
def add_account(i,x,y): if i[1]['op'] == 'add': try: os.system('useradd -s /sbin/nologin '+i[1]['username']) except Exception,e: error_info = i[1]['username'] + " Account add exception\n" log_record(error_info) exit(0) print i[1]['passwd'] child = pexpect.spawn (x) child.expect ('New SMB password:'******'Retype new SMB password:'******'username']+" add success" log_record(debug_info)
def add_mod_dir_auth(i): if i[1]['op'] != 'del': # try: for n in i[1]['dir'].items(): try: os.path.isdir(n[0]) facl_fir = 'setfacl -d -R -m user:'******'username']+':'+n[1]+' '+n[0] facl_sec = 'setfacl -R -m user:'******'username']+':'+n[1]+' '+n[0] except Exception,e: error_info = n[0]+" dir is not exists!\n" log_record(error_info) exit(0) # except Exception,e: # log_record(e) # exit(0) try: os.system(facl_fir.encode("utf8")) os.system(facl_sec.encode("utf8")) except Exception,e: error_info = i[1]['username']+facl_fir+" Add permissions abnormal!\n" log_record(error_info) exit(0)
以后文件系统的权限修改请联系[email protected]! xxx-xxx """ html = Template(html) html = html.substitute(user=k[1]['username'],Pass=k[1]['passwd'],company_name=k[0]) msg = MIMEText(html,'text','utf-8') #构造附件 try: smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(sender, password) smtp.sendmail(sender, k[1]['user_mail'], msg.as_string()) smtp.quit() except Exception,e: error_info = e log_record(error_info) exit(0) #yaml解析 def YAML_RECO(): ''' load yaml doc ''' try: config = yaml.load(file(gain_profilePath('/etc/name.yaml'), 'r')) return config log_record("name.yaml open success!") except Exception,e: error_info = e log_record(error_info) exit(0)