كيف يمكنني إنشاء برنامج نصي متوقع يطالب بكلمة المرور؟

StackOverflow https://stackoverflow.com/questions/681928

  •  22-08-2019
  •  | 
  •  

سؤال

لدي برنامج نصي متوقع يتصل ببعض أجهزة التوجيه عبر ssh.جميع أجهزة التوجيه هذه لها نفس كلمة المرور (أعلم أنها خاطئة)، ويحتاج البرنامج النصي إلى معرفة كلمة المرور هذه حتى يتمكن من الاتصال بأجهزة التوجيه.حاليًا، يتم تمرير كلمة المرور إلى البرنامج النصي الخاص بي كوسيطة في سطر الأوامر، ولكن هذا يعني أن هناك أثرًا لكلمة المرور هذه في ملف .bash_history الخاص بي وكذلك في العمليات الجارية.لذا، بدلاً من ذلك، أود أن تتم مطالبة المستخدم بإدخال كلمة المرور، بصمت إن أمكن.

هل تعرف ما إذا كان من الممكن مطالبة المستخدم بكلمة مرور مع توقع أم لا؟

شكرًا لك.

يحرر:إذا كنت متصلاً بالخوادم بدلاً من أجهزة التوجيه، فمن المحتمل أن أستخدم مفاتيح ssh بدلاً من كلمات المرور.لكن أجهزة التوجيه التي أستخدمها تدعم كلمات المرور فقط.

هل كانت مفيدة؟

المحلول

استخدم توقع stty أمر مثل هذا:

# 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"

لاحظ أنه من المهم الاتصال stty -echo قبل الاتصال send_user - لست متأكدًا تمامًا من السبب:أعتقد أنها مسألة توقيت.

نتوقع أن المبرمجين يجب أن يقرأوا جميعا ال كتاب:استكشاف توقع من قبل دون Libes

نصائح أخرى

وOK، دمج 2 إجابات فوق (أو أقل أو أينما كانوا الآن!):

#!/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"

ملحوظة التي غيرت المتغير $ كلمة المرور إلى $ تمر لتكون متسقة مع الإجابة الأخرى.

وبدلا من ذلك هل يمكن أن تسمح سه جمع كلمة المرور عبر X11 باستخدام متغير البيئة SSH_ASKPASS.

ومن صفحة الرجل:

> 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.)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top