试图SSH与的paramiko一个新的EC2实例问题 [英] Issues trying to SSH into a fresh EC2 instance with Paramiko

查看:226
本文介绍了试图SSH与的paramiko一个新的EC2实例问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作是旋转了一个新的EC2实例与伯特和使用的paramiko SSH客户端的情况下执行远程命令的脚本。无论出于何种原因,该客户端的paramiko是unabled连接,我得到的错误:

 回溯(最近通话最后一个):
  文件脚本/ sconfigure.py,第29行,与<模块>
    ssh.connect(instance.ip_address,用户名='Ubuntu的',key_filename = os.path.expanduser('的〜/ .ssh /测试))
  文件建立/ bdist.macosx-10.3-脂肪/蛋/的paramiko / client.py,291线,在连接
  文件<字符串>中,1号线,在连接
socket.error:[错误61]连接被拒绝
 

我可以在精细手工使用相同的密钥文件,用户SSH。有没有人遇到使用的paramiko问题?我满code是如下。谢谢你。

 进口boto.ec2,时间的paramiko,OS
#连接到美西1区
EC2 = boto.ec2.regions()[3] .connect()
image_id ='AMI-ad7e2ee8
IMAGE_NAME ='的Ubuntu 10.10(小牛狐獴)32位EBS
new_reservation = ec2.run_instances(
    image_id = image_id,
    KEY_NAME ='测试',
    security_groups = ['网络'])

例如= new_reservation.instances [0]

打印旋转起来实例'%s'的 - %s的等待它启动起来。 %(image_id,IMAGE_NAME)
!而instance.state ='运行':
    打印 。
    time.sleep(1)
    instance.update()

打印实例运行,IP:%s的%instance.ip_address

打印连接到%s的用户%的%(instance.ip_address,Ubuntu的)
SSH = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(instance.ip_address,用户名='Ubuntu的',key_filename = os.path.expanduser('的〜/ .ssh /测试))
标准输入,标准输出,标准错误= ssh.exec_command('回声TEST)
打印stdout.readlines()
ssh.close()
 

解决方案

我似乎已经想通了这一点,通过试验和错误。尽管该实例的状态是根据博托跑,有一个当它实际上将允许SSH连接延迟。在ssh.connect(......)之前加入了time.sleep(30),似乎这样的伎俩对我来说,虽然这可能会有所不同。

I'm working on a script that spins up a fresh EC2 instance with boto and uses the Paramiko SSH client to execute remote commands on the instance. For whatever reason, the Paramiko client is unabled to connect, I get the error:

Traceback (most recent call last):
  File "scripts/sconfigure.py", line 29, in <module>
    ssh.connect(instance.ip_address, username='ubuntu', key_filename=os.path.expanduser('~/.ssh/test'))
  File "build/bdist.macosx-10.3-fat/egg/paramiko/client.py", line 291, in connect
  File "<string>", line 1, in connect
socket.error: [Errno 61] Connection refused

I can ssh in fine manually using the same key file and user. Has anyone run into issues using Paramiko? My full code is below. Thanks.

import boto.ec2, time, paramiko, os
# Connect to the us-west-1 region
ec2 = boto.ec2.regions()[3].connect()
image_id = 'ami-ad7e2ee8'
image_name = 'Ubuntu 10.10 (Maverick Meerkat) 32-bit EBS'
new_reservation = ec2.run_instances(
    image_id=image_id,
    key_name='test',
    security_groups=['web'])

instance = new_reservation.instances[0]

print "Spinning up instance for '%s' - %s. Waiting for it to boot up." % (image_id, image_name)
while instance.state != 'running':
    print "."
    time.sleep(1)
    instance.update()

print "Instance is running, ip: %s" % instance.ip_address

print "Connecting to %s as user %s" % (instance.ip_address, 'ubuntu')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(instance.ip_address, username='ubuntu', key_filename=os.path.expanduser('~/.ssh/test'))
stdin, stdout, stderr = ssh.exec_command('echo "TEST"')
print stdout.readlines()
ssh.close()

解决方案

I seem to have figured this out by trial and error. Even though the instance status is "running" according to boto, there is a delay for when it will actually allow an SSH connection. Adding a "time.sleep(30)" before the "ssh.connect(...)" seems to do the trick for me, though this may vary.

这篇关于试图SSH与的paramiko一个新的EC2实例问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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