e81c Check connection with timeout, script telnet web server in script - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Server & Networking > Web & Network Services

Web & Network Services Discussion related to network/web services such as apache, bind, sendmail, etc.

Reply
 
Thread Tools Display Modes
  #1  
Old December 6th, 2008, 10:17
bsddaemon's Avatar
bsddaemon bsddaemon is offline
Member
 
Join Date: Nov 2008
Location: Melbourne
Posts: 102
Thanks: 13
Thanked 7 Times in 7 Posts
Default Check connection with timeout, script telnet web server in script

Hi guys, Im having trouble finding the way to:
Check the connection if it has been established with timeout:
1. Create the connection
2. Check if it has been established
3. If yes, then do something
4. If no, for timeout = 5 seconds, exit with error code.

Netcat sounds like a perfect tools for this task:

Quote:
nc -w 5 domain.com port
But the timeout switch takes no effect. I googled and many others reported the same symptom
Script to telnet web server
Please see the attachment, I cant post here, for some reasons the forum complained the code with the "bad request" error

A similar script I wrote before was working, but unfortunately I forgot to keep copy of it. This time, it doesnt work. I must have syntax errors somewhere.
Attached Files
File Type: txt telnet.txt (81 Bytes, 564 views)
__________________
...then the God created man...
Reply With Quote
  #2  
Old December 6th, 2008, 16:23
DutchDaemon's Avatar
DutchDaemon DutchDaemon is offline
Administrator
 
Join Date: Nov 2008
Location: Rotterdam, the Netherlands
Posts: 9,861
Thanks: 30
Thanked 1,896 Times in 1,338 Posts
Default

You could try net/tcping from ports.

Code:
# tcping -t 5 www.google.com 80
www.google.com port 80 open.
# echo $?
0
Code:
# tcping -t 5 www.google.com 90
www.google.com port 90 user timeout.
# echo $?
2
Easy to use in scripts:

Code:
if ( tcping -q -t 5 www.google.com 80 ); then echo alive; else echo dead; fi
alive

if ( tcping -q -t 5 www.google.com 90 ); then echo alive; else echo dead; fi
dead
Don't use 80 as a check port when you're behind a proxy server
Reply With Quote
  #3  
Old December 6th, 2008, 18:12
bsddaemon's Avatar
bsddaemon bsddaemon is offline
Member
 
Join Date: Nov 2008
Location: Melbourne
Posts: 102
Thanks: 13
Thanked 7 Times in 7 Posts
Default

Thanks, but the script will be run in web server, so I cant install any 3rd party application.
__________________
...then the God created man...
Reply With Quote
  #4  
Old December 6th, 2008, 18:27
DutchDaemon's Avatar
DutchDaemon DutchDaemon is offline
Administrator
 
Join Date: Nov 2008
Location: Rotterdam, the Netherlands
Posts: 9,861
Thanks: 30
Thanked 1,896 Times in 1,338 Posts
Default

When you say that the timeout switch has no effect, what do you mean? That you never get your prompt back unless you ^C? Try adding the -o switch. Oh, there's also the -z flag to consider.

Last edited by DutchDaemon; December 6th, 2008 at 18:33.
Reply With Quote
  #5  
Old December 6th, 2008, 18:49
bsddaemon's Avatar
bsddaemon bsddaemon is offline
Member
 
Join Date: Nov 2008
Location: Melbourne
Posts: 102
Thanks: 13
Thanked 7 Times in 7 Posts
Default

Hi DutchDaemon,

My web server running Linux, there is no -o switch for netcat

Let me elaborate a bit more in details. In my case, it takes about 2 seconds to establish a new connection (a deamon with listening port)

I run this command in background mode:

Quote:
$ command to create new connection &
Then I run netcat. Instead of keeping trying to connect for 5 seconds, netcat quits immediately if it fails at the first attempt:

Quote:
nc -zw 5 hostname port
Thats why I said the timeout feature takes no effect.
__________________
...then the God created man...
Reply With Quote
  #6  
Old December 7th, 2008, 14:03
bsddaemon's Avatar
bsddaemon bsddaemon is offline
Member
 
Join Date: Nov 2008
Location: Melbourne
Posts: 102
Thanks: 13
Thanked 7 Times in 7 Posts
Default

I came up with this function:

Code:
repeatCMD () {
    count="0"

    $@
    while [ $? -ne 0 ]; do
        sleep 1
        echo " ===> I try again..."
        count="$(($count + 1))"
        if [ $count -eq 5 ]; then
            echo " ===> ERR: Max number of retry. Quitting..."
            exit 127
        fi
        $@
    done
}
Now I can run the command:

Code:
repeatCMD nc -zw 5 hostname port
It works (doesnt exactly do the same job as timeout, though)

But Im sure you guys have a more elegant solution. Seriously, 12 line of code for the timeout switch doesnt sound right.

And I havent recalled what I have done with the telnet script :S
__________________
...then the God created man...

Last edited by bsddaemon; December 7th, 2008 at 14:22.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FreeBSD 7 under VMWare Server 2 - sanity check hwyhobo Installing & Upgrading 14 September 10th, 2010 14:03
Autodetect sound card (script) soko1 Howtos & FAQs (Moderated) 12 May 6th, 2009 18:46
DVD rip script for mencoder kamikaze Userland Programming & Scripting 0 December 2nd, 2008 10:24
Greasemonkey script to un-narrow forum. hark Feedback 5 November 20th, 2008 20:35
Autodetect network card (script) soko1 Howtos & FAQs (Moderated) 1 November 18th, 2008 11:46


All times are GMT +1. The time now is 21:23.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0