# get-linksys-addr.ksc
# This script can be used with a Linksys Ethernet Cable/DSL Router 
# to retrieve the IP address for use with Kerberos 5 authentication
# when Network Address Translation (NAT) is enabled.
# 
#  by Frank da Cruz and Jeffrey Altman
#
# Version 1.0

if < \v(version) 800200 {
  end 99 This script requires C-Kermit or Kermit 95 version 800200 or higher
}

# define some default 
local firewall fwuser fwpwd tempfile \%x addr
define firewall 192.168.1.1             ; default value
define fwuser                           ; default value
define fwpwd    admin                   ; default value
define tempfile \v(tempdir)linksys.htm

# Perform HTTP GET and place the Status HTML page into the tempfile
http open \m(firewall)
if failure end 1 Unable to connect to firewall
http /user:\m(fwuser) /password:\m(fwpwd) get /Status.htm \m(tempfile)
if failure end 2 Unable to access Status.htm: \v(http_code): \v(http_message)
http close

# Read the contents of the tempfile into the data variable
file open /binary /read \%x \m(tempfile)
if failure end 3 FOPEN \m(tempfile): \f_errmsg()
file read /size:\fsize(\m(tempfile)) \%x data
file close \%x

# Delete the tempfile
delete \m(tempfile)

# The IP Address of the Router is located in the HTML file
# within a block defined by tags: <!--WAN head--> and <!--WAN tail-->
# We extract the substr defined by the block
.\%x := \findex(<!--WAN head-->,\m(data))
if not \%x end 4 Header <!--WAN head--> not found
.\%y := \findex(<!--WAN tail-->,\m(data),\%x)
if not \%y end 5 Header <!--WAN tail--> not found
.data := \fsubstr(\m(data),\%x,\%y-\%x+15)

# The IP Address is located after the string "IP Address:".
# Find its location in the WAN block
.\%x := \findex(IP Address:,\m(data))
if not \%x end 6 IP Address tag not found

# Extract the IP Address
.addr := \fipaddress(\m(data),\%x)
if failure end 7 No ip address found

# Set the IP address of the Router to be used in Kerberos 5 tickets
set auth k5 addresses \m(addr)

# Done
end 0 Kerberos 5 address list set to: \m(addr)
