Linux test tcp port

Test if remote TCP port is open from a shell script

I’m looking for a quick and simple method for properly testing if a given TCP port is open on a remote server, from inside a Shell script. I’ve managed to do it with the telnet command, and it works fine when the port is opened, but it doesn’t seem to timeout when it’s not and just hangs there. Here’s a sample:

l_TELNET=`echo "quit" | telnet $SERVER $PORT | grep "Escape character is"` if [ "$?" -ne 0 ]; then echo "Connection to $SERVER on port $PORT failed" exit 1 else echo "Connection to $SERVER on port $PORT succeeded" exit 0 fi 

I either need a better way, or a way to force telnet to timeout if it doesn’t connect in under 8 seconds for example, and return something I can catch in Shell (return code, or string in stdout). I know of the Perl method, which uses the IO::Socket::INET module and wrote a successful script that tests a port, but would rather like to avoid using Perl if possible. Note: This is what my server is running (where I need to run this from) SunOS 5.10 Generic_139556-08 i86pc i386 i86pc

The answer lied with Expect. We wrote a simple script that sends a telnet on the port we needed, with a timeout of 8 seconds. There’s plenty of examples to pick from too. We based ours off this post: unix.com/shell-programming-scripting/…

check_tcp from github.com/monitoring-plugins/monitoring-plugins can do this, including entering strings and checking for an expected answer.

18 Answers 18

As pointed by B. Rhodes, nc ( netcat ) will do the job. A more compact way to use it:

That way nc will only check if the port is open, exiting with 0 on success, 1 on failure.

For a quick interactive check (with a 5 seconds timeout):

FWIW, I have completely overhauled my answer with an example, separately applicable to both RHEL 6 and RHEL 7.

on Mac at least, you may need to add -G# to set a connection timeout separate from/in addition to the -w# timeout, which basically functions as a read timeout.

@jolestar You can manually upgrade Ncat on Centos 7 to get the -z option. You may want to consider: unix.stackexchange.com/questions/393762/…

It’s easy enough to do with the -z and -w TIMEOUT options to nc , but not all systems have nc installed. If you have a recent enough version of bash, this will work:

# Connection successful: $ timeout 1 bash -c 'cat < /dev/null >/dev/tcp/google.com/80' $ echo $? 0 # Connection failure prior to the timeout $ timeout 1 bash -c 'cat < /dev/null >/dev/tcp/sfsfdfdff.com/80' bash: sfsfdfdff.com: Name or service not known bash: /dev/tcp/sfsfdfdff.com/80: Invalid argument $ echo $? 1 # Connection not established by the timeout $ timeout 1 bash -c 'cat < /dev/null >/dev/tcp/google.com/81' $ echo $? 124 

What’s happening here is that timeout will run the subcommand and kill it if it doesn’t exit within the specified timeout (1 second in the above example). In this case bash is the subcommand and uses its special /dev/tcp handling to try and open a connection to the server and port specified. If bash can open the connection within the timeout, cat will just close it immediately (since it’s reading from /dev/null ) and exit with a status code of 0 which will propagate through bash and then timeout . If bash gets a connection failure prior to the specified timeout, then bash will exit with an exit code of 1 which timeout will also return. And if bash isn’t able to establish a connection and the specified timeout expires, then timeout will kill bash and exit with a status of 124.

Читайте также:  Копирование файлов консоли линукс

Use a different syntax for Git Bash:

Otherwise, Git Bash will return an error where none is expected:

$ timeout 1 bash -c 'cat < /dev/null >/dev/tcp/google.com/80' $ echo $? 124 

Источник

Best Examples to Test Port Connectivity in Linux(RedHat 7/CentOS 7/Ubuntu 18.04)

CyberITHub

In this tutorial, I will take you through the different tools that can be used to check/test port connectivity in Linux.

Test Port Connectivity

You might be aware of curl, netstat, telnet and nc command in Linux: —

curl — It is usually used for downloading web pages or files from a Linux/Unix command line. But there’s another great usage curl command has: testing TCP ports connectivity. Taking an example, let’s assume you’re helping with some network changes and need to confirm that connection from your server to some remote host and specific TCP port still works.

telnet — telnet command is used for interactive communication with another host using the TELNET protocol. It begins in command mode, where it prints a telnet command prompt («telnet>»). If telnet is invoked with a host argument, it performs an open command implicitly.

nc — nc command is for performing maintenance/diagnosis tasks related to network . It can perform operations like read,write or data redirections over the network, similar to how you can use cat command to manipulate files on Linux system

What is Port

Ports are basically Communication endpoint in Network Topology through which outbound and inbound traffic flows.

Prerequisites

You need to have netstat, curl, nc and telnet command in linux.

Best Examples to Test Port Connectivity in Linux(RedHat 7/CentOS 7/Ubuntu 18.04) 1

Test TCP Ports

What is TCP

TCP stands for Transmission Control Protocol. Using this method, the system sending the data connects directly to the computer it is sending the data it to, and stays connected for the duration of the transfer.

With this method, the two systems can guarantee that the data has received safely and correctly without compromising the integrity of packets, and then they disconnect the connection.

Читайте также:  Skype for linux arch

This method of transferring data tends to be quicker and more reliable, but puts a higher load on the system as it has to monitor the connection and the data flowing across it.

