在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
这些方法都可以实现远程触发命令执行。你可以根据自己的需求选择合适的方法。