用python2将下面的shell实现 ``` 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 mv /usr/local/sbin/sshd /usr/local/sbin/sshd.old202211 mv /lib64/libcrypto.so.1.1 /lib64/libcrypto.so.1.1.old202211 mv /srv/libcrypto.so.1.1 /lib64/ mv /srv/sshd /usr/local/sbin/ chmod 755 /usr/local/sbin/sshd chmod 755 /lib64/libcrypto.so.1.1 systemctl daemon-reload systemctl restart sshd.service source /etc/profile sshd -V useradd secure echo 'Ofm#6%3%fm0IWH'|passwd --stdin secure echo "secure ALL=(ALL) ALL" >> /etc/sudoers sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config service sshd restart systemctl daemon-reload systemctl restart sshd.service sed -i "s/secure ALL=(ALL) ALL/secure ALL=(ALL) NOPASSWD: ALL/" /etc/sudoers chage -M 99999 secure sleep 2 useradd aqassoc echo 'dAxgDt^7LT' | passwd --stdin aqassoc mkdir /batshell chown -R aqassoc:aqassoc /batshell echo 'aqassoc ALL=(ALL) NOPASSWD:ALL,!/bin/su,!/sbin/init,!/sbin/shutdown,!/sbin/halt,!/sbin/reboot,!/usr/sbin/visudo,!/usr/bin/passwd' >> /etc/sudoers sh /srv/script.sh sleep 2 tar -xvf /srv/ssl-1.1.1o* -C /srv/ mv /usr/bin/openssl /usr/bin/openssl.old mv /usr/include/openssl /usr/include/openssl.old cp /etc/ld.so.conf /etc/ld.so.conf.old ln -s /srv/ssl-1.1.1o/bin/openssl /usr/bin/openssl ln -s /srv/ssl-1.1.1o/include/openssl /usr/include/openssl srvlib=`cat /etc/ld.so.conf | grep '/srv/ssl-1.1.1o/lib/' | wc -l` if [ $srvlib -eq 0 ];then sed -i 's/\/srv/#\/srv/'g /etc/ld.so.conf echo '/srv/ssl-1.1.1o/lib/' >> /etc/ld.so.conf else echo 'ssl-1.1.1n is ok!' fi ldconfig -v |grep ssl openssl version rm -rf /srv/root_password.sh rm -rf /srv/ssl-1.1.1o_7.tar.gz curl --connect-timeout 10 -# http://133.64.86.246/iso/shell/initialization.sh -o /tmp/initialization.sh # 只有两个参数 dcn/dmz netbond/netskip baseline/baseskip sh /tmp/initialization.sh dcn netbond/netskip baseline/baseskip ```
时间: 2023-07-17 19:08:58 浏览: 194
以下是使用 Python 2 实现上述 Shell 脚本的示例:
```python
import os
import subprocess
# 查看 SELinux 安全机制
subprocess.call(['getenforce'])
# 将 SELinux 的执行模式从强制模式(Enforcing)切换为宽松模式(Permissive)
subprocess.call(['setenforce', '0'])
# 备份 sshd.service 文件
subprocess.call(['cp', '/usr/lib/systemd/system/sshd.service', '/usr/lib/systemd/system/sshd.old.service'])
# 修改 sshd.service 文件内容
subprocess.call(['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'])
# 修改 sshd_config 文件内容
subprocess.call(['sed', '-i', 's/GSSAPIAuthentication/#GSSAPIAuthentication/', '/etc/ssh/sshd_config'])
subprocess.call(['sed', '-i', 's/GSSAPICleanupCredentials/#GSSAPICleanupCredentials/', '/etc/ssh/sshd_config'])
subprocess.call(['sed', '-i', 's/#PermitRootLogin yes/PermitRootLogin yes/', '/etc/ssh/sshd_config'])
subprocess.call(['sed', '-i', 's/#UsePAM yes/UsePAM yes/', '/etc/ssh/sshd_config'])
# 修改文件权限
subprocess.call(['chmod', '600', '/etc/ssh/ssh_host_rsa_key'])
subprocess.call(['chmod', '600', '/etc/ssh/ssh_host_ecdsa_key'])
subprocess.call(['chmod', '600', '/etc/ssh/ssh_host_ed25519_key'])
# 移动文件和更改权限
subprocess.call(['mv', '/usr/local/sbin/sshd', '/usr/local/sbin/sshd.old202211'])
subprocess.call(['mv', '/lib64/libcrypto.so.1.1', '/lib64/libcrypto.so.1.1.old202211'])
subprocess.call(['mv', '/srv/libcrypto.so.1.1', '/lib64/'])
subprocess.call(['mv', '/srv/sshd', '/usr/local/sbin/'])
subprocess.call(['chmod', '755', '/usr/local/sbin/sshd'])
subprocess.call(['chmod', '755', '/lib64/libcrypto.so.1.1'])
subprocess.call(['systemctl', 'daemon-reload'])
subprocess.call(['systemctl', 'restart', 'sshd.service'])
subprocess.call(['source', '/etc/profile'])
subprocess.call(['sshd', '-V'])
# 创建用户 secure,并设置密码,配置 sudo 权限
subprocess.call(['useradd', 'secure'])
subprocess.call("echo 'Ofm#6%3%fm0IWH'|passwd --stdin secure", shell=True)
with open('/etc/sudoers', 'a') as sudoers_file:
sudoers_file.write("secure ALL=(ALL) ALL\n")
subprocess.call(['sed', '-i', 's/PermitRootLogin yes/PermitRootLogin no/', '/etc/ssh/sshd_config'])
subprocess.call(['service', 'sshd', 'restart'])
subprocess.call(['systemctl', 'daemon-reload'])
subprocess.call(['systemctl', 'restart', 'sshd.service'])
subprocess.call(['sed', '-i', 's/secure ALL=(ALL) ALL/secure ALL=(ALL) NOPASSWD: ALL/', '/etc/sudoers'])
subprocess.call(['chage', '-M', '99999', 'secure'])
subprocess.call(['sleep', '2'])
# 创建用户 aqassoc,并设置密码,创建目录并设置权限,配置 sudo 权限,执行脚本
subprocess.call(['useradd', 'aqassoc'])
subprocess.call("echo 'dAxgDt^7LT' | passwd --stdin aqassoc", shell=True)
subprocess.call(['mkdir', '/batshell'])
subprocess.call(['chown', '-R', 'aqassoc:aqassoc', '/batshell'])
with open('/etc/sudoers', 'a') as sudoers_file:
sudoers_file.write("aqassoc ALL=(ALL) NOPASSWD:ALL,!/bin/su,!/sbin/init,!/sbin/shutdown,!/sbin/halt,!/sbin/reboot,!/usr/sbin/visudo,!/usr/bin/passwd\n")
subprocess.call(['sh', '/srv/script.sh'])
subprocess.call(['sleep', '2'])
# 解压 ssl-1.1.1o.tar.gz 文件,并设置软链接,配置 ld.so.conf
subprocess.call(['tar', '-xvf', '/srv/ssl-1.1.1o*', '-C', '/srv/'])
subprocess.call(['mv', '/usr/bin/openssl', '/usr/bin/openssl.old'])
subprocess.call(['mv', '/usr/include/openssl', '/usr/include/openssl.old'])
subprocess.call(['cp', '/etc/ld.so.conf', '/etc/ld.so.conf.old'])
subprocess.call(['ln', '-s', '/srv/ssl-1.1.1o/bin/openssl', '/usr/bin/openssl'])
subprocess.call(['ln', '-s', '/srv/ssl-1.1.1o/include/openssl', '/usr/include/openssl'])
srvlib = subprocess.check_output("cat /etc/ld.so.conf | grep '/srv/ssl-1.1.1o/lib/' | wc -l", shell=True)
if int(srvlib) == 0:
subprocess.call(['sed', '-i', "s/\/srv/#\/srv/g", "/etc/ld.so.conf"])
subprocess.call(['echo', "'/srv/ssl-1.1.1o/lib/' >> /etc/ld.so.conf"])
else:
print('ssl-1.1.1n is ok!')
subprocess.call(['ldconfig', '-v', '|grep', 'ssl'])
subprocess.call(['openssl', 'version'])
# 清理临时文件
subprocess.call(['rm', '-rf', '/srv/root_password.sh'])
subprocess.call(['rm', '-rf', '/srv/ssl-1.1.1o_7.tar.gz'])
subprocess.call(['curl', '--connect-timeout', '10', '-#', 'http://133.64.86.246/iso/shell/initialization.sh', '-o', '/tmp/initialization.sh'])
# 执行 initialization.sh 脚本,传递参数
subprocess.call(['sh', '/tmp/initialization.sh', 'dcn', 'netbond/netskip', 'baseline/baseskip'])
```
这是一个使用`subprocess`模块调用Shell命令的示例。请确保你在执行这段代码之前对每个操作和命令的含义和可能的影响有充分的了解,并在适当的环境中运行它。
阅读全文