zsh-users
 help / color / mirror / code / Atom feed
* testing ports with ztcpsys
@ 2008-01-14 17:35 antho.charles
  2008-01-14 17:57 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: antho.charles @ 2008-01-14 17:35 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 795 bytes --]

Hi,

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 script.

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 ?


-- 
Anthony CHARLES

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 191 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: testing ports with ztcpsys
  2008-01-14 17:35 testing ports with ztcpsys antho.charles
@ 2008-01-14 17:57 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2008-01-14 17:57 UTC (permalink / raw)
  To: zsh-users

antho.charles@gmail.com 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
  else
    print Connection failed
    kill $!
    # assume it's not there
  fi

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

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-01-14 17:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-14 17:35 testing ports with ztcpsys antho.charles
2008-01-14 17:57 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).