Mailx command examples

mailx is an evolution of the original mail command line utility for sending and receiving mail. It a very useful tool to test mail setup.

How the mail command works

The mail command invokes the “standard” sendmail command which in turns connects to the local MTA (mail transfert agent). In most modern distribution the sendmail binary isn’t used anymore, and it’s just a symbolic link to the MTA.

The local MTA (exim, postfix, etc..) run an SMTP server that accept connection on the port TCP 25.

Install the mail command

On Debian three packages provide the mailx command. I suggest you to install the heirloom-mailx package, because this version has more features and options.

Sending mails

You can use mailx interactively. You can hit enter for new lines, and when done typing the message press Ctrl+D and mailx would display an EOT:

$ mail -s "This is the subject" someone@foobar.com
I'm batman!
EOT

1. Body content from a file

The message body of the email can be taken from a file:

$ mail -s "This is Subject" someone@foobar.com < /path/to/file

The message can also be piped:

$ echo "This is message body" | mail -s "This is Subject" someone@foobar.com

2. Multiple recipients

To send the mail to multiple recipients, specify all the emails separated by a comma:

$ echo "This is message body" | mail -s "This is Subject" someone@foobar.com,someone2@foobar.com

3. CC and BCC

The -c and -b options can be used to add CC and BCC addresses respectively:

$ echo "This is message body" | mail -s "This is Subject" -c user@foobar.com someone@foobar.com

4. Specify From name and address

To specify a “FROM” name and address, use the -r option:

$ echo "This is message body" | mail -s "This is Subject" -r "ThePope<thepope@vactican.com>" someone@foobar.com

5. Specify “Reply-To” address

The reply to address is set with the internal option variable “replyto” using the -S option:

$ echo "This is message" | mail -s "Testing replyto" -S replyto="batman@foobar.com" someone@foobar.com

6. Attachments

Attachments can be added with the -a option:

$ echo "This is message body" | mail -s "This is Subject" -r "ThePope<thepope@vactican.com>" -a /path/to/file someone@foobar.com

nmap

nmap aka. network mapper is a very versatile network tool. It can be use to explore networks, perform quick ‘security’ audit and find open ports on remote machines.

Scan an IP

# nmap 192.168.0.4

Scan a range

# nmap 192.168.0.4-22

Scan a subnet

# nmap 192.168.0.0/24

Beside providing a netmask you can also use a wildcard character:

# nmap 192.168.0.*

Exclude an IP from a scan

Use the --exclude option. You can specify an IP or a range.

Scan a list of hosts from a file

Use the -iL option. nmap will use the file content as a list of host to scan.

Find out ‘live’ hosts in a network

Use the -sP option. Only the responsive hosts will be listed:

# nmap -sP 10.4.0.0/24

Guess the host OS

Use the -O --osscan-guess options. nmap will try to determine the OS of the scanned hosts using several tricks.

Perform a tcp null scan to “fool” a firewall

# nmap -sN 192.168.0.4