我试图通过SSH连接到Amazon EC2实例,使用boto
。我知道ssh连接可以在实例创建一段时间后建立。所以我的问题是:如何检查是否与AWS实例建立SSH连接
- 我能以某种方式检查SSH是否在实例上? (如果是这样,怎么办?)
- 或者我该如何检查
boto.manage.cmdshell.sshclient_from_instance()
的输出?例如,如果输出打印出Could not establish SSH connection
,则比再试一次。
这就是我想,到目前为止,但没有运气:
if instance.state == 'running':
retry = True
while retry:
try:
print 'Connecting to ssh'
key_path = os.path.join(os.path.expanduser('~/.ssh'), 'secret_key.pem')
cmd = boto.manage.cmdshell.sshclient_from_instance(instance,
key_path,
user_name='ec2-user')
print instance.update()
if cmd:
retry = False
except:
print 'Going to sleep'
time.sleep(10)
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
当然,一切工作正常,因为我可以在一段时间后推出相同的代码,并会得到一个连接,将能够使用cmd.shell()
谢谢你,这是很聪明的。 – Vor 2013-07-15 14:32:43