How to Check Network Port Open on Linux

1) 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.

2) 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

3) 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

4) 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

5) 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

6) 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

7) 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)
...
..
.
Advertisements
This entry was posted in Linux and tagged , , , , , , , , , , , , , . Bookmark the permalink.

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.