Here’s how you can do it using curl command and its telnet functionality.

Test SSH port connection with curl

You can test local port 22 through curl command as specified below.

[root@localhost ~]# curl -v telnet://127.0.0.1:22 * About to connect() to 127.0.0.1 port 22 (#0) * Trying 127.0.0.1. * Connected to 127.0.0.1 (127.0.0.1) port 22 (#0) SSH-2.0-OpenSSH_7.4 ^C

Test SSH port connection with telnet

You can test port 22 by using telnet command as mentioned below.

[root@localhost ~]# telnet 127.0.0.1 22 Trying 127.0.0.1. Connected to 127.0.0.1. Escape character is '^]'. SSH-2.0-OpenSSH_7.4 ^C

Test UDP Ports

What is UDP

UDP is known as User Datagram Protocol. Using this method, the system sending the data packages the information into a nice little packets and releases it into the network with the hopes that it will get to the right destination.

What this means is that UDP does not connect directly to the receiving computer like TCP does, but rather sends the data out and relies on the network devices in between the transmitting system and the receiving system to get the data where it is supposed to go properly.

This method of transmission does not provide any guarantee that the data you transmit will ever reach its destination. On the other hand, this method of transmission has a very low overhead and is therefore very popular to use for services that are not that important to work on the first go.

Test SSH port connection with nc

You can test udp port 123 by using below netcat command:-

[root@localhost ~]# nc -v -z -u 127.0.0.1 123 Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: Connected to 127.0.0.1:123. Ncat: UDP packet sent successfully Ncat: 1 bytes sent, 0 bytes received in 2.02 seconds.

Источник

5 ways to check if a Port is open on a remote Linux PC

open ports on Linux system

T here is an ample number of ways to check for any open ports on a remote Linux PC. Knowing open ports on a Linux machine helps system administrators to connect to the remote PC for troubleshooting system and cloud server issues.

TCP and UDP ports

TCP stands for Transmission Control Protocol. In this method, the computers get connected directly until the data transfer is taking place. Therefore, with this method, the data transfer is guaranteed and is reliable but puts a higher load on the server as it has to monitor the connection and the data transfer too.

UDP stands for User Datagram Protocol. Using this method, the data is sent in the form of little packages into the network with the hope that it reaches the final destination. It means the two computers are not connected directly to each other. This method does not provide any guarantee that the data you send will ever reach its destination. Load on the server is less, and so this method is used commonly by the system administrators first to try something that’s not so important.

Читайте также:  Odbc драйвер postgresql linux

Now that you know the types are ports on a Linux system, let’s get started with ways of finding the ones that are open.

Best ways to check if a Port is open on a Linux PC

There are multiple ways you can do it. However, the most reliable way to do this is by using the following commands:

  • nc: netcat command
  • nmap: network mapper tool
  • telnet: telnet command
  • echo > /dev/tcp/..
  • netstat – tuplen

Let’s go through each method one by one.

1. netcat command

netcat is a simple Unix utility that can be used to write and read data using UDP and TCP protocol across network connections.

The primary reason for its design is to provide a back-end tool that works with the scripts and programs. It is also an exploration and network debugging tool that offers tons of features.

To use it, you need to install it in your distro using the respective installation commands.

Источник

How to Test Port [TCP/UDP] Connectivity from a Linux Server

Here is a short post to check port [TCP/UDP] connectivity from a Linux server. A TCP/IP network connection may be either blocked, dropped, open, or filtered. These actions are generally controlled by the IPtables firewall the system uses and is independent of any process or program that may be listening on a network port.

Telnet and nc are common tools used to test port connectivity from Linux server. Telnet can be used to test tcp port connections, where as nc can be used to test both tcp/udp ports connectivity. Make sure telnet and nc tools are installed on the Linux server you are trying to test connectivity.

# yum install nc # yum install telnet

Testing TCP port connectivity with telnet

Lets see how we can use telnet command to test the TCP port connectivity. The syntax to use the telnet command is as follows:

# telnet [hostname/IP address] [port number]

Example of successful connection:

# telnet 192.168.12.10 22 Trying 192.168.12.10. Connected to 192.168.12.10. Escape character is '^]'. SSH-2.0-OpenSSH_6.6.1 Protocol mismatch. Connection closed by foreign host.

Example of unsuccessful connection:

# telnet 192.168.12.10 22 Trying 192.168.12.10. telnet: connect to address 192.168.12.10: No route to host

Using nc command to test TCP port connectivity

The syntax to use nc command for testing TCP post connectivity is as follows:

# nc -z -v [hostname/IP address] [port number]

Example of successful connection:

# nc -z -v 192.168.10.12 22 Connection to 192.118.20.95 22 port [tcp/ssh] succeeded!

Example of unsuccessful connection:

# nc -z -v 192.168.10.12 22 nc: connect to 192.118.20.95 port 22 (tcp) failed: No route to host

Testing UDP port connectivity wit nc command

The syntax to test UDP port connectivity with nc command is as follows:

# nc -z -v -u [hostname/IP address] [port number]

Example of successful connection:

# nc -z -v -u 192.168.10.12 123 Connection to 192.118.20.95 123 port [udp/ntp] succeeded!

Источник

Оцените статью
Adblock
detector