1

私の Expect スクリプトは、クリア テキストでパスワード/ユーザーを表示し、それを非表示にしたいと考えています。

#!/usr/local/bin/expect
###########################################################################################    ############
# Input: It will handle two arguments -> a device and a show command.
###########################################################################################    ############
# ######### Start of Script ######################
# #### Set up Timeouts - Debugging Variables
log_user 0
set timeout 10
set userid  "USER"
set password  "PASS"
# ############## Get two arguments - (1) Device (2) Command to be executed
set device  [lindex $argv 0] 
set command [lindex $argv 1]
spawn /usr/local/bin/ssh -l $userid $device
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 "$password\r"}
     -re "Warning:" {send "$password\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 device 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"
4

5 に答える 5

2

-OUseBatchMode=Yes...そして、キーに問題がある場合、パスワードモードにフォールバックしてハングするだけでなく(インタラクティブに実行しているため)、sshがすぐに失敗するように追加します(終了コードを確認できます)。

于 2010-06-08T22:45:47.137 に答える
1

同じ問題は、どのスクリプト言語にも存在します。パスワードがわからない場合、スクリプトはパスワードを入力できません...最も簡単な解決策は、キーを使用してパスワードなしのsshを使用することです。

于 2010-06-08T15:32:06.573 に答える
0

ここで Glenn の回答を見てください: How can I make an expect script prompt for a password?

私は同じ種類のことをやろうとしていて、これが便利だと思いました。

于 2010-06-09T16:20:34.287 に答える
0

以下は、ユーザー名とパスワードの入力を求める予期されるコードです。

send_user "Username?\ "
expect_user -re "(.*)\n"
set user $expect_out(1,string)

send_user "password?\ "
stty -echo
expect_user -re "(.*)\n"
stty echo
set password $expect_out(1,string)

または、複数回実行する必要があり、毎回パスワードを再入力したくない場合は、パスワードをホームディレクトリのファイルに制限されたアクセス許可 (0400) で保存し、そこからパスワードを読み取ることができます。その後、不要になったらパスワード ファイルを削除します。

于 2015-04-16T00:36:42.493 に答える