0

I have 2 servers. The first server is always active and the second server is always in the stanby state. I'm trying to write a bash script to check the status of these two servers before doing the task. If the first host dies (or the connect SSH to server 1 not OK) then it will connect SSH to the second host for execution to task.

Any idea on how to do it?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
donald.sys
  • 301
  • 2
  • 14

1 Answers1

0

EDIT2: since OP mentioned that there is a file named IP.list from which server names should be read then following may help you here.

while read server
do
  if [[ -n $(ssh user_name@"$server" "ls -ld /tmp") ]]
  then
      echo "$server is up."
      echo "Exiting from script now.."
      exit;
  else
      echo "$server is NOT up, look for another server."
  fi
done < "IP.txt"

EDIT: Since OP is saying ping is not an option to check server's availability then using ssh only for servers. Considering that your source server(from where you are running the script) and target server(whose status you are checking are having passwordless connection between them so that you will NOT be prompted for any password)

if [[ -n $(ssh user_name@server_name "ls -ld /tmp") ]]
then
    echo "Server 1 is up."
else
    echo "Server is NOT up, look for another server."
fi

You could simple do ping to SERVER1 and see if that is happening successfully in case it not it means server is not up and you could move your things to another stand up server.

if [[ $(ping -c5 SERVER1 | grep -q "5 packets transmitted, 5 received") -eq 0 ]]
then
    echo "Server1 is up."
else
    echo "Server is NOT up, look for another server."
fi
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Because security system, all my servers are deny ping. Do you have a ideas diffirent? – donald.sys Jun 25 '18 at 08:16
  • @donald.sys, please check my edit now and let me know then? – RavinderSingh13 Jun 25 '18 at 08:29
  • `[[ $(ssh user_name@server1 exit 10) -eq 10 ]]` is unlikely to ever be true. You’re comparing the *output* of `ssh`, not its exit code. – Biffen Jun 25 '18 at 08:31
  • @Biffen, cool, changed it now and tested too, in case of errors it shows errors. – RavinderSingh13 Jun 25 '18 at 09:13
  • @RavinderSingh13: Yes. But it was not what I expected. I have a IP.txt file with the contents is 192.168.56.101 and 192.168.56.102. I want to check ssh the connect to two hosts and execution with cases: **+ If connect SSH to 192.168.56.101 is success then it's will execution run script.sh file.** **+ If connect SSH to 192.168.56.101 is fail, then it's execution the connect SSH to 192.168.56.102 to run script.sh.** – donald.sys Jun 25 '18 at 09:36
  • @RavinderSingh13 That’ll work, but it’s unnecessarily complex (and who wants to see the contents (OK, not *contents*) of `/tmp` each time?). Why not just replace the whole `[[ … ]]` with `ssh user_name@server_name true`? – Biffen Jun 25 '18 at 09:41
  • @Biffen, when I tested it it is not showing me the content of `/tmp`(since I am using `ls -ld`) + when I tried simply doing ssh then it never came back out of that server's session so only I thought to execute a command for ssh and see its status then. – RavinderSingh13 Jun 25 '18 at 09:43