Do tego celu można użyć modułu boto.manage.cmdshell. Aby z niego skorzystać, musisz mieć zainstalowany pakiet Paramiko. Prosty przykład użycia:
import boto.ec2
from boto.manage.cmdshell import sshclient_from_instance
# Connect to your region of choice
conn = boto.ec2.connect_to_region('us-west-2')
# Find the instance object related to my instanceId
instance = conn.get_all_instances(['i-12345678'])[0].instances[0]
# Create an SSH client for our instance
# key_path is the path to the SSH private key associated with instance
# user_name is the user to login as on the instance (e.g. ubuntu, ec2-user, etc.)
ssh_client = sshclient_from_instance(instance,
'<path to SSH keyfile>',
user_name='ec2-user')
# Run the command. Returns a tuple consisting of:
# The integer status of the command
# A string containing the output of the command
# A string containing the stderr output of the command
status, stdout, stderr = ssh_client.run('ls -al')
To zostało wpisane z pamięci, ale uważam, że jest poprawne.
Można również sprawdzić Fabric (http://docs.fabfile.org/), który ma podobną funkcjonalność, ale ma również znacznie bardziej zaawansowane funkcje i możliwości.
Dzięki Steffen za montaż. Zapamięta poprawki. – vibhor
Powiązane: [Jak korzystać z SSH i uruchamiać polecenia w EC2 przy użyciu boto3?] (Https://stackoverflow.com/q/42645196/55075) – kenorb