Tuesday, September 11, 2007

Tcl tcp check script

Here's my script I use for TCP port checking on Cisco ACE.

The probe is simple :

probe scripted 8080-TCP-SCRIPT
port 8080
script tcp_check.tcl


and here's the script :


#Procedures
proc
opensocket {ip port}
{
set sock [socket $ip $port] }

# Get the IP address of the real server from a predefined global array

set
ip $scriptprobe_env(realIP)


# Get port from command line arguments (may be different from real service's one)
if
{$argc <> 1 )
{ set
port $scriptprobe_env(realPort)
}
else

{ set port [lindex $argv 0] }

# Open a socket to the server. This creates a TCP connection to the
# real server
set
EXIT_MSG "opening socket..."

if
{ [catch { opensocket $ip $port } sock] }
{

set EXIT_MSG "$ip:$port ERR : $sock"

set expCode1 "timeout"

set expCode2 "connection timed out"

set expCode3 "connection refused"

set expCode4 "Unknown host"

set expCode5 "o route to host"

set expCode6 "nable to connect"

set expCode7 "host is unreachable"

if { [ regexp $expCode1 $sock ] }
{
set EXIT_MSG "probe fail. $ip:$port ERR : <$expCode1>"
exit 30002
}

if { [ regexp $expCode2 $sock ] }
{
set EXIT_MSG "probe fail. $ip:$port ERR : <$expCode2>"
exit 30002
}

if { [ regexp $expCode3 $sock ] }
{
set EXIT_MSG "probe fail. $ip:$port ERR : <$expCode3>"
exit 30002
}

if { [ regexp $expCode4 $sock ] }
{
set EXIT_MSG "probe fail. $ip:$port ERR : <$expCode4>"
exit 30002
}

if { [ regexp $expCode5 $sock ] }
{
set EXIT_MSG "probe fail. $ip:$port ERR : <$expCode5>"
exit 30002
}

if { [ regexp $expCode6 $sock ] }
{
set EXIT_MSG "probe fail. $ip:$port ERR : <$expCode6>"
exit 30002
}

if { [ regexp $expCode7 $sock ] }
{
set EXIT_MSG "probe fail. $ip:$port ERR : <$expCode7>"
exit 30002
}

} else {

set EXIT_MSG "probe success"

close $sock

exit 30001

}