0

I am trying to write shell script to test if a server is able to reach a specified ip and port.

The commands i am using: ncat -w 5 <IP> 636 or 389 > /dev/null 2>&1 < /dev/null | echo $? The issue is when i check for an exit code for the above two ports i receive a 1.

When i run it without the /dev/null I get an connected to ip.
ncat -v -w 5 <IP> 636 Ncat: Version 6.40 ( http://nmap.org/ncat ) Ncat: Connected to <IP>:636. ^C

The issue is if i use the second command without /dev/null, i need to manually cancel the command using: CTRL + C

Any suggestions on how can i test the ip and ldap port using ncat or nc?

Jigar P
  • 1
  • 1
  • BTW forgot to mention the ncat version: `ncat --version Ncat: Version 6.40 ( http://nmap.org/ncat )` – Jigar P Sep 28 '17 at 22:00

2 Answers2

0

Try using the -z flag. If it has it.

-z      Specifies that nc should just scan for listening daemons, without
        sending any data to them.  It is an error to use this option in
        conjunction with the -l option.

So try this

ncat -z -w 5 <IP> 636
Henk Langeveld
  • 1,294
  • 10
  • 25
Mike
  • 21,910
  • 7
  • 55
  • 79
  • I try that command: `nc -z -w 5 636 nc: invalid option -- 'z' Ncat: Try '--help' or man(1) ncat for more information, usage options and help. QUITTING.` – Jigar P Sep 28 '17 at 21:59
  • I believe the -z option was included in a later ncat version, I might be using an older ncat as mention above in the comments. – Jigar P Oct 02 '17 at 14:03
0

Piping makes no sense here. Use ; echo $? not | echo $?.

2. Shell Command Language
[...]
2.9.2 Pipelines
[...]
the exit status shall be the exit status of the last command specified in the pipeline

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_02

eel ghEEz
  • 113
  • 4