How to check a route exist between two hosts for a particular port?
How to check whether there is a route between two hosts for a particular port (e.g. 1433).
Solution 1:
with 'ip route get ADDRESS' you can check the route configured in your system, that is, the first hop:
$ ip route get 192.168.10.10
192.168.10.10 via 192.168.10.1 dev eth0 src 192.168.10.11
cache
to check the port connectivity, a simple telnet should do:
$ telnet 192.168.10.10 1433
...
good luck!
Solution 2:
Fisrt, you must know that, the route
action occurs at Network Layer, while port
is defined at Transport Layer. So If there is a route exists between two host doesn't mean you can reach a port in a host.
Imagining two host is two house, port is gate of the house, route is road. You can build many roads between houses. But when you reach a house, if the house's gate is closed, you can not come in.
UPDATE
For you comment question, you could use some command like:
To check route exists:
- route
- traceroute
To check open port:
- telnet
- netcat
Read it manpage and try using.
Solution 3:
As others have mentioned, having a route doesn't necessarily mean you have connectivity. If that's what you're looking to test, netcat
offers the -z
option to scan to see if a port is open. (You may also wish to specify a timeout with -w
if you may not have a route; the default timeout is usually a couple of minutes.)
$ nc -z 127.0.0.1 22; echo $?
0
$ nc -z 127.0.0.1 11; echo $?
1
You can use the exit code to do something (or not) based on whether you have demonstrated connectivity to that address and port:
if nc -z 127.0.0.1 22; then
echo "SSH server is available."
else
echo "Cannot connect to SSH server."
fi
The -v
option will make the output more verbose, and this can be used to scan a range of ports:
$ nc -vz 127.0.0.1 22-25
Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!
nc: connect to 127.0.0.1 port 23 (tcp) failed: Connection refused
nc: connect to 127.0.0.1 port 24 (tcp) failed: Connection refused
Connection to 127.0.0.1 25 port [tcp/smtp] succeeded!
$ echo $?
0
As shown above, in multiport scan mode the exit code will be true (0) if any of the ports succeeded in connecting, or false otherwise.
There are several different versions of netcat
; the one used in the examples above is the netcat-openbsd
package from Debian 9, which is a rewrite of the "traditional" netcat (netcat-traditional
package). For these particular parameters and exit codes the traditional version is substantially similar. If you are having problems with netcat command line parameters and exit codes, check which version you're using; ls -l /bin/nc*
may give some insight.