我正在尝试创建一个 python 脚本,它会在我的 Linux 机器上自动执行一些安全检查。我想用它来将 login.defs 中的 PASS_MIN_DAYS 0 更改为更大的数字。
我已经在一个独立的 .txt 文件上使用了这段代码,该文件将一个简单的短语从一个字符串更改为另一个字符串,但现在当我移至 login.defs 时,它将不起作用。我不确定它是否必须对文件本身或我的代码做任何事情。
def password():
os.chdir('/etc')
with open('login.defs', 'r') as file:
newline=[]
for word in file.readlines():
newline.append(word.replace("PASS_MIN_DAYS", "PASS_MIN_DAYS 7"))
newline.append(word.replace("PASS_MAX_DAYS", "PASS_MAX_DAYS 90"))
newline.append(word.repalce("PASS_WARN_AGE", "PASS_WARN_AGE 14"))
with open('login.defs', 'w') as file:
for line in newline:
file.writelines(line)
file.close()
不包含任何错误消息。如果格式不正确,请提前致歉,这是我第一次在 stackvoverflow 上发帖 :)
最佳答案
subprocess.run(['sed', '-i', '/PASS_MIN_DAYS/c\PASS_MIN_DAYS 7', "login.defs"])
subprocess.run(['sed', '-i', '/PASS_MAX_DAYS/c\PASS_MAX_DAYS 90', "login.defs"])
subprocess.run(['sed', '-i', '/PASS_WARN_AGE/c\PASS_WARN_AGE 14', "login.defs"])
This is the answer that i found that fixes it
关于python - 在 login.defs 中使用子进程自动搜索和更改密码老化时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58036813/