How to avoid multiple ssh -o StrictHostKeychecking=no?

How do i avoid multiple ssh -o StrictHostKeychecking=no in the below script.

if [ "$#" == "0" ]
then
echo " Please mention the server name after the script"
elif ssh -o StrictHostKeychecking=no $1 "[ -d /abc/def/ ]" 2> /dev/null
then
ver=`ssh -o StrictHostKeychecking=no $1 "cat /abc/def/version" 2> /dev/null`
env=`ssh -o StrictHostKeychecking=no $1 "cat /abc/def/environment" 2> /dev/null`
echo "version name for server $1 is $ver"
echo "env name for server $1 is $env"
else
echo " /abc/def not present in the $1"
fi
exit 1

Well, you could run it once and read the responses. Try something like this and see if it gets you nearer to what you are after:-

if [ "$#" == "0" ]
then
   echo " Please mention the server name after the script"
   exit 1
fi

unset dir                        # Ensure we have nothing confusing already set in here

ssh -o StrictHostKeychecking=no $1 \
 "ls -1d /abc/def;
 cat /abc/def/version;
 cat /abc/def/environment" |\
  while read dir
  do
     read ver
     read env
  done

echo "Reference directory ${dir:-not} present on $1"
echo "version name for server $1 is ${ver:-"Not Set"}"
echo "env name for server $1 is ${env:-"Not Set"}"

I hope that this helps,

Robin
Liverpool/Blackburn
UK

1 Like

rbatte1
Registered User

thank you rbatte1.

i did some modification and now the script is looking better than what i posted first :). issue i faced with u r code dir variable is not getting the directory name and also i didn't wanted the last 2 line to be printed if the directory is not available.

1 Like

Well, it's only a suggestion. You can of course modify it as you wish. Glad it helped, and thanks for letting us know.

Robin