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!
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?
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.