Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Passwords 如何使expect脚本提示输入密码?_Passwords_Prompt_Expect - Fatal编程技术网

Passwords 如何使expect脚本提示输入密码?

Passwords 如何使expect脚本提示输入密码?,passwords,prompt,expect,Passwords,Prompt,Expect,我有一个expect脚本,它通过ssh连接到一些路由器。所有这些路由器都有相同的密码(我知道,这是错误的),脚本需要知道该密码才能连接到路由器。目前,密码作为命令行上的参数传递给我的脚本,但这意味着在我的.bash_历史文件以及正在运行的进程中都有该密码的跟踪。因此,如果可能的话,我希望提示用户输入密码 您知道是否可以使用expect提示用户输入密码吗 多谢各位 编辑:如果我连接到服务器而不是路由器,我可能会使用ssh密钥而不是密码。但是我使用的路由器只支持密码。使用expect的stty命令如

我有一个expect脚本,它通过ssh连接到一些路由器。所有这些路由器都有相同的密码(我知道,这是错误的),脚本需要知道该密码才能连接到路由器。目前,密码作为命令行上的参数传递给我的脚本,但这意味着在我的.bash_历史文件以及正在运行的进程中都有该密码的跟踪。因此,如果可能的话,我希望提示用户输入密码

您知道是否可以使用expect提示用户输入密码吗

多谢各位


编辑:如果我连接到服务器而不是路由器,我可能会使用ssh密钥而不是密码。但是我使用的路由器只支持密码。

使用expect的
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"
请注意,在调用
send\u user
之前调用
stty-echo
非常重要——我不确定确切原因:我认为这是一个时间问题


expect程序员都应该阅读这本书:探索Don Libes的expect

好的,合并上面(或下面,或现在的任何地方)的两个答案:


请注意,我将$password变量更改为$pass以与另一个答案一致。

或者,您可以让ssh使用ssh\u ASKPASS环境变量通过X11收集密码

从手册页:

> 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.)

很酷,谢谢!令人惊讶的是,这种语言是多么的不宽容:我写了“set pass$expect_out(1,string)”,在单词“string”之前加了一个空格,然后它爆炸了。我也找不到太多的文档。我希望有别的解决办法,而不是期望的。无论如何,非常感谢。“找不到太多文档”?!?有一整本书,一般认为写得很好,不需要第二版。说真的,看看那本书吧。Re:1和string之间的空格--Tcl(因此expect)没有多维数组。数组键只是一个字符串,“1,string”和“1,string”是不同的。“ssty-echo”是这样的,您输入的密码不会从cli中回显。它模拟的是ssh的行为w/out expect。首先,大多数人不会从/usr/local/bin运行expect,而从/usr/local/bin运行ssh的人更少。文件顶部的设置密码不起任何作用。$user@$host的send_user语句有未定义的变量,这会导致我的expect解释器失败。而且大多数人ssh到一个“主机”而不是一个“设备”,很高兴看到这个答案得到修复,如果没有,我想我可以提供一个工作版本。
> 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.)