使用read -s时禁用期望回显密码

时间:2014-06-25 15:17:44

标签: bash tcl expect

我有一个bash脚本,要求使用" read -s"来输入密码。通过期望运行脚本将密码回显给终端。奇怪的是,它可以按照预期的方式运行,每5次运行一次,但大部分时间都没有。有没有可靠的方法来禁用它?

Bash脚本

#!/bin/bash
read -p 'login name? ' name
read -s -p 'password? ' pass
read -p $'\nfavorite color? ' color
echo -e "$name\n$pass\n$color"

期待脚本

#!/usr/bin/expect -f
spawn -noecho ~/tmp/login_test
expect "login name? " {send "gordon\r"}
expect "password? " {
  #stty -echo     -- This doesn't work
  #log_user 0     -- This doesn't work either
  send "gr33d\r"
  #log_user 1
  #stty echo
}
expect "favorite color? " {send "green\r"}
expect eof {exit}

我想要什么

> ~/tmp/login_test
login name? gordon
password? 
favorite color? green
gordon
gr33d
green

我通常得到什么

> ~/tmp/expect_test
login name? gordon
password? gr33d

favorite color? green
gordon
gr33d
green

1 个答案:

答案 0 :(得分:0)

想出来!解决方案是睡1个小时......

#!/usr/bin/expect -f
spawn -noecho ~/tmp/login_test
expect "login name? " {send "gordon\r"}
expect "password? " {sleep .001; send "gr33d\r"}
expect "favorite color? " {send "green\r"}
expect eof {exit}