簡體   English   中英

如何在python中運行多個fedora命令

[英]how to run multiple fedora commands in python

所以我試圖讓 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/幾天后你就會跟上進度。

最好的祝福。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM