如何解决如何在python中运行多个fedora命令
所以我试图让 Python 运行多个命令来安装程序并启用 SSH 来设置我的 Linux 计算机。我会输入所有这些,但我将在更多设备上执行此操作,所以我想为什么不输入 Python 脚本,但到目前为止,说起来容易做起来难。我对此进行了大量研究,但找不到类似的内容。
这就是我目前得到的。
--import subprocess
--SSH = "systemctl enable sshd"
--payload = "nmap" # it'll be one of a few I'll be installing
--subprocess.call(["sudo","yum","install","-y",payload])
--subprocess.call(["sudo",SSH])
这的第一部分完美地工作。它要求我输入密码,它会更新并安装 nmap。但出于某种原因,命令“systemctl enable sshd”似乎总是把它扔掉。我知道该命令有效,因为我可以直接输入它并且它本身就可以正常工作,但由于某种原因它无法通过这个脚本工作。我也用过 subprocess.run
。我在这里错过了什么?
这是我得到的错误:
--sudo: systemctl start sshd: command not found
解决方法
你想要的是 Ansible。
Ansible 使用 SSH 连接到机器列表并执行配置任务。任务在 YAML 中进行描述,它具有可读性和可扩展性。您可以拥有剧本和临时命令。例如临时安装包将是
ansible -i inventory.file -m yum -a "name=payload state=present"
在剧本中看起来像安装并启用 openssh-server
---
- hosts: all # Single or group of hosts from inventory file
become: yes # Become sudo
tasks: # List of tasks
- name: Install ssh-server # Description free text
yum: # Module name
name: openssh-server # Name of the package
state: present # State " state: absent will uninstall the package"
- name: Start and enable service # Description of the task free text
service: # Service
name: sshd # Name of the service
state: started # Started or Stopped
enabled: yes # Start the service on boot
- name: Edit config file sshd_config # Description of the task
lineinfile: # Name of the module
path: /etc/sshd/sshd_config # Which file to edit
regex: ^(# *)?PasswordAuthentication # Which line to edit
line: PasswordAuthentication no # Whit what to change it
Ansible 有很棒的文档 https://docs.ansible.com/ 几天后您就会跟上进度。
最好的问候。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。