How to Check Network Port Open on Linux

Telnet

$ telnet ractest 1234
Trying 10.3.2.14...
telnet: connect to address 10.3.2.14: Connection refused

$ telnet ractest 3872
Trying 10.3.2.14...
Connected to ractest.
Escape character is '^]'.


Connection closed by foreign host.

cURL

$ curl -v telnet://ractest:1234
* About to connect() to ractest port 1234 (#0)
* Trying 10.3.2.14... Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host


$ curl -v telnet://ractest:3872
* About to connect() to ractest port 3872 (#0)
* Trying 10.3.2.14... connected
* Connected to ractest (10.3.2.14) port 3872 (#0)
* Closing connection #0

Bash

$ cat < /dev/tcp/10.3.2.14/1234
-bash: connect: Connection refused
-bash: /dev/tcp/10.3.2.14/1234: Connection refused


$ cat < /dev/tcp/10.3.2.14/3872
^C

Python

$ python
Python 2.6.6 (r266:84292, Sep 4 2013, 07:46:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('10.3.2.14',1234))
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused
>>>
>>>
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('10.3.2.14',3872))
>>> clientsocket.send('\n')
1

Perl

$ perl
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(PeerHost => '10.3.2.14',
PeerPort =>'1234',
Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";
^D
cannot connect to the server Connection refused

$ perl
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(PeerHost => '10.3.2.14',
PeerPort =>'3872',
Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";
^D
connected to the server

On server to check port is listening

$ netstat -na|grep 1521|grep -i listen
tcp 0 0 10.3.2.14:1521 0.0.0.0:* LISTEN

On server to check connections through listening port

$ lsof -iTCP:1521

$ lsof -i :1521
COMMAND   PID  USER   FD  TYPE DEVICE    SIZE/OFF NODE NAME
ora_lreg_ 5396 oracle 49u IPv4 935666084 0t0      TCP  ractest:23228->ractest-vip:ncube-lm (ESTABLISHED)
...
..
.

ssh

$ ssh -vv HOSTNAME -p port_number

$ ssh -vv racnode2 -p 1521
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
debug1: Reading configuration data /home/grid/.ssh/config
debug1: /home/grid/.ssh/config line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug2: resolving "racnode2" port 1521
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to racnode2 [10.1.2.3] port 1521.
debug1: Connection established.
...
..
.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.