#!name = SSH_PROBE_SCRIPT ######################################################################################## # # Description : # Script connects sends a connect request to a SSH server to see if its alive. # # ACE version : # 1.0+ # # Author : # Chris Greenough (Chris.Greenough@nau.edu) # # Parameters : # [debugFlag] # debug - default 0. Do NOT turn on while multiple probes are configured # # Example config : # probe sshProbe script # script SSH_PROBE [0] # ######################################################################################## #------------------------------------------- # debug procedure # set the EXIT_MSG environment varaible to help debug # also print the debug message when debug flag is on #------------------------------------------- proc ace_debug { msg } { global debug ip port EXIT_MSG set EXIT_MSG $msg if { [ info exists ip ] && [ info exists port ] } { set EXIT_MSG "[ info script ]:$ip:$port: $EXIT_MSG " } if { [ info exists debug ] && $debug } { puts $EXIT_MSG } } #------------------------------------------- # main #------------------------------------------- ace_debug "initializing varaible" set EXIT_MSG "Error config: script SSH_PROBE " set ip $scriptprobe_env(realIP) set port $scriptprobe_env(realPort) ## if port is 0 , use default port 22 if { $port == 0 } { set port 22 } set debug [ lindex $argv 0 ] if { $debug == "" } { set debug 0 } # open connection ace_debug "opening socket" set sock [ socket $ip $port ] ace_debug "setting fconfigure to binary" fconfigure $sock -translation binary ace_debug "sending ssh client hand-shake message" # Sending SSH-2.0-SecureCRT_6.5.0 (build 335) SecureCRT?? set ssh_hello "5353482d322e302d5365637572654352545f362e352e3020286275696c642033333529205365637572654352540d0a" set ssh_length [ string length $ssh_hello ] puts -nonewline $sock [ binary format "H${ssh_length}" $ssh_hello ] flush $sock # read frist 100 bytes from server ace_debug "receiving response" set lines [ read $sock 100 ] # close connection ace_debug "closing socket" close $sock # parsing the 1st 3 bytes from the ssh headers # if it is not a ssh hand shake successful message. failed the probe with exit 30002 # If the server responds with its SSH info then it must be alive set ssh_header "SSH" ace_debug $lines if { ![ binary scan $lines "@0a3" res ] } { ace_debug "probe fail : ssh server response parsing failure" exit 30002 } ace_debug $res if { $res != $ssh_header } { ace_debug "probe fail : ssh hand shake failure with $res !" exit 30002 } # Everything went fine. probe exit with success exit_code 30001 ace_debug "probe success" exit 30001