0

Below you can see my bash script

#createSession.sh

echo "Welcome to ADS2 Session Start..."

while [ true ]
do
echo "Starting service Daemon on the targert"
echo "Enter the ip-address of the target"
read x
if [ -z "$x" ]
then 
    echo "ip-address is empty"
else
    echo "Connecting with target..."
    if [[ $x =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]
    then
        ssh nvidia@"$x" "/opt/ads2/arm-linux64/bin/ads2 svcd&"
        echo "connection succeded"
        break
    else
        "Make sure the io address in the coorect form"  
    fi

fi
done

First question

As you see, the script checks if the user input machtes the ip-address form. However, i would like the script to also accept the ip address as a string which has this form fdt-c-agx-4arbitrary numbers. The last part of the string can be any arbitrary 4 numbers. As a result, the if statement be like

if [[ $x =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] || [ $x = #machtes fdt-c-agx-4arbitrary digits]

Second question

The ip address could be entered in the right form, however does not exist. Consequently, the ssh returns in this case ssh connection time out. However the string connection succeded will be printed which is not true. So how can i make the scripts enough clever to realize that the connection in this case is not established?

Thnaks in advance

3
  • As a result, the if statement be like Soo... write it that way? You have ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ as an example, so just write the same that matches the other one. Commented Mar 19, 2021 at 9:42
  • So what do you think [0-9] means? I recommend regex crosswords, available on the net, to learn regex in no time. Commented Mar 19, 2021 at 9:44
  • @KamilCuk your write. numbers between 0 and 9 Commented Mar 19, 2021 at 9:44

1 Answer 1

2

how can i make the scripts enough clever to realize that the connection in this case is not established?

Just check the ssh exit status.

if ! ssh something@something something ; then
     echo "Och nuuu - problem with ssh" >&2
     exit 1
else
     echo "Yes! It worked! Good job!"
fi

As a result, the if statement be like

Sure:

if 
   [[ $x =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] ||
   [[ $x =~ ^fdt-c-agx-[0-9][0-9][0-9][0-9]$ ]]
then
   ...
while [ true ]

Just:

while true

The [ command checks if the string true has nonzero length. Because it has, it's a success. You could write [ anything ] with same result. But just true, it's a command on it's own.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the detailed answer. However, the OR operator seems not to work. I tried every condition independently and it works. However, its more efficient with or. Am I using the or operator wrongly?
And for listening to the ssh, when the ip address is right, it asks again for the password like if its calling the connection again
I solved the problem with OR operator. However the if statement regarding listening to ssh exit status prompts the user to enter the password again
if statement regarding listening to ssh exit status prompts the user to enter the password again so? That's good - you should authenticate yourself before connecting to remote server. If you do not want to give password, research public key authentication ie. ssh-copy-id. If you want your credentials to be cached, research ssh-agent and maybe ControlMaster option to ssh.
that's right. However it already asks after checking the ip address and executes the command remotely. Why would it ask again when i observe the ssh exit ?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.