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?

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
Mike
  • 22,748
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
  • 115