1

I'm resuming the discussion "Check if remote host/port is open - Can't use GNU Netcat nor NMap - RHEL 7".
I can't obtain the correct result because on my rh versions (5.6 and 7.1) there isn't the path /dev/tcp nor /dev/udp.

Here https://access.redhat.com/solutions/1214763 is explained that it can be resolved using

echo | nc -w1  $host $port >/dev/null 2>&1  ;echo $? 

and it runs with TCP protocol only but not with UDP:

echo | nc -w1 -u $host $port >/dev/null 2>&1  ;echo $? 

Indeed checking an inexistent service it ends with 0. Using the same ip and port on the RH 5.6 with the old option -z

nc -zu $host $port and it ends with 1, correctly.

Do you have an idea to resolve it?

Thanks

HBruijn
  • 72,524
  • 21
  • 127
  • 192
intore
  • 111
  • 3

1 Answers1

1

there isn't the path /dev/tcp nor /dev/udp

That is correct. Those aren't present in your file-system as directories, but when you include both a hostname/ip-address and and port number AND use them in a redirect bash will do some magic. As phrased in the manual:

Bash handles several filenames specially when they are used in redirections, as described in the following table:

  • /dev/fd/fd
    If fd is a valid integer, file descriptor fd is duplicated.
  • /dev/stdin
    File descriptor 0 is duplicated.
  • /dev/stdout
    File descriptor 1 is duplicated.
  • /dev/stderr
    File descriptor 2 is duplicated.
  • /dev/tcp/host/port
    If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a TCP connection to the corresponding socket.
  • /dev/udp/host/port
    If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a UDP connection to the corresponding socket.

Instead of for instance:

$ ls -l /dev/tcp/serverfault.com/80  
ls: cannot access /dev/tcp/serverfault.com/80: No such file or directory

try a redirect with > or <

$ echo "" > /dev/tcp/serverfault.com/80
$ echo " " > /dev/tcp/serverfault.com/81
-bash: connect: Connection refused
HBruijn
  • 72,524
  • 21
  • 127
  • 192
  • Hi HBruijn,thanks but it doesn't run. It didn't give me any output, using both a valid couple IP/PORT and an invalid couple. – intore Aug 08 '17 at 14:21