Search Blog

Author: Chris Hougard
Date: January 1, 2014

Goal:Send email from linux command line using sendmail with a monospaced font

Difficulty: Easy

Prerequisites: Sendmail Installed, Access to a Linux Console

There are many times when formatting an email is not only a nice to have, but necessary to make it useful to the recipient. In this tutorial I will cover how to send an email from linux, using the sendmail program, with fixed font, also known as monospaced font.

This example contains the following files:

mailheader.txt - A text file that contains our static header data, things like the html starting tags, content type, etc

mailbody.txt - A text fiel that contains the body / text of the email we want to send in fixed font / monospace font

mailfooter.txt - A text file that contains our static footer data, basically just there to close out tags for our mailheader.txt, most likely with simple examples this won't be required but since i'm slightly anal about closing my html tags I'll leave it in here even though most things will render the email fine without them.

mailsender.sh - The shell script that runs our sendmail command and ties all of our formatting files together with our content file

mailheader.txt

MIME-Version: 1.0
Content-Type: text/html
Content-Disposition: inline
<html>
<body>
<pre style="font: monospace">

So breaking down mailheader.txt. We are setting the MIME version so that we can set a content type of html and content disposition of inline. Basically what this means is that from here on, the email will be an HTML formatted email, and the content should be inline in the email, as opposed to an attached html file. We then use the

<pre> tag to indicate that the content after this should use html's pre-formatting option which basically makes it so that we can include a simple blurb of text after it and we don't have to worry about our line endings or extra whitespace that html would typically ignore. The other thing we'll note is that we are setting an HTML font styling of monospaced. Now, there are a large number of fonts to choose from but in this example we are going for our typewriter style font so I chose monospace, you could just as easily have made this times new roman or your favorite web font.

mailbody.txt

Fruit     Number    Color     
------------------------------
Apple     100       Green     
Apple     2000      Red       
Orange    50        Orange

This is our text file that we want to include in a fixed/monospaced font in our email. Typically in my mind this means a fixed font table of some sort but there it certainly works for anything you might want.

mailfooter.txt

</pre>
</body>
</html>

And as I said in the file descriptions above, mailfooter.txt isn't the most difficult of files to read. It's just here to close our html tags out from the mailheader.txt file. Additionally, if you were using a multi-boundary email to say include some attachments, you could close off your html section here and start the necessary headers for your attachment.... But I digress.

mailsender.sh

#!/bin/bash
(
        echo "From: John Doe <john.doe@example.com>";
        echo "To: jane.doe@example.com";
        echo "Subject: My Awesome Typewriter Email";
        cat mailheader.txt mailbody.txt mailfooter.txt
) | /usr/sbin/sendmail -t

And now the script that ties them all together. In this file we set the from address, to address, subject, and we tie all of our text files together into one big happy family.

Author: Joe Meyer
Date: January 1, 2014

Goal: Use LDAP and PHP to authenticate with Active Directory

Difficulty: Moderate

Prerequisites: FastCGI and IIS Web Server, PHP LDAP module, Working knowledge of PHP

Many times in enterprise environments you already have an active directory server and all the users you would ever want to access something have an account there. So why go through the trouble of creating yet another authentication system? Why not just leverage those accounts, that way you eliminate another system to remember your password for, use your active directory system to enforce password strength, account lockouts, and all those other built in features that come with active directory.

Some notes about the implementation below: I implemented this on a windows 2012 server with IIS and PHP over FastCGI. Make sure that you have enabled / compiled the LDAP module in php. Where i defined the $adServer variable you can specify either the host name of the domain controller or the ip address. I also included a simple echo in the example to show you how to access objects of the active directory account logging in as well as a var dump so that you can see what the object contains. Please remove these once you know the information you are after.

<?php
/**
 * Created by Joe of ExchangeCore.com
 */
if(isset($_POST['username']) && isset($_POST['password'])){

    $adServer = "ldap://domaincontroller.mydomain.com";
	
    $ldap = ldap_connect($adServer);
    $username = $_POST['username'];
    $password = $_POST['password'];

    $ldaprdn = 'mydomain' . "\\" . $username;

    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);

    $bind = @ldap_bind($ldap, $ldaprdn, $password);


    if ($bind) {
        $filter="(sAMAccountName=$username)";
        $result = ldap_search($ldap,"dc=MYDOMAIN,dc=COM",$filter);
        ldap_sort($ldap,$result,"sn");
        $info = ldap_get_entries($ldap, $result);
        for ($i=0; $i<$info["count"]; $i++)
        {
            if($info['count'] > 1)
                break;
            echo "<p>You are accessing <strong> ". $info[$i]["sn"][0] .", " . $info[$i]["givenname"][0] ."</strong><br /> (" . $info[$i]["samaccountname"][0] .")</p>\n";
            echo '<pre>';
            var_dump($info);
            echo '</pre>';
            $userDn = $info[$i]["distinguishedname"][0]; 
        }
        @ldap_close($ldap);
    } else {
        $msg = "Invalid email address / password";
        echo $msg;
    }

}else{
?>
    <form action="#" method="POST">
        <label for="username">Username: </label><input id="username" type="text" name="username" /> 
        <label for="password">Password: </label><input id="password" type="password" name="password" />        <input type="submit" name="submit" value="Submit" />
    </form>
<?php } ?> 

Author: Joe Meyer
Date: January 1, 2014

Goal: Count Number of Files in a Directory

Difficulty: Easy

