我正在运行python脚本来扫描区域中的假AP。目前,该脚本已配置为通过以下代码向管理员发送电子邮件通知。我想创建一个新的.csv文件并将结果(消息)保存到csv文件,并取消所有发送消息通知的代码。我试图写一个简单的脚本来帮助解决这个问题,但似乎没有任何工作。任何帮助解决这个问题将不胜感激。提前谢谢
# sending an alert to the admin email
def AlertAdmin(message):
try:
cmd = "select opt_val from options where opt_key = 'admin_email'"
cursor.execute(cmd)
if cursor.rowcount > 0:
row = cursor.fetchone()
admin_email = row[0]
cmd = "select opt_val from options where opt_key = 'admin_smtp'"
cursor.execute(cmd)
if cursor.rowcount > 0:
row = cursor.fetchone()
admin_smtp = row[0]
cmd = "select opt_val from options where opt_key = 'admin_smtp_username'"
cursor.execute(cmd)
if cursor.rowcount > 0:
row = cursor.fetchone()
admin_smtp_username = row[0]
cmd = "select opt_val from options where opt_key = 'admin_smtp_password'"
cursor.execute(cmd)
if cursor.rowcount > 0:
row = cursor.fetchone()
admin_smtp_password = row[0]
message = "From: EvilAP_Defender <{}>\nTo: Admin <{}>\nSubject: EvilAP_Defender Alert!\n\n"\
.format(admin_smtp_username, admin_email) + message
try:
print bcolors.OKBLUE + "\nConnecting to SMTP server\n" + bcolors.ENDC
mailsrv = smtplib.SMTP(admin_smtp,587)
print bcolors.OKBLUE + "\nSending ehlo message to SMTP server\n" + bcolors.ENDC
mailsrv.ehlo()
print bcolors.OKBLUE + "\nStarting TLS with SMTP server\n" + bcolors.ENDC
mailsrv.starttls()
print bcolors.OKBLUE + "\nSending ehlo message to SMTP server\n" + bcolors.ENDC
mailsrv.ehlo()
print bcolors.OKBLUE + "\nLogin to SMTP server\n" + bcolors.ENDC
mailsrv.login(admin_smtp_username,admin_smtp_password)
print bcolors.OKBLUE + "\nSending the message ...\n" + bcolors.ENDC
mailsrv.sendmail(admin_smtp_username, admin_email, message)
print bcolors.OKBLUE + "\nDisconnecting from mail server ...\n" + bcolors.ENDC
mailsrv.quit()
print bcolors.OKGREEN + bcolors.BOLD + "\nSuccessfully sent email to admin\n" + bcolors.ENDC
except:
print bcolors.FAIL + bcolors.BOLD + "\nError: unable to send an email to admin: {}\n".format(sys.exc_info()[0]) + bcolors.ENDC
#print bcolors.OKGREEN + bcolors.BOLD + "\nSuccessfully sent email to admin\n" + bcolors.ENDC
else:
print bcolors.WARNING + "Cannot send alert. SMTP password not found!\nConfigure admin notification from Learning Mode\n" + bcolors.ENDC
else:
print bcolors.WARNING + "Cannot send alert. SMTP username not found!\nConfigure admin notification from Learning Mode\n" + bcolors.ENDC
else:
print bcolors.WARNING + "Cannot send alert. SMTP address not found!\nConfigure admin notification from Learning Mode\n" + bcolors.ENDC
else:
print bcolors.WARNING + "Cannot send alert. Admin email not found!\nConfigure admin notification from Learning Mode\n" + bcolors.ENDC
except:
print bcolors.FAIL + bcolors.BOLD + "Unexpected error in 'AlertAdmin': {}\n".format(sys.exc_info()[0]) + bcolors.ENDC
#print bcolors.OKGREEN + bcolors.BOLD + "\nSuccessfully sent email to admin\n" + bcolors.ENDC
return
答案 0 :(得分:0)
在文件写入方面,非常基础将类似于python3.6中的内容。阅读类似于模式&#39; r&#39; 。 。
import csv
with open('myfile.txt', mode='wt', encoding='utf-8') as f:
f.write('line1')
f.write('line2')
f.close()
答案 1 :(得分:0)
使用以下代码更新try-except
块:
try:
ofile = open('messagelog.csv', "ab")
writer = csv.writer(ofile, quoting=csv.QUOTE_ALL)
record = [admin_smtp_username, admin_email, message]
writer.writerow(record)
ofile.close()
except:
print bcolors.FAIL + bcolors.BOLD + "\nError: unable to write message to log file: {}\n".format(sys.exc_info()[0]) + bcolors.ENDC
注意:不要忘记import csv