Automating ssh connection

I want to create a script for the ssh connection that handles any input. Be it IP or DNS.
And instead of typing in giant commands, I'd like to downplay it and treat it.

I imagined the following command:

i machine
or
i machine.exemple.com.fr
or
i 111.222.333.4444

Login attempts should be made with devops, or with matheus.

I do not know much about shell script I'm reading a few hours and working on the script below.
Can you help me?
Thank you.

> /bin/i && chmod +x /bin/i && vi /bin/i
bash /bin/i

#!/bin/bash

if [ "$1" == "" ]
then
clear
echo "how to use:"
echo "i 111.222.333.444"
echo "i name"
echo "i name.exemple.com.fr"
echo ""
fi

#verifying that the input is a valid ip
if [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
      clear
      echo "Success! Accessing $1 with devops"
      ssh devops@$1
 elif
      clear
      echo "Oops! Could not access with devops, trying to access as matheus"
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1
else
      clear
      echo "Could not connect! Here's some information to access the provider platform."
      whois $1 | grep -E "mnt-by|OrgName|nserver"
      getent hosts $1
fi

#-f If you have text
if [ -f $1 ]; then
      clear
      echo "Success! Accessing $1 with devops"
      ssh devops@$1.exemple.com.fr
  elif
      clear
      echo "[1] Trying to connect..."
      ssh devops@$1
  elif
      clear
      echo "Oops! Could not access with devops, trying to access as matheus"
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1.exemple.com.fr
  elif
      clear
      echo "[2] Trying to connect..."
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1
else
      echo "Could not connect! Here's some information to access the provider platform."
      whois $1 | grep -E "mnt-by|OrgName|nserver"
      getent hosts $1
fi

Have you created ssh keys and placed them in your user's home directory in a new directory called .ssh You then have to place your public in the .ssh directory of the remote user.

DO NOT use root to connect. Bad idea security-wise. Plus you have to modify the sshd.conf file on any box you want to ssh into -- using root. By default ssh (openssh version 2) will not let root log in.

Also I don't understand what you are doing - looks like you are trying to subvert or get around security which is also another bad idea.

1 Like

I have my key configured correctly.
I just want to handle these cases so I can connect, and if it is not possible, get the information from the provider so I can access the platform and find out what happened to the machine and maybe open a ticket at the technical support.
Thank you

Me too, I don't really understand your purposes here. Why don't you let the resolver handle IPs and names for you? Plus, you have logical inconsistencies in your script, e.g. testing for correct IP structure and, if failed, connect as root? Plus, ssh by default uses a user's public key file - no need to specify it unless taken from somewhere else.

1 Like

It would be safer to connect to a personal account and then use sudo or plain su to elevate your privileges once there. Can you explain a bit more about what you are really trying to achieve and why you need to get connected this way?

Just for clarity, with the keys in place that you already have set up, can you connect using the command line without putting in a passphrase?

Kind regards,
Robin

1 Like
#!/bin/bash

if [ "$1" == "" ]
then
clear
echo "how to use:"
echo "i 111.222.333.444"
echo "i name"
echo "i name.exemple.com.fr"
echo ""
fi

#verifying that the input is a valid ip
if [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
      clear
      echo "Success! Accessing $1 with devops"
      ssh devops@$1
 elif
      clear
      echo "Oops! Could not access with devops, trying to access as matheus"
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1
else
      clear
      echo "Could not connect! Here's some information to access the provider platform."
      whois $1 | grep -E "mnt-by|OrgName|nserver"
      getent hosts $1
fi

#-f If you have text
if [ -f $1 ]; then
      clear
      echo "Success! Accessing $1 with devops"
      ssh devops@$1.exemple.com.fr
  elif
      clear
      echo "[1] Trying to connect..."
      ssh devops@$1
  elif
      clear
      echo "Oops! Could not access with devops, trying to access as matheus"
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1.exemple.com.fr
  elif
      clear
      echo "[2] Trying to connect..."
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1
else
      echo "Could not connect! Here's some information to access the provider platform."
      whois $1 | grep -E "mnt-by|OrgName|nserver"
      getent hosts $1
fi