loading...

Linux Mint – Sending system reports via e-mail

How to Install Docker on macOS

To wrap up this chapter, we’ll work through an example of having the system e-mail you information at various intervals. This is very useful if you want to be updated periodically about any specific resource, such as installed packages or even backups. In our example, we’re going to set up a report that will contain a list of all the packages installed on our system.

In order to create e-mail alerts, you first need an e-mail account from which you can send the alert. It’s recommended that you do not use your personal e-mail account when dealing with scripts. Instead, either create an e-mail account for this purpose, or if you work in a corporate environment, ask the e-mail administrator to set you up with an alert account to use. Next, you’ll need the sendEmail package installed, so let’s take care of this with the following command:


sudo apt-get install sendemail

The sendEmail utility runs in the shell, so you won’t see it in your Applications menu. The purpose of the sendEmail command is to provide you with an interface through which you can send e-mails through a script. However, before we dive in, let’s take a look at the following example of the syntax used for the sendEmail command:


sendEmail -f <from_address> -t <send_to_address> -u "Subject of Email" -o message-file=message.txt -s <SMTP server address> -xu <Email account username> -xp <Email account password>

The sendEmail command example is definitely the longest we’ve used so far, so let’s break it down. First, we type the sendEmail command; there is no surprise here. Then, we pass a number of flags to the sendEmail command. After the -f flag, we type the e-mail address that should appear in the “from” field of the e-mail. The -t flag is where we supply the “to” address. The -u flag is where we type the subject of the message, and the -o flag is where we include an attachment to put into the body of the e-mail. Next, we have -s where we supply the address of the SMTP server, and then we have the -xu flag where we provide the user account used for the e-mail server, followed by -xup and then the password for the account.

Another sample with the flags filled in with more relevant examples is shown as follows:


sendEmail -f server@mycompany.com -t admin@myemail.com -u "Important Alert!" -o message-file=/tmp/reporttext.txt -s smtp.server.net -xu jdoe -xp SecretPassword

Note

Notice how the password is shown in clear text. This is why it’s a good idea to use a dedicated account, and not one that’s actually important. Even with using a dedicated account, keep the script in a secure place, so bots can’t find it and start sending malware using your company’s e-mail server.

Notice the message file attachment set to /tmp/reporttext.txt. This file does not have to be named reporttext.txt, nor does it need to be stored in the /tmp directory; this may be a good place to store it though, since you only need the output file long enough to send the e-mail.

So, how do you generate the output used in the reporttext.txt file? You can redirect the output of any command you want to report on into a text file. In this example, we want to create and e-mail a list of installed packages. The following command will print a list of installed packages on the system:


dpkg –get-selections

Unfortunately, the command merely prints the list directly to the standard output (what you see in the terminal window) so this won’t help us, but the following statement will:


dpkg –get-selections > /tmp/reporttext.txt

Now, we have something we can use. We basically took the output of the dpkg –get-selections command and threw it into a text file. Now, we can include this text file in our e-mail. In fact, the beauty of this is that you can send pretty much anything you want. As long as you can get it into a text file, you can send it, so you can report on anything you want. Now, all we have to do is send the file. We can use the following sample command to include the reporttext.txt file and send the contents off in an e-mail.

When you put all of this together in a script, it looks something like the following:


#!/bin/bash
dpkg –get-selections > /tmp/reporttext.txt
sendEmail -f server@mycompany.com -t admin@myemail.com -u "Important Alert!" -o message-file=/tmp/reporttext.txt -s smtp.server.net -xu jdoe -xp SecretPassword

Note

In order for this script to work, you’ll have to replace each flag with actual values for a real e-mail server. In order to find out what those values are, you’ll have to check the help menu for your e-mail service or ask the individual who manages your e-mail server. Typically though, the values are the same as the values your e-mail service uses to send e-mails from a client program, such as Outlook or Thunderbird.

Now that you have a script, you can move it into the /usr/local/bin folder, using the following command line, so that it is recognized throughout the Linux shell:


sudo mv myemailscript /usr/local/bin

Then, make sure it’s marked as executable using the following statement:


sudo chmod +x /usr/local/bin/myemailscript

Once this is complete, you can also add the script as a job in cron, so that it automatically sends you the report at various intervals. Feel free to create as many reports as you want and then use the section on adding a cron job as a guide to make the report run automatically. Also, feel free to experiment and see what kind of reports you can create. Another good example is disk space, which you can capture with the following command:


df -h > /tmp/hdusage.txt

Comments are closed.

loading...