如何使期望脚本提示输入密码? [英] How can I make an expect script prompt for a password?

查看:92
本文介绍了如何使期望脚本提示输入密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an expect script that connects to a few routers through ssh. All these routers have the same password (I know, it's wrong), and the script needs to know that password in order to be able to connect to the routers. Currently, the password is passed to my script as an argument on the command line, but this means that there's a trace of that password in my .bash_history file as well as in the running processes. So instead I would like the user to be prompted for a password, if possible silently.

您知道是否可以提示用户输入带有期望的密码?

Do you know whether or not it's possible to prompt the user for a password with expect?

谢谢.

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

Edit: if I was connecting to servers instead of routers, I would probably use ssh keys instead of passwords. But the routers I'm using just support passwords.

推荐答案

像下面这样使用Expect的stty命令:

Use expect's stty command like this:

# 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_user之前先呼叫stty -echo 很重要-我不确定为什么:我认为这是一个计时问题.

Note that it's important to call stty -echo before calling send_user -- I'm not sure exactly why: I think it's a timing issue.

期望程序员都应该阅读 这本书:唐·里伯斯(Don Libes)探索的期望

expect programmers should all read the book: Exploring Expect by Don Libes

这篇关于如何使期望脚本提示输入密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