【问题标题】:Can't get amazon cmd shell to work through boto无法让亚马逊 cmd shell 通过 boto 工作
【发布时间】:2013-02-28 12:16:09
【问题描述】:

我正在尝试创建一个实例并使用 ssh 登录到服务器,我正在按照书中的示例进行操作:

import os
import time
import boto
import boto.manage.cmdshell

def launch_instance(ami="ami-54cf5c3d",
                    instance_type="t1.micro",
                    key_name="paws",
                    key_extension=".pem",
                    key_dir="~/.ssh",
                    group_name="paws",
                    ssh_port="22",
                    cidr="0.0.0.0/0",
                    tag="paws",
                    user_data=None,
                    cmd_shell=True,
                    login_user="ec2-user",
                    ssh_passwd=None):

    cmd=None
    ec2 = boto.connect_ec2() # Crededentials are stored in /etc/boto.cfg

    try:
        ec2.get_all_key_pairs(keynames=[key_name])[0]
    except ec2.ResponseError, e:
        if e.code == 'InvalidKeyPair.NotFound':
            print 'Creating keypair %s' % key_name
            key = ec2.create_key_pair(key_name)
            key.save(key_dir)
        else:
            raise

    try:
        group = ec2.get_all_security_groups(groupnames=[group_name])[0]
    except ec2.ResponseError, e:
        if e.code == 'InvalidGroup.NotFound':
            print 'Creating security group %s' % group_name
            group = ec2.create_security_group(group_name,
                                              'A group that allows SSH access')
        else:
            raise

    try:
        group.authorize('tcp',ssh_port,ssh_port,cidr)
    except ec2.ResponseError, e:
        if e.code == 'InvalidPermission.Duplicate':
            print 'Security group %s already authorized' % group_name
        else:
            raise

    reservation = ec2.run_instances(ami,
                                    key_name=key_name,
                                    security_groups=[group_name],
                                    instance_type=instance_type,
                                    user_data=user_data)
    instance = reservation.instances[0]

    print 'waiting for instance...'
    while instance.state != 'running':
        time.sleep(5)
        instance.update()
    print 'Instance is now running' 
    print 'Instance IP is %s' % instance.ip_address

    instance.add_tag(tag)

    if cmd_shell:
        key_path = os.path.join(os.path.expanduser(key_dir),
                                key_name + key_extension)
        cmd = boto.manage.cmdshell.sshclient_from_instance(instance,
                                                           key_path,
                                                           user_name=login_user)

    return (instance, cmd)


launch_instance()  

这就是我得到的输出:

root@johntheripper-PORTEGE-Z835:~/boto# python ec2_launch_test.py 
Security group paws already authorized
waiting for instance...
Instance is now running
SSH Connection refused, will retry in 5 seconds
SSH Connection refused, will retry in 5 seconds
SSH Connection refused, will retry in 5 seconds
SSH Connection refused, will retry in 5 seconds
SSH Connection refused, will retry in 5 seconds
Could not establish SSH connection

正如您从最后一行看到的那样,出了点问题,我正在考虑权限,但即使我以 root 身份运行它,也没有任何变化。

但是我可以通过ssh -i ........ ec2-user@..... 连接到这个实例 你能指出我做错了什么吗?

【问题讨论】:

  • 我遇到了这个问题:有时 ssh 服务器需要一些时间才能启动......让我挖掘一下我的代码,看看我是否能找到我的解决方案
  • 好点,我去看看
  • 我想我刚刚结束了使用 time.sleep :( reference
  • 太棒了,它现在可以工作了,如果你能把它作为答案会很好,所以我可以结束这个问题。再次感谢

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


【解决方案1】:

我认为更好的解决方案是实际轮询实例以检查 ssh 服务器是否准备好,然后继续。

def poll_ssh(reservation): 
    # check if it is ready for ssh
    print '\nConnecting to ssh'
    for instance in reservation.instances:
        retry = True
        while retry:
            try:
                key_path = os.path.join(os.path.expanduser(KEY_DIR), KEY_NAME)
                key_path += '.pem'
                cmd = boto.manage.cmdshell.sshclient_from_instance(instance, key_path, user_name='ubuntu')
                #print 'debug: %s' % cmd
                if cmd:
                    retry = False #breaks the while loop once cmdshell returns true
                    cmd = None
                    print 'Connected to', instance
            except:
                time.sleep(5) # Let the sshd start up
        print 'Done\n'

【讨论】:

    【解决方案2】:

    即使实例“正在运行”,有时 ssh 服务器也需要一段时间才能启动。

    输入time.sleep(30),然后尝试使用 ssh 进行连接,它应该可以工作。

    Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-08
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多