简体   繁体   English

如何运行位于 AWS EC2 服务器上的 python 脚本?

[英]How do I run a python script that is located on the AWS EC2 server?

I want to use python code on my computer to run a python script that is located on the server (EC2 ubuntu 18).我想在我的计算机上使用 python 代码来运行位于服务器上的 python 脚本(EC2 ubuntu 18)。 I understand that you can use boto for this, but I didn't find a full-fledged example where it would be written here is the server, we connect to it like this, we execute the script like this.我知道您可以为此使用 boto,但我没有找到一个完整的示例,它可以在这里编写为服务器,我们像这样连接到它,我们像这样执行脚本。

You can do so by using AWS SSM or lambda function.您可以使用 AWS SSM 或 lambda function 来执行此操作。

Refer to @mokugo-devops 's answer for AWS SSM请参阅 @mokugo-devops对 AWS SSM 的回答

or Refer to this for lambda function approach或参考此 lambda function 方法

#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')
    }

Take a look at AWS SSM - Run Command .看看 AWS SSM - 运行命令

From your local Python script you can run the send-command从本地 Python 脚本中,您可以运行send-command

You can either:您可以:

To execute you this, you will need to ensure that the target instance has SSM Agent is intalled, and that instance has a role with the correct privileges .要执行此操作,您需要确保目标实例已安装SSM 代理,并且该实例具有具有正确权限的角色。 Example command示例命令

import boto3
client = boto3.client('ssm')

client.send_command(
    InstanceIds=[
        'i-01234567',
    ],
    DocumentName='AWS-RunShellScript',
    Parameters={
        'commands': [
            'python3 /home/ec2-user/main.py',
        ]
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在AWS EC2服务器上启动时运行命令(python文件) - How run a command (python file) on boot on AWS EC2 server 如何在 EC2 服务器上连续运行 Python 脚本? - How to continuously run a Python script on an EC2 server? 在Amazon EC2中,当我“克隆”该实例时,如何让它运行python脚本? - In Amazon EC2, how do I make it run a python script when I “clone” that instance? 如何在不使用完整服务器或 ec2 实例的情况下在 cronjob 中运行 python 脚本? - How can i run a python script in cronjob without utilising the full server or ec2 instance? 如何从python脚本'刀EC2服务器创建' - how to do 'knife ec2 server create' from python script 如何在 AWS EC2 实例上安装 Python 3? - How do I install Python 3 on an AWS EC2 instance? 如何永远运行python脚本EC2? - How to run python script forever EC2? 在 AWS 上运行 Python 脚本并将 5GB 的文件传输到 EC2 - Run Python Script on AWS and transfer 5GB of files to EC2 每当触发 lambda 时,如何运行 AWS EC2 中存在的 python 脚本? - How to run a python script present in an AWS EC2 whenever a lambda is triggered? 如何在 AWS EC2 上运行 Python 代码并将 csv 文件从服务器写入我的本地计算机? - How to run a Python code on AWS EC2 and write a csv file from the server to my local machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM