Link to home
Start Free TrialLog in
Avatar of bravo2nothing
bravo2nothing

asked on

A script to ping and then login via SSH, then drop the connection. With logging and sendmail

Thanks for the previous script but is it possible to add multiple servers and send a sendmail message upon failure?
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Sure.
Assuming that you have a file containing your hostnames called hostlist and that the same userid is capable of logging in to all servers -
#!/bin/ksh
USER=myuser
for SERVER in $(cat hostlist)
do
  ping -c1 -w1 $SERVER >/dev/null 2>&1
  RC=$?
   if [ $RC -eq 0 ] ; then
    # "Ping succeeded on $SERVER. Now trying ssh!"
    ssh $USER@$SERVER uname -a >/dev/null 2>&1
    RC=$?
     if [ $RC -eq 0 ] ; then :
      #  "ssh succeeded on $SERVER"
      else
       echo "ssh failed on $SERVER but PING succeeded" | mailx -s "SSH Failure on $SERVER!" recipient@domain.tld
     fi
   else
     echo "Ping failed on $SERVER - not trying SSH" | mailx -s "PING Failure on $SERVER!" recipient@domain.tld
  fi
done
exit    
Please note the colon ( : ) here - "if [ $RC -eq 0 ] ; then :  " - it's very important!
wmp
Avatar of bravo2nothing
bravo2nothing

ASKER

That's fantastic, how would I add logging to this also?
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks.....excellent help!