jeudi 30 avril 2015

if statement, SSH and executing remote commands

I'm writing some code that currently SSH to several servers in turn and remotely tests to see if a process is running. The end goal is for me to receive an email alert if the process is not running. I have that much working.

However, I also want a warning if the SSH connection fails. Trouble is, no matter what I try, I cannot get it to work with my current code of if, the, else.

#!/bin/sh
filename="/root/scripts/sysmon_server_lists.txt"
identity="/root/.ssh/sysmon"
OLDIFS=$IFS
IFS=,

while read -r server searchtxt;
do
        if [ ! -z $server ]
        then
                if [ `ssh -n -q -t -i $identity root@$server "ps -ef | grep $searchtxt | grep -v grep | wc -l"` -gt 0 ]
                        then
                                echo "Process is running on $server."
                        else
                                echo "Process is not running on $server."
                        fi

                echo "Server is $server"
                echo "Searchtext is $searchtxt"
                echo " "
                echo "-----"
                echo " "
        fi
        done < $filename

IFS=$OLDIFS
echo "Finished"

exit 0

Aucun commentaire:

Enregistrer un commentaire