Report this

What is the reason for this report?

How do I wait for a new droplet to be ready for ssh using the API (DropletKit)?

Posted on January 28, 2017
wiq

By wiq

I’m currently using the V2 API to spin up new droplets based on my own custom snapshot. When I run the droplets.create call via DropletKit it returns relatively quickly with the public ip.

The next step is for me to ssh into the machine and kick off a bash script. Unfortunately, I’m seeing ssh timeouts because the droplet is still powering on, which is causing my scripts to fail.

If I need to wait for a droplet to become accessible via SSH, what’s the recommended way of doing this?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Sleep for 60 seconds, then use a while loop to check whether SSH responds every 5 seconds or so.

@Woet yes that works (I’m currently using net-ssh with a timeout that does that). I suppose my question is more… does the API give any information about it’s connectivity status? Or is polling the go to strategy here?

@wiq

The API doesn’t provide exact information on whether a specific service is up, running, or accessible though you can check the status a few different ways instead of simply trying to connect until it’s up.

If you use nmap, you can check for return status using something such as:

nmap DROPLET_IP -PN -p ssh | egrep 'open|closed|filtered' | awk '{print $2}'

When SSH isn’t open/available it’ll return either closed or filtered and, when open and able to accept connection, it’ll return open.

You would then run your while loop until open is returned. Using this method, you shouldn’t need to worry about SSH Keys or passwords as it’s simply checking the port itself, not actually connecting to it.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.