Question

create droplet ssh not connecting

Posted November 1, 2021 91 views
APIDeployment

I have a script that starts a droplet and runs through till everything is installed.

I am checking for a response on SSH to decide if the server is ready to start loading software.


new_droplet=$(curl -X POST "https://api.digitalocean.com/v2/droplets" \
    -d'{"name":"jeffsproject","region":"nyc3","size":"s-1vcpu-2gb","image":"ubuntu-18-04-x64","ssh_keys":['$SSH_ID'],"user_data":null}' \
    -H "Authorization: Bearer $DO_TOKEN" \
    -H "Content-Type: application/json")

echo " Create new droplet"
set -xv
new_droplet_id=$(echo $new_droplet | jq .droplet.id)
while [[ ${new_droplet_id} == "null" ]] ; do
    sleep 10
    new_droplet_id=$(echo $new_droplet | jq .droplet.id)
    echo "droplet failed"
done
echo $new_droplet_id | jq .


while [[ ${droplet_status} != "active" ]] ; do
    sleep 10
    new_droplet_details=$(curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $DO_TOKEN" "https://api.digitalocean.com/v2/droplets/${new_droplet_id}")

    new_droplet_ip=$(echo $new_droplet_details | jq -r .droplet.networks.v4[1].ip_address)
    droplet_status=$(echo $new_droplet_details | jq -r .droplet.status)
    export DROPLET_IP=$new_droplet_ip
done

while [[ $ssh_ready != 0 ]] ;
do
    sleep 5
    ssh_ready=$(ssh -vvv -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@$new_droplet_ip echo '0')
    echo "Trying again..."
    echo $ssh_ready
done

 its  pretty basic  stuff.   My  token and  ssh  ID are  in my environment
++ ssh -vvv -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@10.132.1.224 echo 0
OpenSSH_7.6p1 Ubuntu-4ubuntu0.5, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/jeff/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "10.132.1.224" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 10.132.1.224 [10.132.1.224] port 22.
debug2: fd 3 setting O_NONBLOCK
debug1: connect to address 10.132.1.224 port 22: Connection timed out
ssh: connect to host 10.132.1.224 port 22: Connection timed out
+ ssh_ready=
+ echo 'Trying again...'
Trying again...
+ echo

+ [[ '' != 0 ]]
+ sleep 5
++ ssh -vvv -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@10.132.1.224 echo 0
OpenSSH_7.6p1 Ubuntu-4ubuntu0.5, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/jeff/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "10.132.1.224" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 10.132.1.224 [10.132.1.224] port 22.
debug2: fd 3 setting O_NONBLOCK


I have also  tried  to SSH  in as  root and  it times out.  

any ideas  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
2 answers

no idea who or when it happened but newdropletip=$(echo $newdropletdetails | jq -r .droplet.networks.v4[1].ipaddress)
droplet
status=$(echo $newdropletdetails | jq -r .droplet.status) that index for the IP address should be [0] not [1]

Hi @jefftully,

So essentially the script checks your Droplet and if it can SSH to it. It seems like there aren’t issues with the script so what I would suggest you doing is try to execute the parts of the script manually which are essential.

What do I mean, you mentioned the following is giving 0 rather than 1 :

    new_droplet_details=$(curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $DO_TOKEN" "https://api.digitalocean.com/v2/droplets/${new_droplet_id}")

    new_droplet_ip=$(echo $new_droplet_details | jq -r .droplet.networks.v4[1].ip_address)
    droplet_status=$(echo $new_droplet_details | jq -r .droplet.status)
    export DROPLET_IP=$new_droplet_ip

In this case, try to execute it manually, try it command after command and see what happens.

The same goes for the SSH command, see if you are able to SSH using only the command. If yu are able to, it would mean you have a problem with your Script if you are unable to, then you can check the droplet.