import os import subprocess files_to_backup = [ "/etc/pam.d/su", "/etc/pam.d/password-auth-ac", "/etc/pam.d/system-auth", "/etc/login.defs", "/etc/ssh/sshd_config", "/etc/vsftpd/vsftpd.conf", "/etc/profile", "/etc/motd" ] for file_path in files_to_backup: if os.path.exists(file_path): backup_file_path = file_path + ".bak" subprocess.call(["cp", file_path, backup_file_path]) else: print(f"File {file_path} does not exist")
时间: 2024-04-14 22:25:40 浏览: 155
你的代码看起来几乎正确,只是缩进有些问题。请注意在Python中,缩进对代码块的结构非常重要。以下是已做修正的代码:
```python
import os
import subprocess
files_to_backup = [
"/etc/pam.d/su",
"/etc/pam.d/password-auth-ac",
"/etc/pam.d/system-auth",
"/etc/login.defs",
"/etc/ssh/sshd_config",
"/etc/vsftpd/vsftpd.conf",
"/etc/profile",
"/etc/motd"
]
for file_path in files_to_backup:
if os.path.exists(file_path):
backup_file_path = file_path + ".bak"
subprocess.call(["cp", file_path, backup_file_path])
else:
print(f"File {file_path} does not exist")
```
上述代码将检查文件是否存在,并在存在的情况下执行备份操作。如果文件不存在,则会打印相应的消息。
请确保你有足够的权限来执行备份操作,并且文件路径正确。运行代码后,将执行文件备份操作或打印相应的消息。
相关问题
if [ -f "/etc/pam.d/system-auth" ];then if grep -q "password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1" /etc/pam.d/system-auth then sed -i '/password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1/s/^#//' /etc/pam.d/system-auth else echo 'password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1' >> /etc/pam.d/system-auth fi if grep -q "password required pam_unix.so remember=5 use_authtok md5 shadow" /etc/pam.d/system-auth then sed -i '/password required pam_unix.so remember=5 use_authtok md5 shadow/s/^#//' /etc/pam.d/system-auth else echo 'password required pam_unix.so remember=5 use_authtok md5 shadow' >> /etc/pam.d/system-auth fi fi if [ -f "/etc/pam.d/password-auth-ac" ];then if grep -q "password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1" /etc/pam.d/password-auth-ac then sed -i '/password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1/s/^#//' /etc/pam.d/password-auth-ac else echo 'password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1' >> /etc/pam.d/password-auth-ac fi if grep -q "password required pam_unix.so remember=5 use_authtok md5 shadow" /etc/pam.d/password-auth-ac then sed -i '/password required pam_unix.so remember=5 use_authtok md5 shadow/s/^#//' /etc/pam.d/password-auth-ac else echo 'password required pam_unix.so remember=5 use_authtok md5 shadow' >> /etc/pam.d/password-auth-ac fi fi
以下是将这些命令转换为Python代码的示例:
```python
import subprocess
system_auth_file = '/etc/pam.d/system-auth'
password_auth_ac_file = '/etc/pam.d/password-auth-ac'
# Update system-auth file
if subprocess.call('[ -f "/etc/pam.d/system-auth" ]', shell=True) == 0:
if subprocess.call('grep -q "password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1" /etc/pam.d/system-auth', shell=True) == 0:
subprocess.call("sed -i '/password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1/s/^#//' /etc/pam.d/system-auth", shell=True)
else:
subprocess.call("echo 'password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1' >> /etc/pam.d/system-auth", shell=True)
if subprocess.call('grep -q "password required pam_unix.so remember=5 use_authtok md5 shadow" /etc/pam.d/system-auth', shell=True) == 0:
subprocess.call("sed -i '/password required pam_unix.so remember=5 use_authtok md5 shadow/s/^#//' /etc/pam.d/system-auth", shell=True)
else:
subprocess.call("echo 'password required pam_unix.so remember=5 use_authtok md5 shadow' >> /etc/pam.d/system-auth", shell=True)
# Update password-auth-ac file
if subprocess.call('[ -f "/etc/pam.d/password-auth-ac" ]', shell=True) == 0:
if subprocess.call('grep -q "password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1" /etc/pam.d/password-auth-ac', shell=True) == 0:
subprocess.call("sed -i '/password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1/s/^#//' /etc/pam.d/password-auth-ac", shell=True)
else:
subprocess.call("echo 'password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1' >> /etc/pam.d/password-auth-ac", shell=True)
if subprocess.call('grep -q "password required pam_unix.so remember=5 use_authtok md5 shadow" /etc/pam.d/password-auth-ac', shell=True) == 0:
subprocess.call("sed -i '/password required pam_unix.so remember=5 use_authtok md5 shadow/s/^#//' /etc/pam.d/password-auth-ac", shell=True)
else:
subprocess.call("echo 'password required pam_unix.so remember=5 use_authtok md5 shadow' >> /etc/pam.d/password-auth-ac", shell=True)
```
上面的代码首先检查是否存在`/etc/pam.d/system-auth`文件,如果存在则更新该文件。如果文件中存在`password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1`行,则将其注释去除,否则将该行添加到文件末尾。接着,检查文件中是否存在`password required pam_unix.so remember=5 use_authtok md5 shadow`行,如果不存在则添加该行到文件末尾。
然后,检查是否存在`/etc/pam.d/password-auth-ac`文件,如果存在则更新该文件。如果文件中存在`password requisite pam_cracklib.so retry=3 minlen=8 minclass=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1`行,则将其注释去除,否则将该行添加到文件末尾。接着,检查文件中是否存在`password required pam_unix.so remember=5 use_authtok md5 shadow`行,如果不存在则添加该行到文件末尾。
请确保你具有足够的权限来执行这些操作,并将需要修改的文件路径正确添加到`system_auth_file`和`password_auth_ac_file`变量中。运行代码后,命令将被执行。
python2 查看是否存在下面的文件并备份 cp /etc/pam.d/su /etc/pam.d/su.bak cp /etc/pam.d/password-auth-ac /etc/pam.d/password-auth-ac.bak cp /etc/pam.d/system-auth /etc/pam.d/system-auth.bak cp /etc/login.defs /etc/login.defs.bak cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak cp /etc/profile /etc/profile.bak cp /etc/motd /etc/motd.bak
以下是将这些命令转换为Python代码的示例:
```python
import subprocess
files_to_backup = [
"/etc/pam.d/su",
"/etc/pam.d/password-auth-ac",
"/etc/pam.d/system-auth",
"/etc/login.defs",
"/etc/ssh/sshd_config",
"/etc/vsftpd/vsftpd.conf",
"/etc/profile",
"/etc/motd"
]
for file_path in files_to_backup:
backup_file_path = file_path + ".bak"
subprocess.call(["cp", file_path, backup_file_path])
print("Backup completed.")
```
上述代码使用循环来遍历要备份的文件路径列表。对于每个文件,它使用`cp`命令将原文件复制为具有`.bak`扩展名的备份文件。
请确保你具有足够的权限来执行这些操作,并将需要备份的文件路径添加到`files_to_backup`列表中。运行代码后,文件将被备份,并打印出`Backup completed.`。
阅读全文