I am creating a droplet like this :
newdroplet=$(curl -X POST “https://api.digitalocean.com/v2/droplets” \
keys”:[’$SSHID’],“userdata”:null}’ \
-H “Authorization: Bearer $DO_TOKEN” \
-H “Content-Type: application/json”)

after extracting some item from the JSON I am using the following to
move a folder and substructure to the droplet:

tar -vczf - * | ssh -o StrictHostKeyChecking=accept-new root@${newdropletip} tar -xzf - -C /opt/

it starts listing files and then stops. 30 percent of the time it announces that the key has been added to known hosts. and proceeds through the rest of the script

the rest of the time it returns
ssh: connect to host IP address here port 22: Connection refused
tar: -: Wrote only 4096 of 10240 bytes
tar: Child returned status 141
tar: Error is not recoverable: exiting now

the ssh takes (15 seconds? ish )to respond, I suspect sometimes it’s longer and times out?

comments and suggestions appreciated

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.

Submit an Answer
1 answer

Hello, @jefftully

Are you experiencing the same ssh-timeouts when you’re connected with an ssh-client like Putty or using your terminal?

I will recommend you to check our existing tutorial on how to troubleshoot the connection refused errors in ssh:


Hope that this helps!

  • My thought is that I am waiting until the status in the JSON reply says active… I have no clue what the person that wrote the API used as criteria for “active”, so I am currently trying to add a loop that checks for ssh is ready. If you look at the GUI when you create a droplet, the IP address is assigned much earlier ( in the scale of script operation ) than the ssh. If I can first document when ssh is ready and available on 22 then I can troubleshoot further if fails

    Testing indicates that my theory is correct
    sshstatus=$(curl $newdroplet_ip:22)
    forces it to wait until ssh sends it an answer, which means its good to go . It counts and then proceeds with the script. need to make it prettier

    • Hello there,

      That is a valid point. The ssh service will be available shortly after the droplet has been created but as you mentioned this will happen after the IP address has been assigned to the droplet as well.

      I will recommend you to either edit the API (if possible) and perhaps add a sleep (wait) or act when the status of the ssh is active like the one you used with curl in the example.