将当前shell脚本承载业务使用python实现出来 ··· sh /srv/root_password.sh getenforce#查看SELinux 安全机制 setenforce 0#将 SELinux 的执行模式从强制模式(Enforcing)切换为宽松模式(Permissive) cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/sshd.old.service sed -i -e 's/^Type=.*/Type=simple/g' -e '/ExecStart=/{s/\/usr\/sbin\/sshd/\/usr\/local\/sbin\/sshd/g}' /usr/lib/systemd/system/sshd.service#将文件中以"Type="开头的行替换为"Type=simple";包含"ExecStart="的行中,将"/usr/sbin/sshd"替换为"/usr/local/sbin/sshd" sed -i "s/GSSAPIAuthentication/#GSSAPIAuthentication/" /etc/ssh/sshd_config#将/etc/ssh/sshd_config文件中的"GSSAPIAuthentication"替换为"#GSSAPIAuthentication" sed -i "s/GSSAPICleanupCredentials/#GSSAPICleanupCredentials/" /etc/ssh/sshd_config sed -i "s/#PermitRootLogin yes/PermitRootLogin yes/" /etc/ssh/sshd_config sed -i "s/#UsePAM yes/UsePAM yes/" /etc/ssh/sshd_config chmod 600 /etc/ssh/ssh_host_rsa_key chmod 600 /etc/ssh/ssh_host_ecdsa_key chmod 600 /etc/ssh/ssh_host_ed25519_key ···
时间: 2023-07-17 07:03:52 浏览: 192
下面是将您提供的Shell脚本转换为Python代码的示例:
```python
import subprocess
# 执行命令的函数
def run_command(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, _ = process.communicate()
return output.decode().strip()
# 执行脚本中的命令
run_command("sh /srv/root_password.sh")
run_command("setenforce 0")
run_command("cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/sshd.old.service")
run_command("sed -i -e 's/^Type=.*/Type=simple/g' -e '/ExecStart=/{s/\\/usr\\/sbin\\/sshd/\\/usr\\/local\\/sbin\\/sshd/g}' /usr/lib/systemd/system/sshd.service")
run_command("sed -i 's/GSSAPIAuthentication/#GSSAPIAuthentication/' /etc/ssh/sshd_config")
run_command("sed -i 's/GSSAPICleanupCredentials/#GSSAPICleanupCredentials/' /etc/ssh/sshd_config")
run_command("sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config")
run_command("sed -i 's/#UsePAM yes/UsePAM yes/' /etc/ssh/sshd_config")
run_command("chmod 600 /etc/ssh/ssh_host_rsa_key")
run_command("chmod 600 /etc/ssh/ssh_host_ecdsa_key")
run_command("chmod 600 /etc/ssh/ssh_host_ed25519_key")
```
上述代码使用`subprocess`模块执行Shell命令,并将其转换为Python函数`run_command()`。然后,按照原始脚本中的顺序调用这些命令。
请注意,Python代码的执行结果可能会与直接在Shell中运行命令时略有不同。如果有任何问题,请及时反馈。
阅读全文