[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Help with Expect & Xterm



#!/usr/bin/expect --
# page 294 oriley expect

set send_slow { 1 .001 }
set prompt "(%|#|\\\$)"
set shell $env(SHELL)

#proc open_xterm_shell {} {
#global shell


# in order to talk to an xterm expect must obtain a pty and pass it to the xterm
spawn -pty
# the variable $spawn_out(slave,name) is the descriptor needed by expect
# the first spawn command has an open file descriptor assosiated with it
# where chars are sent to, we need to pass this to xterm, spawn_out(slave,fd)

# the pty must be initialized to raw mode and have echo off
stty raw -echo < $spawn_out(slave,name)

#hack to twart some vendors descision to pad xterm dev name
#sorta like split in other lang
regexp ".*(.)(.)" $spawn_out(slave,name) dummy c1 c2
# if first char is a slash change it to a zero
if {[string compare $c1 "/"] == 0} {
   set c1 "0"
}


# because there is a spawn above waiting to attach use exec
# pass the xterm the fd to read from
exec xterm -S$c1$c2$spawn_out(slave,fd) &

# there are 2 programs with the slave fd open expect and xterm, expect doesnt
# need its open anymore

close -slave

#discard xterm window id

set xterm $spawn_id
set xterm_pty $spawn_out(slave,name)
expect "\n"
spawn "$shell"
#}


#open_xterm_shell
#run a shell
spawn "telnet"
expect "telnet>"
send -s "open localhost\r"
sleep 5
set tries 0
while {$tries < 5} {
   send_user "hello $tries dip shit"
   expect {
"login:" { send -s "mp3\r" } "assword:" { send -s "mp3\r" } "Last login" { break }
   }
   incr tries
}
send_user "login success" interact -u xterm


----------
comments: uh, this doesnt open 3 terminals, i got sick of some strange behavior, it opens one teminal, and is kinda flakey, but it is expect controling and xterm , btw, buy the orielly book, it is fairly good

darrell



mallarj@texas.net wrote:

On Wednesday 13 February 2002  9:46 pm, Cameron Kerr wrote:

On Wed, 13 Feb 2002, Jay Mallar wrote:

spawn telnet <ip here>
sleep 5
send "login <name here>"
sleep 5
send "<pass here>"

Look at the docs (man expect) for log_user, and interact

Starting up three xterms isn't a problem, but automating the logins is..

Is there any reason you can't do it the right way and use ssh, with
ssh-agent?

Cameron Kerr


Thanks Cameron - after another day with the man page, I figured it out - and pointing me to log_user and interact again did some good.

Thanks!






Reply to: