【问题标题】:AWS Python - Windows - Running a script after I create an instanceAWS Python - Windows - 在我创建实例后运行脚本
【发布时间】:2016-06-27 01:33:08
【问题描述】:

我进步很大。

我的进口是:

import boto.rds as rds
import boto3 as b3
import boto.ec2 as ec2
from boto.manage.cmdshell import sshclient_from_instance

我可以使用以下方式连接到我的 aws:

conn = boto.ec2.connect_to_region('us-west2',aws_access_key_id='MY_ID', aws_secret_access_key='MY_PASS')

我可以使用以下方法创建实例:

conn_args = {
    'aws_access_key_id': 'MY_KEY',
    'aws_secret_access_key': 'MY_PASS',
    'region_name': 'us-west-2'
}

ec2_res = b3.resource('ec2', **conn_args)

new_instance = ec2_res.create_instances(
    ImageId='ami-123456',
    MinCount=1,
    MaxCount=1,
    InstanceType='t2.micro',
    KeyName='my-keyname',
SecurityGroups=[
        'my-securitygroup'
    ]
    )

现在,当我尝试在我的新实例上运行命令时,我的头撞到了墙上。

我正在尝试类似的东西:

ssh_client = sshclient_from_instance(instance,
                                     'C:\\users\\%USER%\\aws\\windows-west-keypair.pem',
                                     user_name='Administrator')

status, stdout, stderr = ssh_client.run('cd')

但我回来了:

C:\Users\%USER%\Miniconda\lib\site-packages\paramiko\hostkeys.pyc in load(self, filename)
     93         :raises IOError: if there was an error reading the file
     94         """
---> 95         with open(filename, 'r') as f:
     96             for lineno, line in enumerate(f, 1):
     97                 line = line.strip()

IOError: [Errno 2] No such file or directory: 'C:\\Users\\%USER%/.ssh/known_hosts'

通过 ssh 连接到我的 aws 实例对我来说是新事物,所以我不希望有目录/文件。

我需要什么?有什么我需要提前安装的吗?究竟出了什么问题?

我感觉如此接近,却又如此遥远!

任何帮助都会很棒。

【问题讨论】:

  • 它是 Windows 实例吗?
  • @error2007s 是的,它是一个 Windows 实例。
  • 您无法使用 pem 文件 SSH 进入 Windows 实例检查此步骤以连接到 AWS Windows EC2 实例docs.aws.amazon.com/opsworks/latest/userguide/…
  • @error2007s 抱歉,问题并不清楚,但我想以编程方式在 ec2 实例上运行脚本。含义 - 创建实例后我想说instance.run_command('python my_python_script.py') - 含义 - 如何以编程方式访问命令行?
  • 我猜你在寡妇中运行脚本的方式有错误检查这个问题stackoverflow.com/questions/1934675/…

标签: python amazon-web-services amazon-ec2 boto paramiko


【解决方案1】:

如 cmets 中所述,您不能直接 ssh 进入 windows。

所以回到您最初的要求:在创建实例后运行脚本我不会从您的 python 脚本中执行此操作,而是在 ec2 实例上使用脚本并让它们自动运行ec2实例启动

您可以拥有将在您的实例上运行的user data script。在 windows 实例上,您可以运行基本的 cmd 脚本或 powershell 脚本

【讨论】:

  • 感谢您的回答。对于面临这种用例的人,他们应该使用 linux 实例,而不是 windows。
  • 肯定会更好!
  • Frederic,你在 linux 实例上使用 phantomjs 吗?
  • 我在没有在 AWS 上运行的 linux box (ubuntu) 上运行 phantomjs,但它们在 VM 上运行
猜你喜欢
  • 1970-01-01
  • 2018-08-30
  • 2017-06-10
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
  • 2019-02-02
相关资源
最近更新 更多