Need help with multiple "if" for ssh

Hello Folks,

Good day,

I have a little script here where i connect to my servers using below.

# s <hostname>

Below is the sample script i had written to simplify this step successfully.

jump1 ~ $ s abc
Last login: Wed Oct 25 11:23:28 2017 from jump1
[root@abc ~]#

However i did not get the expected output as below when the script cannot find the hostname with both .XX.com and XXX.com.

    echo "Could not resolved $server, please check hostname"

Can you assist to check what went wrong here?

Thank you so much.

#!/bin/bash
#

hostname=$1

server=`echo $hostname | tr '[A-Z]' '[a-z]'`

ping -q -c 1 $server.XX.com &>/dev/null

if [ "$?" = 0 ];
then
        ssh -q -o StrictHostKeyChecking=no -l root $server.XX.com
else
        if [ "$?" -ne "0" ];
        then
                ssh -q -o StrictHostKeyChecking=no -l root $server.XXX.com
        else
        echo "Could not resolved $server, please check hostname"
        fi
fi


Looks like a problem with the resolver. Does the host server exist and ping ? What be the output of

nslookup "$hostname"
nslookup "$server"

What is the contents of /etc/resolv.conf ?

On the other hand, you don't seem to use the if consistently; once you check an integer value, the other is checking a string value. And, as $? is overwritten with every command executed, assign it to a variable before any evaluation and then use the latter.