#!/usr/bin/expect -f
#
# Routing through Internet dialup connection
# An example for expect's flexibility.
#
# This is another sample script. It connects locally from machine
# "al7nj" to "dl6rai" by telnet. Then it dials out to a machine called
# colin.muc.de, which is on the Internet. After logging in there,
# it uses telnet to connect moe.wu3v.net across the Internet, where
# it logs in as DL6RAI.

# --------------------- User configurable ---------------------
set destination "unknown"
set colin_password "contest97"
# --------------------- User configurable ---------------------

set timeout 10
log_user 1

#
# Just for debugging
#

proc syslog {msg} {

# To put something into the log

	global destination
        set filename "$destination script"
        exec logger -p local5.info "$filename (pid=[pid]): $msg"
}

set con_ctl $user_spawn_id
set con_ctl_pid [pid]

# Read the destination from con_ctl. It writes to us something like:
#
#       connect ax25 db0bcc
#
expect timeout {
	syslog "timed out waiting for connect command"
	exit 1
} -re "(connect ax25 .*)\n"
set destination [ lindex $expect_out(0,string) 2 ]

spawn /usr/bin/telnet dl6rai
set call $spawn_id
set call_pid [exp_pid -i $call]

expect timeout {
	syslog "timed out waiting for login: from dl6rai"
	exit
} "ogin: "
send "clx\r"
expect "\\\$"
send "\r"
expect "\\\$"
expect -re ".*"		# clear input buffer
syslog "sucessfully logged in at dl6rai"

send "cu colin\r"
set timeout 60

expect timeout {
		syslog "timed out waiting for login: from colin"
		exit
} "ogin: " 
send "buettneb\r"

expect "word:"
send "$colin_passwort\r"

set timeout 10
expect "\\\$"
send "\r"
expect "\\\$"
expect -re ".*"		# clear input buffer
syslog "sucessfully logged in at colin"

send "telnet moe.wu3v.net\r"
set timeout 30
expect timeout {
		syslog "timed out waiting for login: from moe.wu3v.net"
		exit
} "ogin: " 
send "dl6rai\r"
expect "word:"
send "ben\r"
syslog "sucessfully logged in at moe.wu3v.net"

expect "Bye >"
send "dxc\r"
expect -re "\\\*\\\*\\\* connected to WU3V\n"
syslog "Connected to $destination"

interact "\3" {
		syslog "ctrl-C pressed - exiting"
		exit
	}
syslog "eof rcvd from telnet - exiting"
