sendmail and shell script --> path to?

Discussion in 'Server Operation' started by danhansen@denmark, Oct 26, 2013.

  1. danhansen@denmark

    danhansen@denmark Member HowtoForge Supporter

    Hello ;)


    I'm running an Ubuntu Server 12.04.2 - just a basic installation. No Apache, No PHP or anything. I'm doing a shell script and this script needs to send a mail.
    My port 25 IS open of course. To have the script send a mail on the Ubuntu Server 12.04.2 I need to define a path for the mail client. Here's a part of the script:

    echo '============================'
    echo '= CPU Temp. i$ degrees ='
    echo '============================'
    /sbin/shutdown -h now
    /usr/sbin/ssmtp [email protected] </home/xxx/MyScripts/hotcpu.txt
    echo 'Email Sent.....'
    exit

    1. Is "sendmail" the default mail client in Ubuntu Server 12.04?

    2. I need the right path to put into my script.

    3. Will I need to protect the server against spammers? Server is in network where router uses Fixed IP, LAN/NAT and server is running UFW
     
  2. florian030

    florian030 Well-Known Member HowtoForge Supporter

    To get the path you can use which XXX

    And in your script:

    Code:
    MAIL=`which mail`[code]
    
    afterwards the path is stored in $MAIL
    
    BTW: I would send the mail before shutting down the system.
     
  3. danhansen@denmark

    danhansen@denmark Member HowtoForge Supporter

    sendmail? path to client? protection against spammers?

    Hi florian,

    Thanks for your reply ;)

    1.
    In this post: http://forum.havetheknowhow.com/viewtopic.php?f=5&t=820&p=2629#p2629
    I asked the same question. The guy who made the first script said:
    b. The reason the shutdown command comes before the command to send the email is to ensure the server gets shutdown. I didn't want it staying alive if the email command made the whole script hang. The server does not shut down instantly and so has enough time to run the other lines in the script.
    But I agree with you! It needs to be place after the shutdown command - my servers shuts down in less than 2 seconds! And isn't it possible to add a command to force the script to goto a sudden command if something halts it?!?

    2.
    Please define this. I'm not that "Ubuntu" yet ;)

    3.
    I'm not sure what you mean here, I inserted/wrote the X's myself. I write X's in scripts/code's/text's whenever it's a choice. Instead of e.g. /home/MYFOLDERNAME/MyScripts/msg.txt.
    It's this path I need to know/change: /usr/sbin/ssmtp to a program in Ubuntu Server 12.04 which will send the txt file/msg. And I think I read somewhere that "sendmail" is a default mail-client in Ubuntu and the program I need to send my txt.file from the server.

    4.
    Will I need to protect the server against spammers?

    Here the's whole script before I edited it - I changed it so that you get a warning if it gets hot and shutsdown if it gets too hot!
    Code:
    #!/bin/bash
    
    # PURPOSE: Script to check temperature of CPU cores and report/shutdown if specified temperatures exceeded
    #
    # AUTHOR: feedback[AT]HaveTheKnowHow[DOT]com
    
    # Expects two arguments:
    #    1. Warning temperature
    #    2. Critical shutdown temperature
    #    eg. using ./CPUTempShutdown.sh 30 40
    #        will warn when temperature of one or more cores hit 30degrees and shutdown when either hits 40degrees.
    
    # NOTES:
    # Change the strings ">>/home/htkh" as required
    # Substitute string "[email protected]" with your own email address in the string which starts "/usr/sbin/ssmtp [email protected]"
    
    # Assumes output from sensors command is as follows:
    #
    # coretemp-isa-0000
    # Adapter: ISA adapter
    # Core 0:      +35.0 C  (high = +78.0 C, crit = +100.0 C)  
    #
    # coretemp-isa-0001
    # Adapter: ISA adapter
    # Core 1:      +35.0 C  (high = +78.0 C, crit = +100.0 C) 
    #
    # if not then modify the commands str=$(sensors | grep "Core $i:") & newstr=${str:14:2} below accordingly
    
    echo "JOB RUN AT $(date)"
    echo "======================================="
    
    echo ''
    echo 'CPU Warning Limit set to => '$1
    echo 'CPU Shutdown Limit set to => '$2
    echo ''
    echo ''
    
    sensors
    
    echo ''
    echo ''
    
    for i in 0 1
    do
    
      str=$(sensors | grep "Core $i:")
      newstr=${str:14:2}
    
      if [ ${newstr} -ge $1 ]
      then
        echo '============================'                             >>/home/htkh/Desktop/CPUWarning.Log
        echo $(date)                                                    >>/home/htkh/Desktop/CPUWarning.Log
        echo ''                                                         >>/home/htkh/Desktop/CPUWarning.Log
        echo ' WARNING: TEMPERATURE CORE' $i 'EXCEEDED' $1 '=>' $newstr >>/home/htkh/Desktop/CPUWarning.Log
        echo ''                                                         >>/home/htkh/Desktop/CPUWarning.Log
        echo '============================'                             >>/home/htkh/Desktop/CPUWarning.Log
      fi
      
      if [ ${newstr} -ge $2 ]
      then
        echo '============================'
        echo ''
        echo 'CRITICAL: TEMPERATURE CORE' $i 'EXCEEDED' $2 '=>' $newstr
        echo ''
        echo '============================'
        /sbin/shutdown -h now
        /usr/sbin/ssmtp [email protected] </home/htkh/MyScripts/msg.txt
        echo 'Email Sent.....'
        exit
      else
        echo ' Temperature Core '$i' OK at =>' $newstr
        echo ''
      fi
    done
    
    echo 'Both CPU Cores are within limits'
    echo ''
    
     
    Last edited: Oct 27, 2013
  4. florian030

    florian030 Well-Known Member HowtoForge Supporter

    Hi Dan,

    I would just send the mail before shutting down the system. You may send the mail in the background by adding &bg.

    I just don´t like hardcoded paths in scripts. :)
    Code:
    #!/bin/bash
    SSMTP=`which ssmtp`
    $SSMTP [email protected] </home/xxx/MyScripts/hotcpu.txt
    
    I would use mail / mailx to send local mail from a script.

    Depends on your setup. ;) You can add some rules to the firewall to limit access to port 25 or setup your MTA to reject unwanted clients.
     

Share This Page