python - 在 login.defs 中使用子进程自动搜索和更改密码老化时遇到问题

标签 python python-3.x linux ubuntu scripting

我正在尝试创建一个 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/

相关文章:

python - Pandas Dataframe 到元组字典

python-3.x - 命令提示符显示 "No such file or directory"但文件存在

python - 计算活人与死人的年龄

python - 使用 Python 3 支持构建 OpenCV

linux - 当前在 Linux 中运行的应用程序

linux - 将多个文件名扩展到 bash 中的一个目录中

python - 如何在没有实例的情况下调用同一类的另一个方法?

python - 我可以覆盖 Python 列表显示吗?

linux - 在PiTooth.py程序中从第二个终端配对后,另一个终端中的程序仍然是 "waiting for connection"

python - PyQt - 基于值更新的闪烁背景颜色