0

I need to print a count of a remote server which is written on '/REMOTE_DIR/DR_count'. But that remote server is not much reliable due to a network or OS failure. However I need to print the DR_count value from the local machine if that remote machine is not available. Here is my logic. Please correct me to how to check that if condition in correct way. I'm running this script on Solaris 11.3.

#!/bin/sh

if[check wether ssh user@host_name is possible]
then 

op="cat /REMOTE_DIR/DR_count"
cmd="ssh user@host_name $op"
drlog=`$cmd`

else 

drlog='cat /LOCAL_DIR/DR_count'


fi


echo $drlog
2
  • Why don't you simply try to ssh to the host and check the exit code of the command, to see whether your were successful? Commented Nov 22, 2019 at 7:08
  • @user1934428 please implement a answer Commented Nov 22, 2019 at 7:13

2 Answers 2

3

As I said in my comment, I would simply try to ssh, and if use its exit code to see whether it worked:

ssh -o ConnectTimeout=5 user@host_name cat /REMOTE_DIR/DR_count 2>/dev/null || cat /LOCAL_DIR/DR_count
Sign up to request clarification or add additional context in comments.

Comments

1

you should check for exit code 255 to detect whether you have network error or not

#!/bin/bash

#EXIT STATUS
#     ssh exits with the exit status of the remote command or with 255 if an error occurred.

cnt=`ssh -o ConnectTimeout=5 root@$host "cat /REMOTE_DIR/DR_count"`

exit_code=$?

if [ $exit_code -eq 255 ]; then
    cnt=`cat /LOCAL_DIR/DR_count`
fi

it makes sense also to check for other (non 0 / 255) exit codes to check possible issues at remote side (like file missing on remote side)

Comments

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.