python - 修改一个linux文件,用Python保存

标签 python linux

我的目标是修改 linux 配置文件并更新以下设置之一:

PasswordAuthentication no --> PasswordAuthentication yes


我有

import os
import fileinput


username = raw_input("Enter username : ")
os.system("adduser -m "+username )
os.system("echo {PASSWORD-HERE} | passwd" + username)
os.system("usermod -aG sudo "+username )
os.system("chsh -s /bin/bash "+username )

with fileinput.FileInput('/etc/ssh/sshd_config', inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace('PasswordAuthentication yes', 'PasswordAuthentication no'), end='')

os.system("service ssh restart ")

我在正确的轨道上吗?

最佳答案

str.replace 会将第一个参数作为“旧字符串”,将第二个参数作为“新字符串”

str.replace(old, new[, max])

你给的是反的。

关于python - 修改一个linux文件,用Python保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38517652/

相关文章:

python - 使用 matplotlib.pyplot 和 numpy 在 python 中显示 Mandelbrot 集

python - 退出 tkinter gui,没有错误

linux - GREP 或 AWK : Search in the first N characters of each line, 并输出匹配模式的周围行

c++ - 在为视频游戏实现基本的二进制序列化时,是否需要担心可移植性?

linux - 无法为 ARM 交叉编译 postgresql 12.2

python - 使用 python 脚本搜索多个文件并为每个文件输出一个单独的文件

python 版本与 json.dumps 冲突

匹配 'category/' 或 'categories/' 的 Python 正则表达式?

python - 如何使用 python 脚本将 EML 文件作为电子邮件一次发送一个电子邮件列表?

python - Tkinter 字体大小仅影响 Raspberry pi 上字母之间的间隙