PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Expect, How do I have different output for different prompts
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Expect, How do I have different output for different prompts

Réponse
 
LinkBack Outils de la discussion
Vieux 02/11/2007, 17h57   #1
cozzmo1@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Expect, How do I have different output for different prompts

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

  Réponse avec citation
Vieux 02/11/2007, 18h30   #2
Glenn Jackman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Expect, How do I have different output for different prompts

[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
  Réponse avec citation
Vieux 02/11/2007, 18h46   #3
Kenan Kalajdzic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Expect, How do I have different output for different prompts

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
  Réponse avec citation
Vieux 02/11/2007, 19h12   #4
avgjoe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Expect, How do I have different output for different prompts

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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h56.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14644 seconds with 12 queries