简体   繁体   English

Python piramiko,建立SSH连接时连接超时错误

[英]Python piramiko, Connection Timed Out Error While Establishing SSH Connection

I am working on a project in Python that will create an Amazon ec2 instance , and establish a SSH and SFTP connection to transfer files and commands between my machine and ec2 instance. 我正在使用Python创建一个项目,该项目将创建一个Amazon ec2实例 ,并建立SSH和SFTP连接以在本机和ec2实例之间传输文件和命令。

So I began to code, I coded the function that creates an ec2 instance using boto3 library. 因此,我开始编写代码,并编写了使用boto3库创建ec2实例的函数。

# creating a file named sefa.pem that will store the private key
outfile = open('sefa.pem', 'w')
keypair = ec2.meta.client.create_key_pair(KeyName='sefakeypair')  # creates key pair
keyout= str(keypair['KeyMaterial'])  # reads the key material
outfile.write(keyout)  # writes the key material in sefa.pem

# creates the instance finally
response = ec2.create_instances(ImageId='ami-34913254', MinCount=1, MaxCount=1, InstanceType='t2.micro')

After that, I should establish a SSH Connection between my machine and ec2 instance to send command and I also should transfer and bring back files between my machine and ec2 instance. 之后,我应该在机器和ec2实例之间建立SSH连接以发送命令,并且还应该在机器和ec2实例之间传输并取回文件。

After research, I found out that there is a Python library called piramiko for establishing SSH Connection and SFTP Connection between my computer and ec2 instance. 经过研究,我发现有一个名为piramiko的Python库,用于在我的计算机和ec2实例之间建立SSH连接和SFTP连接

I tried to establish a SSH Connection between my computer and ec2 instance, but I have been facing with the "[Errrno 110]Connection Timed Out Error" for a day. 我试图在我的计算机和ec2实例之间建立SSH连接,但是一天都遇到“ [Errrno 110]连接超时错误” I have been searching the internet for hours, but I couldn't find anything useful. 我已经在互联网上搜索了几个小时,但是找不到任何有用的信息。 Here is the code that emerges "Connection Time Out Error": 这是出现“连接超时错误”的代码:

    con = paramiko.SSHClient()  # ssh client using paramiko library
    con.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # this is needed because of adding policy automautically
    k = paramiko.RSAKey.from_private_key_file("sefa.pem")  # k reads sefa.pem and stores private key
    time.sleep(30)  # added this because ec2 should do 2/2 checks  before connecting
    print("connecting")
    con.connect(hostname=PUB_DNS, username="ubuntu", pkey=k, look_for_keys=True)  # HERE IS THE ERROR, I CAN'T CONNECT
    print("connected")
    stdin, stdout, stderr = con.exec_command('echo "TEST"')
    print(stdout.readlines())
    con.close()

I can not go any further without establishing a connection between my machine and ec2 instance. 没有建立我的机器和ec2实例之间的连接,我就走得更远。

  • Do you have any suggestions to solve this problem? 您对解决这个问题有什么建议吗?
  • Is there any alternative library to piramiko? piramiko是否有其他库?

I managed to solve the problem. 我设法解决了这个问题。 The problem is my ec2 instance. 问题是我的ec2实例。 These solved the issue: 这些解决了问题:

  • Make sure that instance's security group has ssh deamon and allows you to connect. 确保实例的安全组具有ssh守护程序并允许您连接。
  • Make sure that you have the keypair that you created while creating the instance. 确保具有创建实例时创建的密钥对。
  • Make sure that you execute chmod 400 keypair.pem 确保执行chmod 400 keypair.pem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM