无法获得亚马逊CMD壳工作,通过博托 [英] Can't get amazon cmd shell to work through boto

查看:179
本文介绍了无法获得亚马逊CMD壳工作,通过博托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个实例,并使用ssh登录到服务器,我下面一个例子来自一本书:

I'm trying to create an instance and login to the server using ssh, I'm following an example from a book:

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()  

这就是我得到的输出:

and that's the output I'm getting:

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

正如您从最后一行看到,不对的地方,我在想权限,但即使我运行它作为一个根,没有什么变化。

As you can see from the last line, that something wrong, I was thinking about permission, but even if I run it as a root, nothing changes.

不过,我能连接到该实例通过的ssh -i ........ EC2用户@ ..... 能否请你指出我什么,我做错了什么?

However I'm able to connect to this instance through ssh -i ........ ec2-user@..... Can you please point me what I'm doing wrong?

推荐答案

有时需要一段时间的SSH服务器来启动,即使实例运行。

Sometimes it takes awhile for the ssh server to start up even though the instance is "running".

再加上一个 time.sleep(30),然后尝试使用ssh连接,它应该工作。

Throw in a time.sleep(30) and then try to connect using ssh and it should work.

<一个href="http://stackoverflow.com/questions/6025546/issues-trying-to-ssh-into-a-fresh-ec2-instance-with-paramiko">Reference

这篇关于无法获得亚马逊CMD壳工作,通过博托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