Testing connectivity using netcat on RHEL/CentOS

In this post I am going to show you how to test connectivity to a server on specific ports, essentially port scanning. We will perform these tests using a tool called netcat, which is a versatile networking utility for debugging and investigating the network.

Although the title states that this is for RHEL/CentOS, the same syntax applies for Ubuntu as I have tested it. I am sure it works for other versions of Linux, I just haven’t tested them so cannot confirm.

Before using netct, you must install it. On RHEL/CentOS, install it using yum:

yum install nc

The basic netcat command follows this syntax:

netcat [options] host port

This command will initiate a TCP connaction to the host and port stated in the command. Please note that netcat always initiates a TCP connection by default unless you specifically use the -u option which specifies UDP. Another thing to note is that connections made via netcat are unencrypted.

So using the above, I can test whether a web server is listening on port 80 by running the following command:

[roo@vs12app001:~]# nc -vz 172.16.0.33 80
Connection to 172.16.0.33 80 port [tcp/http] succeeded!
[roo@vs12app001:~]#

You will notice that used options -vz which are defined below:

-v Have nc give more verbose output.
-z Specifies that nc should just scan for listening daemons, without sending any data to them.

If the connection failed, meaning that port 80 is not open then you would see a time out error per the below:

[roo@vs12app001:~]# nc -vz 172.16.0.33 80
nc: connect to 172.16.0.33 port 80 (tcp) failed: Connection timed out
[roo@vs12app001:~]#

If the port was open but a firewall blocked it then you would see a connection refused error:

[roo@vs12app001:~]# nc -vz 172.16.0.33 80
nc: connect to 172.16.0.33 port 80 (tcp) failed: Connection refused
[roo@vs12app001:~]#

To test connectivity over a range of ports use the following syntax:

netcat host startport-endport

The command below shows this in practice, where I test connectivity to 172.16.0.31 on port rage 8080-8085:

[roo@vs12app001:~]# nc -vz 172.16.0.33 8080-8085
Connection to 172.16.0.33 8080 port [tcp/http-alt] succeeded!
Connection to 172.16.0.33 8081 port [tcp/tproxy] succeeded!
nc: connect to 172.16.0.33 port 8082 (tcp) failed: Connection refused
nc: connect to 172.16.0.33 port 8083 (tcp) failed: Connection refused
nc: connect to 172.16.0.33 port 8084 (tcp) failed: Connection refused
nc: connect to 172.16.0.33 port 8085 (tcp) failed: Connection refused
[roo@vs12app001:~]#

There are many more examples of how netcat can be used but the ones above are how I utilize this tool the most. See the references below for more information.

References:
Testing Network Services with netcat
How To Use Netcat to Establish and Test TCP and UDP Connections on a VPS
8 Practical Linux Netcat NC Command Examples
Linux Command Man Page for netcat

Testing SMTP Server from the command line

In a previous post I explained how to use the SMTPDIAG tool to test that SMTP and DNS were configured correctly. The tool does not however send a test message.

In this post I wil demonstrate two methods of sending test emails via the command line.

Method 1 – Telnet

I am going to assume that your server is Windows Server 2008 R2, although these steps will work on Server 2003 also. Another assumption is that you have the telnet client installed. If you don’t have it installed follow the steps in this post and then follow these instructions:

1. Fire up the command prompt and type telnet:

C:\Users\admin>telnet

2. At the telnet prompt, type set LocalEcho then press ENTER:

Welcome to Microsoft Telnet Client
Escape Character is 'CTRL+]'
Microsoft Telnet>set LocalEcho

3. Then type open localhost 25 and then press ENTER.

Welcome to Microsoft Telnet Client
Escape Character is 'CTRL+]'
Microsoft Telnet>set LocalEcho
Local echo on
Microsoft Telnet>open localhost 25

The output will look similar to this:

220 mail.vsysad.com Microsoft ESMTP MAIL Service, Version: 7.5.7601.17514 ready at Fri, 23 Aug 2013 01:02:03 +0000

4. Type helo me and then press ENTER. You should receive a 250 response from the SMTP Server meaning that it has accepted your command:

helo me
250 mail.vsysad.com Hello [127.0.0.1]

5. Then type the mail from:email@domain.com – obviously fill in the email address you are actually sending from and then press ENTER. The output will be similar to the below:

mail from:blog@vsysad.com
250 2.1.0 blog@vsysad.com....Sender OK

6. Type rcpt to:youremail@yourdomain.com – the address you are sending to and then press ENTER. The output will be similar to the below:

rcpt to:recipient@gmail.com
250 2.1.5 recipient@gmail.com

7. Type Data and then press ENTER, resulting in the following:

Data
354 Start mail input; end with <CRLF>.<CRLF>

8. Type Subject:This is a test email and then hit ENTER twice. Then type This is a test email being sent via telnet and hit ENTER. Hit ENTER again, then type a full stop (.), and then hit ENTER once more:

Subject:This is a test email

This is a test email being sent via telnet

.

9. The resulting output would be similar to this:

250 2.6.0 <WEB1Ze4CkNl4THZaN1E00000004@mail.vsysad.com> Queued mail for delivery

It means that an email has been generated and is in the queue and is ready to be delivered.

10. Now that you have finished, type quit and the connection to the SMTP Server will be closed:

quit
221 2.0.0 mail.vsysad.com Service closing transmission channel

Connection to host lost.

A screenshot of all the commands being run is below:

20130824104951

That’s it. Now go to your email account and verify that the email has arrived successfully. In my case I received the email below to my Gmail account:

20130824112821

Method 2 – PowerShell

The PowerShell method is much less tedious and is my recommended way of sending email via the command line on Windows. Assuming that PowerShell is installed on your server, launch the console and simply run the command below, ensuring that you complete the sending and receiving email addresses plus the subject and body text:

PS C:\Users\admin> Send-MailMessage -SMTPServer localhost -To xxxxx@gmail.com -From blog@vsysad.com -Subject "This is a test email" -Body "Hi Japinator, this is a test email sent via PowerShell"

The above command sent an email to my Gmail account, a screenshot of the email generated is below:

20131031223718

The PowerShell method is far easier to use. You can save the command in a .ps1 file and run it on demand whenever you need to test sending/routing of mail.

References:
How To Test SMTP Services Manually in Windows Server 2003
PowerShell Send-MailMessage command line reference