简体   繁体   English

如何在python中运行多个fedora命令

[英]how to run multiple fedora commands in python

So I'm trying to have Python run multiple commands to install programs and enable SSH to setup my Linux computer.所以我试图让 Python 运行多个命令来安装程序并启用 SSH 来设置我的 Linux 计算机。 I would type all this in, but I'll be doing this to more devices, so I figured why not put in a Python script, but so far it's easier said than done.我会输入所有这些,但我将在更多设备上执行此操作,所以我想为什么不输入 Python 脚本,但到目前为止,说起来容易做起来难。 I did a boatload of research on this and I can't find anything like this.我对此进行了大量研究,但找不到类似的东西。

So here's what I got so far.所以这就是我到目前为止所得到的。

--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])

The first part of this works perfectly.这的第一部分完美地工作。 It asks for my password it'll update and install nmap.它要求我输入密码,它会更新并安装 nmap。 But for some reason the command "systemctl enable sshd" seems to always throw it off.但出于某种原因,命令“systemctl enable sshd”似乎总是把它扔掉。 I know the command works because I can just type it out and it'll work just fine by itself, but for some reason it won't work through this script.我知道该命令有效,因为我可以直接输入它并且它本身就可以正常工作,但由于某种原因它无法通过这个脚本工作。 I've used subprocess.run as well.我也使用过subprocess.run What am I missing here?我在这里缺少什么?

Here's the error that I get:这是我得到的错误:

--sudo: systemctl start sshd: command not found

What you want is Ansible.你想要的是 Ansible。

Ansible uses SSH to connect to list of machines and perform configuration tasks. Ansible 使用 SSH 连接到机器列表并执行配置任务。 Tasks are described in YAML, which is readable and scale.任务在 YAML 中进行描述,它具有可读性和可扩展性。 You can have playbooks and ad hoc commands.您可以拥有剧本和临时命令。 For example ad hoc to install package will be例如,临时安装包将是

ansible -i inventory.file -m yum -a "name=payload state=present"

In a playbook will look like Install and enable openssh-server在剧本中看起来像安装并启用 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 have great documentation https://docs.ansible.com/ in a few days you will be up to speed. Ansible 有很棒的文档https://docs.ansible.com/几天后你就会跟上进度。

Best regards.最好的祝福。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM