在Ubuntu系統中,可以通過多種方式實現遠程觸發命令執行。以下是一些常見的方法:
ssh 用戶名@目標機器IP '要執行的命令'
例如,要在遠程機器上執行ls
命令,可以使用以下命令:
ssh user@192.168.1.100 'ls'
/etc/ansible/hosts
文件中添加目標機器的信息。例如:[remote_servers]
192.168.1.100 ansible_user=user
接下來,創建一個名為remote_command.yml
的Ansible playbook文件,包含要執行的命令:
---
- hosts: remote_servers
tasks:
- name: Execute command on remote server
ansible.builtin.command: 要執行的命令
最后,運行Ansible playbook:
ansible-playbook remote_command.yml
pip install fabric
然后,創建一個名為fabfile.py
的Fabric腳本文件,包含要執行的命令:
from fabric import Connection
def execute_command():
c = Connection('目標機器IP', user='用戶名')
result = c.run('要執行的命令', hide=True)
print(result.stdout)
最后,運行Fabric腳本:
fab execute_command
這些方法都可以實現遠程觸發命令執行。你可以根據自己的需求選擇合適的方法。