my first expect script - updated - 11 5

my first expect script - http.exp is updated.
It now supports to specify port number as its argument.

Usage: http.exp <hostname or IP> [ <port number> ]
( if <port number> is not specified, it assumes 80 as port number. )
#!/usr/local/bin/expect --
# created by Song, Minsuk <dnd@neowiz.com>
# a simple http service check script
# ver 1.3 2000/11/04	supports to specify port number
# ver 1.4 2000/11/05	uses lindex instead of lrange ;-)
#

if { $argc < 1 } {
	send_user "usage: $argv0 <hostname or IP> \[ <port number> \]\n";
	exit 1;
}

set host [lindex $argv 0]
if { $argc < 2 } {
	set port 80
} else {
	set port [lindex $argv 1]
}

# is host alive ?
spawn ping $host

set timeout 3
expect {
	"is alive" {
		exp_continue;
	}
	"unknown host" {
		send_user "Unknown host. |nohost"
		exit 2
	}
	timeout {
		send_user "no answer from $host |down"
		exit 1
	}
}
		
# is http port alive ?
spawn telnet $host $port

set timeout 3
expect {
	"Escape character is" {
		send "HEAD / HTTP/1.0\n\n";
	}
	"Connection refused" {
		send_user "Port $port Connection refused. |noservice"
		exit 2
	}
	"Unknown" {
		send_user "Unknown host. |nohost"
		exit 2
	}
	timeout {
		send_user "Ping is ok, but no response from port $port. |crit";
		exit 2
	}
}

# port is opened! now check response is good.
set timeout 10
expect {
	"HTTP/1.1" {
		send_user "HTTP Ok |ok"
	}
	"Connection closed" {
		send_user "Empty response. |crit";
	}
	timeout {
		send_user "Socket timeout after 10 seconds. |crit";
		exit 1
	}
}


Sunday, 05-Nov-2000 14:00:46 KST