Skip to content
Advertisement

Bash : Configuring email-provider for bash-script

I am working on writing a script to run on our Debian server(Debian 3.2.68-1+deb7u5 x86_64 GNU/Linux), which will monitor a specific port we have and when there is no process running on that port, or that port is available, then I will have to send out an email. I intend to run the script every 15 minutes and then send out an email.

We have an email-server and I want to add it’s configuration in the script, but I am not sure how I can do that.

When in Java, I am using the configuration in this manner :

Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "EMAIL-HOST-NAME");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "PORT-PROVIDED");

Currently, I have the script as follows, any suggestions to script are also welcome. Thank you.

#!/bin/bash

server=0.0.0.0 // localhost
port=PORT-NUMBER

  if nc $server $port &> /dev/null; then
       // do nothing
    else
       // send email 
  fi

Thank you.

Advertisement

Answer

Both mailx and sendmail can send full e-mails from CLI; most systems feature both preinstalled.

Example with ‘mailx’:

echo "This is the message body and contains the message" | mailx -v 
-r "someone@example.com" 
-s "This is the subject" 
-S smtp="mail.example.com:587" 
-S smtp-use-starttls 
-S smtp-auth=login 
-S smtp-auth-user="someone@example.com" 
-S smtp-auth-password="abc123" 
-S ssl-verify=ignore 
yourfriend@gmail.com
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement