Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 13 of 13
@atreyu. Yes you are right, probably a good idea to get thoose emails. But thanks for the solution. I got a thought, again.. Is it possible in some way after ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Just Joined!
    Join Date
    Jan 2012
    Posts
    5

    @atreyu. Yes you are right, probably a good idea to get thoose emails. But thanks for the solution.

    I got a thought, again..

    Is it possible in some way after server2 "backup ip" have started up with wakeonlan to get it to go to sleep (halt command) when server1 "primary ip" goes up again. In som way some sort of loop, when server1 is up everything is fine, when server1 goes down server2 starts up (and mail), if server1 goes back up again server2 goes to sleep (halt) (and a new email), and then this goes back from the start again.

    Maybe this fails on the "halt command" because it have to run by root...

  2. #12
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by seven01 View Post
    Is it possible in some way after server2 "backup ip" have started up with wakeonlan to get it to go to sleep (halt command) when server1 "primary ip" goes up again. In som way some sort of loop, when server1 is up everything is fine, when server1 goes down server2 starts up (and mail), if server1 goes back up again server2 goes to sleep (halt) (and a new email), and then this goes back from the start again.
    If by going to sleep, you mean "shut down the system", then yes, you can do this. Assuming the ssh server is running on server2, then from you script you can log in and call the shutdown command (either as root, or as a user with sudo rights - more on that later).

    In the code, I made one change: when server1 is up, if the counter is greater 0, then server1 must have just come up, so server2 is pinged, and if it is up, an ssh command is run to remotely log in and shut it down. As coded, it calls sudo to do this (see bottom for details). In order to ssh to the remote machine without being prompted for a password, you can set up and use public/private SSH keys. This is easy to do and is quite common for such things.

    Here's a command to create the keys on server1:
    Code:
    ssh-keygen -t dsa -P '' -i ~/.ssh/id_dsa
    Now copy the keys to server2:
    Code:
    ssh-copy-id -i ~/.ssh/id_dsa server2
    That's it. You can test by just trying to ssh to server2 - you should not be prompted for a password:
    Code:
    ssh -i ~/.ssh/id_dsa -oPasswordAuthentication=no server2
    Now for the code itself:
    Code:
    #!/bin/bash
    
    # the counter file
    counterFile='/tmp/counter.txt'
    
    # set this to the maximum number of messages to be emailed
    maxCnt=10
    
    # ip address or hostname of primary and backup servers
    primary_ip='192.168.1.5'
    backup_ip='192.168.1.6'
    
    # mac of primary and backup servers
    primary_mac='00:11:22:33:44:55'
    backup_mac='00:00:00:11:11:11'
    
    ping_host() {
      local host out rc
      host=$1
      echo -n "Pinging host $host ... "
      out=$(ping -c4 $host|grep -q ' 0% packet loss')
      if [ $? -eq 0 ]; then
        echo alive
        rc=0
      else
        echo down
        rc=1
      fi
      return $rc
    }
    
    # get the current count
    declare -i cnt
    cnt=$(<$counterFile)
    
    # ping the primary server
    ping_host $primary_ip
    
    # if it is up
    if [ $? -eq 0 ]; then
    
      # shut down the back up server, if the primary just came back online
      if [ $cnt -gt 0 ]; then
    
        # ping the backup server
        ping_host $backup_ip
        if [ $? -eq 0 ]; then
    
          # send email
          #mail command here
    
          # run cmomand to shut down backup server, e.g. ssh
          ssh -i ~/.ssh/id_dsa $backup_ip sudo /sbin/shutdown -h now
    
        fi
      fi
    
      # reset the counter to zero
      echo 0 > $counterFile
    
      # exit the script
      exit 0
    fi
    
    
    # see if the count max has been reached
    if [ $cnt -ge $maxCnt ]; then
      echo "Message max has been reached"
    else
    
      # one-up the file counter
      cnt+=1
      echo $cnt > $counterFile
    
      # send an email
      echo "Sending email..."
      #(put your email command here)
    
    fi
    
    # if backup server is up...
    #if [ $? -eq 0 ]; then
      /path/to/wakeonlan $backup_mac
      if [ $? -eq 0 ]; then
        echo "The wakeup command succeeded"
    
        # see if the server truly woke up and is ping-able
        sleep 1
        ping_host $backup_ip
        exit $?
    
      else
        echo "The wakeup command FAILED!"
        exit 1
      fi
    
    # if backup server is down...
    #else
    #  echo "Cannot continue"
    #  exit 1
    #fi
    Quote Originally Posted by seven01 View Post
    Maybe this fails on the "halt command" because it have to run by root...
    Yes, you need to be root to shut down the system. For that reason, I made the shutdown command use sudo - a utility which allows a user to run a given program as root (with or without a password). If, as you seem to indicate, you cannot log in to server2 as root, then perhaps you can set up a user to be allowed to do this. Look into the visudo command, which is the program that root runs to edit /etc/sudoers, the file which defines sudo rights.

    Here's an example of a sudo entry:
    Code:
    username ALL = (root) NOPASSWD: /sbin/shutdown -h now

  3. #13
    Just Joined!
    Join Date
    Jul 2012
    Posts
    6
    #!/bin/bash
    HOSTS="192.168.0.121 192.168.0.145"
    COUNT=4

    for myHost in $HOSTS
    do
    count=$(ping -c $COUNT $myHost | awk -F',' '{ print $2 }')
    #echo $count
    count1="${count//[!0-9]}"
    #echo $count1
    #done

    if [ $count1 -eq 0 ]; then
    # 100% failed
    echo "Server failed at $(date)" | mail -s "Server Down" mymail@gmail.com
    echo "Host : $myHost is down (ping failed) at $(date)"

    fi
    done

    I modified this script like this but i am not getting particularly which server or ip is down, i am getting mail alert saying server down..please tell me what i need to modify this script, i am very thankful to everyone

  4. $spacer_open
    $spacer_close
Page 2 of 2 FirstFirst 1 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •