Domanda

Ho uno script di aspettarsi che si connette a un paio di router tramite ssh. Tutti questi router hanno la stessa password (lo so, è sbagliato), e lo script ha bisogno di sapere che la password in modo da essere in grado di connettersi ai router. Attualmente, la password viene passato al mio script come argomento sulla linea di comando, ma questo significa che c'è una traccia di quella password nel mio file .bash_history così come nei processi in esecuzione. Così, invece vorrei che l'utente venga richiesta una password, se possibile, in silenzio.

Sai se sia o non è possibile richiedere all'utente una password con aspettarsi?

Grazie.

Modifica : se fossi la connessione ai server, invece di router, avrei probabilmente usare le chiavi SSH al posto di password. Ma i router che sto usando solo supportano le password.

È stato utile?

Soluzione

Usa si aspettano comando stty in questo modo:

# grab the password
stty -echo
send_user -- "Password for $user@$host: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)

#... later
send -- "$pass\r"

Si noti che è importante chiamare stty -echo prima send_user chiama - non sono sicuro esattamente perché: Penso che sia un problema di temporizzazione

.

si aspettano i programmatori dovrebbero leggere tutti i il libro: Exploring aspettarsi da Don Libes

Altri suggerimenti

OK, la fusione delle 2 risposte di cui sopra (o al di sotto o ovunque si trovino ora!):

#!/usr/bin/expect
log_user 0
set timeout 10
set userid  "XXXXX"
set pass    "XXXXXX"

### Get two arguments - (1) Host (2) Command to be executed
set host    [lindex $argv 0] 
set command [lindex $argv 1]

# grab the password
stty -echo
send_user -- "Password for $userid@$host: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)

spawn /usr/bin/ssh -l $userid $host
match_max [expr 32 * 1024]

expect {
    -re "RSA key fingerprint" {send "yes\r"}
    timeout {puts "Host is known"}
}

expect {
     -re "username: " {send "$userid\r"} 
     -re "(P|p)assword: " {send "$pass\r"}
     -re "Warning:" {send "$pass\r"}
     -re "Connection refused" {puts "Host error -> $expect_out(buffer)";exit}
     -re "Connection closed"  {puts "Host error -> $expect_out(buffer)";exit}
     -re "no address.*" {puts "Host error -> $expect_out(buffer)";exit}

     timeout {puts "Timeout error. Is host down or unreachable?? ssh_expect";exit}
}

expect {
   -re "\[#>]$" {send "term len 0\r"}
   timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}
}


expect {
   -re "\[#>]$" {send "$command\r"}

   timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}
}

expect -re "\[#>]$"
set output $expect_out(buffer)
send "exit\r"
puts "$output\r\n"

Si noti che ho cambiato la variabile $ password per $ passare per essere coerente con l'altra risposta.

In alternativa si potrebbe lasciare che ssh raccogliere le password tramite X11 usando la variabile d'ambiente SSH_ASKPASS.

Dalla pagina man:

> SSH_ASKPASS
>     If ssh needs a passphrase, it will read the passphrase from the
>     current terminal if it was run from a terminal.  If ssh does not
>     have a terminal associated with it but DISPLAY and SSH_ASKPASS
>     are set, it will execute the program specified by SSH_ASKPASS
>     and open an X11 window to read the passphrase.  This is particularly
>     useful when calling ssh from a .xsession or related script.
>     (Note that on some machines it may be necessary to redirect the
>     input from /dev/null to make this work.)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top