【发布时间】:2014-02-03 01:46:44
【问题描述】:
我正在生成 python 代码来分析 Amazon EC2 提供的各种服务器 AMI 的性能。我目前在尝试 ssh 到我生成的实例时遇到问题。我已经通过他们的网络界面成功地做到了这一点,但不能以编程方式。
下面的代码使用以编程方式生成的安全组和密钥对(保存在本地)生成单个 Red hat AMI。实例运行后,我尝试使用保存的密钥对(在它已被 chmod 400'd 之后)通过 ssh 进入实例,但 ssh 命令冻结不产生任何输出。
代码:
#!/usr/bin/env python
import sys
from boto.ec2 import EC2Connection
#Fill in with your respective keys
awsAccessKey = ""
awsSecretKey = ""
#All AMI instance names from the free tier
#In the EC2 panel, goto "instances" -> "launch instance" -> "free tier"
amiNameArr = ["ami-bba18dd2","ami-a25415cb","ami-e8084981","ami-ad184ac4","ami-7527031c"]
#Lets just use a varying set of AMI's
amiDescArr = ["Amazon Linux","Red Hat Enterprise","SUSE Enterprise",
"Ubuntu Server 13.10","Microsoft Server 2012"]
#AMI Instance types, physical machine types that the AMIs run on; ti.micro only free one
#In order of optimizations: Micro, General, Memory, Storage, Compute
amiInstTypesArr = ["t1.micro",
"m1.small","m1.medium","m1.large","m1.xlarge","m3.medium",
"m2.xlarge","m2.2xlarge","m2.4xlarge",
"hi1.4xlarge","hs1.8xlarge",
"c1.medium","c1.large","c3.large","c3.xlarge","c3.2xlarge"]
if __name__ == "__main__":
from time import gmtime, strftime
sessionStart = strftime("h%Hm%Ms%S", gmtime())
#Connect to amazon AWS
print("\nConnectiong to AWS, start time: " + sessionStart)
awsConn = EC2Connection(awsAccessKey, awsSecretKey)
connParms = awsConn.get_params()
print("Connected with access key id: " + str(connParms['aws_access_key_id']))
#Create a key pair for this session
print("Creating key pair...")
keyPairName = "AWSAnalysisKeyPair" + sessionStart
awsKeyPair = awsConn.create_key_pair(keyPairName)
awsKeyPair.save("~")
print("Saved key pair: " + keyPairName)
#Create a security group for all server instances to use
print("Creating security group...")
securityGroupName = "AWSAnalysisSecurityGroup" + sessionStart
securityGroupDesc = "For access and analysis of programmatically spawned machines"
awsSecGroup = awsConn.create_security_group(securityGroupName, securityGroupDesc)
awsSecGroup.authorize('tcp',22,22,'0.0.0.0/0',awsSecGroup)
awsSecGroup.authorize('tcp',80,80,'0.0.0.0/0',awsSecGroup)
#Start spawning new server instances!
#For each AMI, create all machine instance types we can
print("Spawning instances...")
for amiIndx in range(1, 2): #len(amiNameArr)):
print(" AMI description: " + str(amiDescArr[amiIndx]))
for typeIndx in range(0, 1): #len(amiInstTypesArr)):
print(" starting machine: " + str(amiInstTypesArr[typeIndx]))
awsConn.run_instances(
amiNameArr[amiIndx],
instance_type = amiInstTypesArr[typeIndx],
security_groups = [securityGroupName],
key_name = keyPairName,
max_count = 1
)
#We now want to get information about each machine instance so we can analyze it
#conn.get_all_instances() returns a list of Reservation objects
from pprint import pprint
print("All spawned instance information")
reservations = awsConn.get_all_instances()
instances = [i for r in reservations for i in r.instances]
for i in instances:
#pprint(i.__dict__) #Shows all possible instance info
print("- id: " + str(i.__dict__['id']) + "\n"
" image: " + str(i.__dict__['image_id']) + "\n" +
" type: " + str(i.__dict__['instance_type']) + "\n" +
" state: " + str(i.__dict__['state']) + "\n" )
通过查看在线 EC2 界面,我知道我正在生成一个实例并且它正在运行,并且它具有以编程方式生成的密钥对和与之关联的安全组。鉴于它与这两个相关联,我不得不弄清楚我的问题在于我如何构建密钥对和安全组。
我是否正确构建了安全组和密钥对?还有其他原因导致我无法通过 ssh 访问这些实例吗?
我也知道我正确地尝试使用 ssh 访问机器实例,因为我可以通过从 Web 界面生成实例并通过 ssh 进入它们来成功地做到这一点。
【问题讨论】:
-
您使用哪个用户进行连接。在 RedHat 和 Amazon Linux 上,您必须使用 ec2-user。在 Ubuntu 上,使用 ubuntu
标签: python linux amazon-web-services ssh amazon-ec2