Expect, How do I have different output for different prompts

Expect, How do I have different output for different prompts

am 02.11.2007 17:57:58 von cozzmo1

Perhaps someone could lend some assistance,
Here is my script.
I would like to do an "if" within the expect that will allow a
different response based on the prompt.


#!/usr/local/bin/expect -f
send_user "\n"

set force_conservative 0 ;# set to 1 to force conservative mode even
if
;# script wasn't run conservatively
originally



if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}


set password [lindex $argv 1]

set hostname [lindex $argv 0]
set timeout -1
set send_human {.1 .3 1 .05 2}
spawn bash
match_max 100000

##ssh to the device
expect -exact "\$ "
send -- "ssh $hostname\r"

########
##Here is where the prob lies, sometimes it asks
##"Are you sure you want to continue connecting(yes/no)?" #(I want to
reply "yes")
##other times it just asks "password:" #in this case the script
works.
########
##apply PW
expect -exact "\password:"
send "$password"
send -- "\r"
interact

Re: Expect, How do I have different output for different prompts

am 02.11.2007 18:30:22 von Glenn Jackman

[followup set]

At 2007-11-02 12:57PM, "cozzmo1@hotmail.com" wrote:
> Perhaps someone could lend some assistance,
> Here is my script.
> I would like to do an "if" within the expect that will allow a
> different response based on the prompt.
>
>
> #!/usr/local/bin/expect -f
> send_user "\n"
>
> set force_conservative 0 ;# set to 1 to force conservative mode even
> if
> ;# script wasn't run conservatively
> originally
>
>
>
> if {$force_conservative} {
> set send_slow {1 .1}
> proc send {ignore arg} {
> sleep .1
> exp_send -s -- $arg
> }
> }
>
>
> set password [lindex $argv 1]
>
> set hostname [lindex $argv 0]
> set timeout -1
> set send_human {.1 .3 1 .05 2}
> spawn bash
> match_max 100000
>
> ##ssh to the device
> expect -exact "\$ "
> send -- "ssh $hostname\r"
>
> ########
> ##Here is where the prob lies, sometimes it asks
> ##"Are you sure you want to continue connecting(yes/no)?" #(I want to
> reply "yes")
> ##other times it just asks "password:" #in this case the script
> works.
> ########
> ##apply PW
> expect -exact "\password:"
> send "$password"
> send -- "\r"
> interact

You can use a "compound" expect statement:

expect {
"Are you sure you want to continue connecting(yes/no)?" {
exp_send "yes\r"
exp_continue
# will continue to look for the password prompt
}
"password:" {
exp_send -- "$password\r"
# the expect command will now return
}
}
interact

Are you aware that you don't have to spawn a bash shell first? You can
spawn ssh $hostname

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Re: Expect, How do I have different output for different prompts

am 02.11.2007 18:46:22 von Kenan Kalajdzic

In comp.unix.shell cozzmo1@hotmail.com wrote:
> Perhaps someone could lend some assistance,
> Here is my script.
> I would like to do an "if" within the expect that will allow a
> different response based on the prompt.
>
> #!/usr/local/bin/expect -f
> send_user "\n"
>
> set force_conservative 0 ;# set to 1 to force conservative mode even
> if
> ;# script wasn't run conservatively
> originally
>
> if {$force_conservative} {
> set send_slow {1 .1}
> proc send {ignore arg} {
> sleep .1
> exp_send -s -- $arg
> }
> }
>
> set password [lindex $argv 1]
>
> set hostname [lindex $argv 0]
> set timeout -1
> set send_human {.1 .3 1 .05 2}
> spawn bash
> match_max 100000
>
> ##ssh to the device
> expect -exact "\$ "
> send -- "ssh $hostname\r"

Change this line to

send -- "ssh -o 'StrictHostKeyChecking no' $hostname\r"

in order to get rid of the extra question and also get rid of a nice
security feature.

> ########
> ##Here is where the prob lies, sometimes it asks
> ##"Are you sure you want to continue connecting(yes/no)?" #(I want to
> reply "yes")

Since you always want to reply with "yes", disabling strict host key
checking as shown above will do it.

> ##other times it just asks "password:" #in this case the script
> works.
> ########
> ##apply PW
> expect -exact "\password:"
> send "$password"
> send -- "\r"
> interact
>

--
Kenan Kalajdzic

Re: Expect, How do I have different output for different prompts

am 02.11.2007 19:12:16 von avgjoe

On Nov 2, 12:57 pm, cozz...@hotmail.com wrote:
> Perhaps someone could lend some assistance,
> Here is my script.
> I would like to do an "if" within the expect that will allow a
> different response based on the prompt.
>
> #!/usr/local/bin/expect -f
> send_user "\n"
>
> set force_conservative 0 ;# set to 1 to force conservative mode even
> if
> ;# script wasn't run conservatively
> originally
>
> if {$force_conservative} {
> set send_slow {1 .1}
> proc send {ignore arg} {
> sleep .1
> exp_send -s -- $arg
> }
>
> }
>
> set password [lindex $argv 1]
>
> set hostname [lindex $argv 0]
> set timeout -1
> set send_human {.1 .3 1 .05 2}
> spawn bash
> match_max 100000
>
> ##ssh to the device
> expect -exact "\$ "
> send -- "ssh $hostname\r"
>
> ########
> ##Here is where the prob lies, sometimes it asks
> ##"Are you sure you want to continue connecting(yes/no)?" #(I want to
> reply "yes")
> ##other times it just asks "password:" #in this case the script
> works.
> ########
> ##apply PW
> expect -exact "\password:"
> send "$password"
> send -- "\r"
> interact

You could move the users .../.ssh/known_hosts file out of the way and
put it back at the end of the script or zero it out completly.
Then you would be prompted for a yes every time.