Prerequisites: Root Login Access

Often times you want to get an accurate count of the number of files in a specific linux directory/folder. So here's come code to do just that

Note: This will give you the count of files and folders of the directory/folder you are already in.

This will not include hidden files and will be 1 more than the number of files you actually have. So if you have 3 files the output will be 4. Here it goes:

ls -l | wc -l

 

Now there are times when you really want to see all hidden files and folders in addition to your normal ones.

ls -la | wc -l

 

We can even take this one step further and do searches on our file information. For example if you wanted to count all files that are read/write for owner only you could use this: (Keeping in mind to subtract 1 from the returned numbers also don't forget to escape your dashes.)

ls -la | grep "\-rw\-\-\-\-\-\-\-"| wc -l

 

Also, if you want to find all files and folders recursively in a directory you can use the following command:

find DIR_NAME -type f -print| wc -l

Author: Joe Meyer
Date: January 1, 2014

Goal: To remove old / unused linux kernels

Difficulty: Easy

Prerequisites: Root Login Access

Something many linux users will run across is after several times of running their favorite "yum update" command, they are suddenly out of room on the /boot partition of their drive and to make things worse yum update is spewing back error messages saying /boot is out of space every time they try to run. If you're like me perhaps the most annoying thing is that little update icon in the top right hand corner of the screen sitting there saying that it has kernel updates for you and no matter how many times you tell it to update and restart your machine it is still there, but I digress.

Step 1: Find Your Current Kernel Version

To find your current kernel version you can run the following command, but beware if you recently updated your kernel but have not yet restarted or kspliced, your system will show you the kernel that is in use, not necessarily the most recently downloaded. So rule of thumb before kernel updates is make sure you've restarted before removing old versions if possible.

Command:

uname -r

Sample Output:

2.6.32-279.5.2.el6.x86_64

Step 2: List Installed Kernels

Depending on your operating system you can list your installed kernels by using the rpm or dpkg command.

Command for Debian/Ubuntu:

 dpkg --list 'linux-image*'

Sample Output:

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-f/Unpacked/Failed-cfg/Half-inst/t-aWait/T-pend
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name                          Version                       Description
+++-=============================-=============================-==========================================================================
ii  linux-image                   2.6.22.14.21                  Generic Linux kernel image.
un  linux-image-2.6                                       (no description available)
rc  linux-image-2.6.20-15-generic 2.6.20-15.27                  Linux kernel image for version 2.6.20 on x86/x86_64
ii  linux-image-2.6.20-16-generic 2.6.20-16.32                  Linux kernel image for version 2.6.20 on x86/x86_64
ii  linux-image-2.6.22-14-generic 2.6.22-14.47                  Linux kernel image for version 2.6.22 on x86/x86_64
ii  linux-image-generic           2.6.22.14.21                  Generic Linux kernel image

Command for rpm based systems:

 rpm -q kernel

Sample Output:

 kernel-2.6.12-1.el5
kernel-2.6.18-17.el5
kernel-2.6.18-53.el5
kernel-2.6.18-53.1.4.el5

Step 3: Removing Old Kernels

Finally the actual deleting. Be very careful not to remove the kernel the system is currently running; doing so may result in system problems and will cause you a great deal of headaches. Now that you've been warned pick the kernel from your list of the installed kernels that is not the one you are using and run the appropriate remove command below.

For RHEL / CentOS / Fedora (and any other RPM based system) use:

rpm -e kernel-2.6.12-1.e15

For Debian / Ubuntu systems use:

sudo apt-get remove linux-image-2.6.22-14-generic

Author: Joe Meyer
Date: January 1, 2014

Goal: Generate a Random String with PHP

Difficulty: Easy

Estimated Time: Seconds

Prerequisites: PHP

Below is a function and implementation of a random string generator using PHP. Since PHP does not have it's own random string generator, we need to specify the allowed characters for our random string. In this example I have limited my random strings to Alpha-Numeric characters, but you can easily add your own.

function generateRandomString($length){ 
    $characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $charsLength = strlen($characters) -1;
    $string = "";
    for($i=0; $i<$length; $i++){
        $randNum = mt_rand(0, $charsLength);
        $string .= $characters[$randNum];
    }
    return $string;
}

Now if you want to generate a random string in your code you just drop this function in and you can call the function by doing something like echo generateRandomString(5); which will echo to the screen a random string of 5 characters long.

Author: Joe Meyer
Date: January 1, 2014

Goal: Find and Replace words in MySQL field

Difficulty: Easy

Prerequisites: Access to run MySQL Update queries

Often times you want to search through an entire column in a MySQL table and do a find and replace on that column. MySQL has a wonderful string function called Replace(). This allows you to pass a field along with a value you wish to find in the field, and replace it with a value of your choice. You can do this on the fly in select statements, but it is more commonly used for updating the tables themselves (at least in my experience).

UPDATE `table_name` SET field_name = replace(field_name,'find this','replace with this');

Another handy feature that MySQL has is using a REPLACE statement. This essentually does the same thing as INSERT except if there is a row with the same primary or unique key, it replaces it with this new record and deletes the old one. While this isn't quite as useful for doing bulk Find and Replaces, it does server a useful purpose when you are trying to determine if you need to create a new record, or update a new one. It should be noted that using this REPLACE method is similar to using INSERT IF NOT EXISTS AND UPDATE IF EXISTS except for the fact that it performs a DELETE operation before the record is recreated.