#!/usr/local/bin/expect #Check to see if a web site is up by sending the HTTP command GET #to HTTP port of the web server; its exit status is captured by #ckweb.ksh, which explains what happens. This script is better #called by companion script ckweb.ksh. #http://yong321.freeshell.org/freeware/ckweb.html #(C) Copyright 2000,2003 Yong Huang (yong321@yahoo.com) #proxy servers we're using (see Appendix of above URL for tips #to find your proxy server names) set proxy1 "myproxy1" set proxy2 "myproxy2" proc doproxy {proxysvr proxyport} { global url port ret puts *********************************************** spawn /usr/bin/telnet $proxysvr $proxyport expect_before eof { return 1 } expect "Trying " set timeout 10 ;#10 seconds should be enough expect "Connected to " expect "Escape character is '^]'." send "GET http://$url:$port HTTP/1.0\r\r" set timeout 30 ;#$ret could contain | so use -re expect -re "HTTP/1.\[10] $ret*" { return 0 } timeout { return 2 } } #Main Logic #Based on # of args, choose direct connection or via proxy #args are url, port, expectedreturncode in that order #url string should contain no port (i.e. not like "www.abc.com:8000") if {$argc == 3} { ;##########Directly connect to URL (no proxy)########## set url [lindex $argv 0] set slash [string first "/" $url] if {$slash == -1} { ;#url like "www.abc.com" set svr $url set page "/" ;#use "/" in GET command in this case } else { ;#even if url like "www.abc.com/", page will be "/" set svr [string range $url 0 [incr slash -1]] set page [string range $url [incr slash] "end"] } set port [lindex $argv 1] ;#Web server port (usually 80) ;#HTTP return code, usually one number like 200 but could be like ;#200,401 meaning either 200 or 401 is acceptable set ret [join [split [lindex $argv 2] ,] |] puts *********************************************** spawn /usr/bin/telnet $svr $port expect_before eof { exit 1 } expect "Trying " set timeout 45 ;#give more time for initial connection expect "Connected to " expect "Escape character is '^]'." send "GET $page HTTP/1.1\r" send "Host: $svr\r\r" set timeout 30 expect "HTTP/1.\[10] $ret*" { exit 0 } timeout { exit 2 } } elseif {[lindex $argv 3] == "P"} { ;##########By way of proxy to connect to an outside URL########## set url [lindex $argv 0] set port [lindex $argv 1] set ret [join [split [lindex $argv 2] ,] |] ;#try first proxy server Unocal is using and its port set doproxyret [doproxy $proxy1 8080] if {$doproxyret == 1 || $doproxyret == 2} { ;#try another proxy server set doproxyret [doproxy $proxy2 8080] } exit $doproxyret }