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 80
Connection to 80 port [tcp/http] succeeded!

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 80
nc: connect to port 80 (tcp) failed: Connection timed out

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

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

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 on port rage 8080-8085:

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

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.

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

PowerShell script to copy file to multiple remote servers

20160614150939I often have to copy files and folders from one server to another for various reasons. Copying stuff manually is a bore but if you are only copying to a couple of servers it is quicker to do it manually but if you have to copy to several servers it would be quicker to use a script. We can leverage PowerShell to perform this task.

We will use the Copy-Item cmdlet in PowerShell. It will copy  file and folders to a remote server using the following syntax:

Copy-Item "source" -Destination "\\server\C$"

Please note that the user you are running the commands under must also exist and work on the servers you are copying the file(s) to. All the examples in this post were performed in a test domain environment using a Domain Admin account but as long as your user has sufficient rights the commands should work fine.

Let us use an example to demonstrate how Copy-Item works. I want to copy the EMC Networker SQL agent NWVSS.exe from server vs11vdc01 to server vs11app01. To do so, I would run:

Copy-Item "C:\Software\EMC\Networker\NWVSS.exe -Destination \\vs11app01\c$\temp

As you can see from the above, I have stated the full path of the NWVSS.exe file and the destination it is going to, in this case \\vs11app01\c$\temp. Running  results in a successful file copy:


To copy to another server you could use the same command and change the destination accordingly but that wouldn’t good enough for the Japinator. I prefer a slightly more sophisticated solution – we can use the Get-Content cmdlet or gc for short to get the contents of a text file called servers.txt that contains a list of the servers you want to copy to. This text file simply contains a list of servers, each on a new line per the below:


We can pass each line of the contents of servers.txt as variables to the Copy-Item cmdlet to perform the copies to those servers. The script below will achieve this:

# This file contains the list of servers you want to copy files/folders to
$computers = gc "C:\scripts\servers.txt"

# This is the file/folder(s) you want to copy to the servers in the $computer variable
$source = "C:\Software\EMC\Networker\NWVSS.exe"

# The destination location you want the file/folder(s) to be copied to
$destination = "C$\temp\"

#The command below pulls all the variables above and performs the file copy
foreach ($computer in $computers) {Copy-Item $source -Destination "\\$computer\$destination" -Recurse}

The file copies successfully to all servers in C:\scripts\servers.txt successfully, below we see the file on vs11app02:


To make it a little but more sophisticated we will add a check to confirm that the remote servers’ path exists using the Test-Path cmdlet before performing the copy. If the path does not exist it will state that it “is not reachable or does not exist”:

# This file contains the list of servers you want to copy files/folders to
$computers = gc "C:\scripts\servers.txt"

# This is the file/folder(s) you want to copy to the servers in the $computer variable
$source = "C:\Software\EMC\Networker\NWVSS.exe"

# The destination location you want the file/folder(s) to be copied to
$destination = "C$\temp\"

foreach ($computer in $computers) {
if ((Test-Path -Path \\$computer\$destination)) {
Copy-Item $source -Destination \\$computer\$destination -Recurse
} else {
"\\$computer\$destination is not reachable or does not exist"

I hope this is useful to you and saves you time and energy! So kick this script off, get a coffee and put your feet up.

PowerShell : Copy File(s) TO / FROM remote Servers