Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: testing ports with ztcpsys

antho.charles@xxxxxxxxx wrote:
> I'm writing a script that gather some system informations on several
> computers, and I want to test if some ports are in use. I think netcat is
> good, but I remember that zsh have a tcp module so I try:
> -- code --
> #!/bin/zsh
> autoload -U tcp_open
> tcp_open "$1" "$2" &> /dev/null
> [[ $? -eq 1 ]] && exit 1 || tcp_close &>/dev/null && exit 0
> -- end code --
> It works but when the host doesn't respond ( you can try tcp_open
> www.google.fr 8080 for example ), the timeout is really long and slow the s=
> cript.
> So, I try with ztcp, checking $REPLY value but it doesn't work: $REPLY
> is set in the same shell than the ztcp command, so I should wait for
> the ztcp to return before I can check $REPLY.
> My question is: 
> is there a way to hack a timeout ?

It doesn't look like there's an easy way of doing this.  The best I can
come up with is to try it in a subshell and time this out in the parent
shell, along the lines of (I haven't actually tried this):

  (if ztcp www.google.fr 8080; then
     ztcp -c $REPLY
     print $REPLY
  fi) > /tmp/ztcp.$$ 2>&1 &

  # could loop checking, or something more sophisticated with
  # SIGALRM or pipes
  sleep 5
  if [[ -s /tmp/ztcp.$$ ]]; then
    print Connection succeeded
    # assume it will work again
    print Connection failed
    kill $!
    # assume it's not there

Obviously this only gives an indication of whether the device is
alive, not a connection within a given timeout.

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070

Messages sorted by: Reverse Date, Date, Thread, Author