Ldap – Exit code for ldap testing using nc or ncat

ldapncshellshell-scripting

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?

Best Answer

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