암호에 대한 기대 스크립트 프롬프트를 어떻게 만들 수 있습니까?
문제
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
- 정확히 왜 그런지 잘 모르겠습니다. 타이밍 문제라고 생각합니다.
프로그래머는 모두 읽어야합니다 그만큼 책 : Don Libes의 기대 탐구
다른 팁
좋아, 위의 두 가지 답변을 병합 (현재 또는 현재 어디에 있든) : :
#!/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"
$ password 변수를 다른 답변과 일치하도록 $ pass로 변경했습니다.
또는 SSH_ASKPass 환경 변수를 사용하여 SSH가 X11을 통해 비밀번호를 수집 할 수 있습니다.
Man Page에서 :
> 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.)
제휴하지 않습니다 StackOverflow