Logo Questions Linux Laravel Mysql Ubuntu Git Menu

stuck with status check of amazon ec2 instance

I am using Python to launch an ec2 instance, after I get "running" state of my instance, I am trying to SCP a shell script and run it via ssh.

I am getting the following error

"ssh: connect to host ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com port 22: Connection refused"

When I check in the console, the Status check is "Initializing", once It changes "2/2 checks passed", I am able to ssh or run any script.

Is there any way I can get the "status check" via python boto API?

I am using Python 2.7.5+, boto 2.19.0

Thanks in advance.

like image 339
Saravanakumar Karunanithi Avatar asked Mar 22 '23 05:03

Saravanakumar Karunanithi

1 Answers

Simple way is to check the port 22 of the newly created instance is reachable or not by using socket module

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.connect(('hostname', 22))
     print "Port 22 reachable"
except socket.error as e:
     print "Error on connect: %s" % e

When you will able to reach the port 22 then you can invoke ssh to it.

like image 156
linuxnewbee Avatar answered Mar 27 '23 21:03