Friday 13 January 2017

Script to run a local script remotely only if remote server is reachable

Today I'd like to share a script with you all which all run a local script on a remote server only if the server is reachable via ping. In linux we can count the number of pings we want to send to the server & we'll get an exit status almost immediately. In Solaris we do not have the option of sending a count & if we wait for the default value of 20 seconds for a ping to time out then we'll end up with a very slow script. The work around for this to change the default time out value. The script I use in this article has a ping timeout value of 2 seconds. The remote execution of the local script is carried out by using "bash -s" in the ssh session.

Here is the script:

#!/usr/bin/bash

################################################################
#Purpose: run automount check script on Linux/Solaris servers  #
#date: 12/01/2017                                              #
################################################################

host_list=${1}

##cleaan error server list##
>/export/home/`whoami`/ping_error.txt
>/export/home/`whoami`/automount_hung.txt

##check that server list exists##

if [ $# != 1 ] ; then
        echo "script usage: $0 <server list>"
        exit
fi

for name in `cat $host_list`
do

##check that host is reachable##

ping ${name} 2 &> /dev/null

if [ $? -eq 0 ] ; then
        OS_TYPE=$(ssh -o StrictHostKeyChecking=no -q ${name} 'uname -s')



        if [ $OS_TYPE == "Linux" ] ; then

                ##check if automount is hung##
                AUTO_HUNG="ps -eLo pid,pgrp,lwp,comm,wchan|grep autofs4_wait | grep -v automount"
                ssh ${name} ${AUTO_HUNG} >> /dev/null
                        if [ $? -eq 1 ] ; then
                                ssh -o StrictHostKeyChecking=no -q ${name}  "bash -s" < /export/home/ssuri/automount_check.sh
                        else
                                echo "autoFS service is hung on server ${name}"
                                echo "autoFS service is hung on server ${name}" >>  /export/home/`whoami`/automount_hung.txt
                        fi

        else
                ssh -o StrictHostKeyChecking=no -q ${name} "bash -s" < /export/home/ssuri/automount_check_solaris.sh
        fi
else
        echo "could not connect to server ${name}"
        echo "could not connect to server ${name}" >> /export/home/`whoami`/ping_error.txt
fi

echo -e "------------------------------- \n"

done

echo -e "------------------------------- \n\n"
echo "list of unreachable servers /export/home/`whoami`/ping_error.txt"
echo "list of servers where automount was hung is /export/home/`whoami`/automount_hung.txt"


The script being run remotely automount_check, checks for NFS and auto mount file systems to make sure that they are & can be mounted correctly & in read/write mode. Here is the script:

#!/bin/bash

##############################################
#Purpose: check for automount file systmes.  #
#date: 12/01/2017                            #
##############################################

##echo color codes##
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'


echo -e  "$YELLOW Checking automount file systems on server $(hostname) $NC"

##get automount map file names##

cat /etc/auto.master| grep ^/ | egrep -v '/etc/auto.common|/etc/auto.unix|/etc/auto.ots|/etc/auto.export_home|/etc/auto.misc' | awk '{print $2}' >> /tmp/map_list


for i in `cat /tmp/map_list`; do cat $i | awk '{print $1}'; done >> /tmp/mount_list
cat /etc/fstab | grep nfs | awk '{print $2}' >> /tmp/mount_list
#cat /etc/vfstab | grep nfs | awk '{print $3}' >> /tmp/mount_list

##loop through autmount maps and nfs file systems in fstab file ##

for fs_name in `cat /tmp/mount_list`
do

#FS_name=$(cat $map_name | awk '{print $1}')

echo "checking file system: $fs_name"
cd $fs_name

##make sure autoFS mounts as nfs or cifs##

FS_type=$(df -hT . | awk 'NR==3 {print $1}')

if [ $FS_type == "nfs" ] || [ $FS_type == "cifs" ] ; then
        echo -e $GREEN"$fs_name is of type $FS_type and is  mounted correctly$NC"
else
        echo -e "$RED $fs_name is mounted correctly on $(hostname). Please check $NC"
fi

##check that FS is writable##

sudo touch testfile
if [ $? -eq 0 ] ; then
        echo -e "$GREEN $fs_name mounted on $(hostname) is writable $NC"
else
        echo -e "$RED $fs_name is not writable on $(hostname). Please check $NC"
fi
sudo rm testfile

cd

done

rm /tmp/map_list
rm /tmp/mount_list


I wrote it for doing some post checks after a network activity involving NAS shares just to make sure that everything was normal after the completion of the activity.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...