所以这是我的代码:https://github.com/trisimix/serialcontrol/blob/master/serialcontrol.py
import subprocess
import os
import stat
dmi = subprocess.check_output('dmidecode', shell=True)
#print(variable)
#run program once as root then cron it as root
try :
file = open("/var/log/serialcontrol/dmidecode.txt", "r")
file.close()
except FileNotFoundError:
script = '/var/tmp/serialcontrol.bash'
with open(script, 'w') as file:
file.write("#!/bin/bash/\nif [ ! -d /var/log/serialcontrol/ ]\nthen\n\tmkdir /var/log/serialcontrol/\nfi");
#st = os.stat(script)
#os.chmod(script, st.st_mode | stat.S_IEXEC)
subprocess.call(["bash", script])
subprocess.call(["rm", script])
#with open('/var/log/serialcontrol/dmidecode.txt' , 'w') as file:
# file.write(dmi);
file = open("/var/log/serialcontrol/dmidecode.txt" , "w");
dmi = str(dmi)
dmi = dmi.replace('\\n', '\n')
dmi = dmi.replace('\\t', '\t')
file.write(dmi)
file.close()
script2 = '/var/log/serialcontrol/serialcontro1.bash'
#with open(script2, 'w') as file:
# file.write('#!/bin/bash\nrecipients="archmachine9@gmail.com"\nsubject="...Subject..."\necho -e "to: $recipients\nsubject: $subject\n"| (cat - &&uuencode /var/log/serialcontrol/dmidecode.txt) | ssmtp archmachine9@gmail.com')
import smtplib
sender = 'archmachine9@gmail.com'
receivers = 'archmachine9@gmail.com'
message = "\r\n".join([
"From: archmachine9@gmail.com",
"To: archmachine9@gmail.com",
"Subject: SerialControl",
"",
dmi
])
username = 'archmachine9@gmail.com'
password = ''
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(sender, receivers, message)
server.quit()
#subprocess.call(["bash", script2])
#this sub is supposed to /n with actual /n's
#subprocess.run(["sed -i 's/\\n/\n/g' /var/log/serialcontrol/dmidecode.txt"], shell=True)
#except FileNotFoundError:
#file = open('/var/tmp/serialcontrol.bash', 'w')
#file.write("#!/bin/bash/\nif [ ! -d /var/log/serialcontrol/]\nthen\n\tmkdir /var/log/serialcontrol/\nfi");
#file.close()
#st = os.stat("/var/tmp/serialcontrol.bash")
#os.chmod("/var/tmp/serialcontrol.bash", st.st_mode | stat.S_IEXEC)
#subprocess.call("/var/tmp/serialcontrol.bash")
我知道它真的,真的,真的,真的,真的,不好但是在密码段中我必须先输入我的密码然后才能输入这个脚本并且密码以明文形式存储,如何修复?
答案 0 :(得分:0)
当您使用bash标记问题时,我将假设您使用的是Linux或类Unix系统。
这就是我所说的机器密码。如果您希望从通过cron启动的脚本中使用它,则必须可以在磁盘上的某个位置访问。
正确(或不太糟糕)的方式是:
archmachine9
暗示您已经这样做了......) - 理想情况下应该是不可能的以密码字段中的此帐户(a single
*`登录,例如)这样,即使邮件帐户是通过不同的脚本共享的,您也会有一个中心点来更改密码,因为它存储在只能访问系统帐户的文件中,只有机器管理员(root)应该能够阅读它。而且你不必知道它来开发,维护和测试脚本,你只需要在你的开发环境中放置一个测试邮件帐户
TL / DR:密码必须以明文形式存储在机器上,以便脚本可以通过cron启动,但它不应该在脚本本身中