如何运行位于AWS EC2服务器上的python脚本?
我想在我的计算机上使用python代码来运行位于服务器(EC2 Ubuntu18)上的python脚本。我知道您可以使用boto来实现这一点,但我没有找到一个完整的例子,在这里它将被写在服务器上,我们像这样连接到它,我们像这样执行脚本。看看AWS SSM- 从本地Python脚本可以运行 您可以:如何运行位于AWS EC2服务器上的python脚本?,python,python-3.x,amazon-web-services,amazon-ec2,boto3,Python,Python 3.x,Amazon Web Services,Amazon Ec2,Boto3,我想在我的计算机上使用python代码来运行位于服务器(EC2 Ubuntu18)上的python脚本。我知道您可以使用boto来实现这一点,但我没有找到一个完整的例子,在这里它将被写在服务器上,我们像这样连接到它,我们像这样执行脚本。看看AWS SSM- 从本地Python脚本可以运行 您可以: 创造你自己的 使用文档 要执行此操作,您需要确保已装入目标实例,并且该实例具有具有的角色。 示例命令 import boto3 client = boto3.client('ssm') clie
- 创造你自己的
- 使用文档
import boto3
client = boto3.client('ssm')
client.send_command(
InstanceIds=[
'i-01234567',
],
DocumentName='AWS-RunShellScript',
Parameters={
'commands': [
'python3 /home/ec2-user/main.py',
]
}
)
看看AWS SSM-
从本地Python脚本可以运行
您可以:
- 创造你自己的
- 使用文档
import boto3
client = boto3.client('ssm')
client.send_command(
InstanceIds=[
'i-01234567',
],
DocumentName='AWS-RunShellScript',
Parameters={
'commands': [
'python3 /home/ec2-user/main.py',
]
}
)
您可以使用AWS SSM或lambda函数来执行此操作 参考@mokugo devops的 或参考此,了解lambda函数方法
#requires paramiko package
#paramiko package is available at:
# https://github.com/pranavmalaviya2/COVID-19-Live-Data-board/tree/master/lambda%20functions/SSH_lambda-Deployment-package
import json
import boto3
import paramiko
import time
def lambda_handler(event, context):
# boto3 client
client = boto3.client('ec2')
s3_client = boto3.client('s3')
# getting instance information
describeInstance = client.describe_instances()
# downloading pem file from S3
s3_client.download_file('bucket-name','key-name.pem', '/destination/folder/new-key-name.pem')
# reading pem file and creating key object
key = paramiko.RSAKey.from_private_key_file("/destination/folder/new-key-name.pem")
# an instance of the Paramiko.SSHClient
ssh_client = paramiko.SSHClient()
# setting policy to connect to unknown host
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# connect using ec2 instance ID if requires
ssh_client.connect(hostname="12.12.12.12", username="ubuntu", pkey=key)
# command list
commands = [
"python script.py",
"python script2.py",
"aws s3 cp --recursive source/ s3://destination-bucket/",
]
# executing list of commands within server
print("Starting execution")
for command in commands:
print("Executing command: " + command)
stdin , stdout, stderr = ssh_client.exec_command(command)
print(stdout.read())
print(stderr.read())
print("finished execution")
return {
'statusCode': 200,
'body': json.dumps('Execution Completed')
}
您可以使用AWS SSM或lambda函数来执行此操作 参考@mokugo devops的 或参考此,了解lambda函数方法
#requires paramiko package
#paramiko package is available at:
# https://github.com/pranavmalaviya2/COVID-19-Live-Data-board/tree/master/lambda%20functions/SSH_lambda-Deployment-package
import json
import boto3
import paramiko
import time
def lambda_handler(event, context):
# boto3 client
client = boto3.client('ec2')
s3_client = boto3.client('s3')
# getting instance information
describeInstance = client.describe_instances()
# downloading pem file from S3
s3_client.download_file('bucket-name','key-name.pem', '/destination/folder/new-key-name.pem')
# reading pem file and creating key object
key = paramiko.RSAKey.from_private_key_file("/destination/folder/new-key-name.pem")
# an instance of the Paramiko.SSHClient
ssh_client = paramiko.SSHClient()
# setting policy to connect to unknown host
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# connect using ec2 instance ID if requires
ssh_client.connect(hostname="12.12.12.12", username="ubuntu", pkey=key)
# command list
commands = [
"python script.py",
"python script2.py",
"aws s3 cp --recursive source/ s3://destination-bucket/",
]
# executing list of commands within server
print("Starting execution")
for command in commands:
print("Executing command: " + command)
stdin , stdout, stderr = ssh_client.exec_command(command)
print(stdout.read())
print(stderr.read())
print("finished execution")
return {
'statusCode': 200,
'body': json.dumps('Execution Completed')
}
或者你可以提供python代码,我看到的文档)嗨,你可以通过这个提供python代码作为python命令。它的行为就像是在shell命令行上运行一样,您可以使用一个小示例,比如说main.py有一个脚本。开始时导入了哪些库?让我们来看看。或者您可以提供python代码,我看到的文档)嗨,您可以通过此命令以python命令的形式提供python代码。它的行为就像是在shell命令行上运行一样,您可以使用一个小示例,假设main.py有一个脚本。开始时导入了哪些库?让我们来看看。