我试图在运行期望脚本时动态地传递密码。如何在运行时期望shell脚本中传递参数
脚本看起来有点像这样:
#!/usr/bin/Expect
set server [lindex $argv 0]
send "enter you password"
read Password;
send $password\n;
spawn ssh [email protected]$server ...
而行书期间获得来自终端的口令卡住了。
我试图在运行期望脚本时动态地传递密码。如何在运行时期望shell脚本中传递参数
脚本看起来有点像这样:
#!/usr/bin/Expect
set server [lindex $argv 0]
send "enter you password"
read Password;
send $password\n;
spawn ssh [email protected]$server ...
而行书期间获得来自终端的口令卡住了。
[read]
命令会读取到文件结束,因此它正在等待您关闭终端。使用[gets]
命令:
set password [gets stdin]
而且,你用错了[read]
。第一个参数是要读取的通道ID。请参阅文档的详细信息:
在代码中,你使用下面的代码像puts
声明
send "enter your password"
这是不正确的方法。通常,send
命令将尝试向控制台发送命令,如果通过脚本产生了任何进程,则该命令将被发送到该进程。
无论如何,你会得到打印在控制台上的语句。但是,要意识到这一点。相反,最好使用send_user
命令。
你可以尝试这个
#!/usr/bin/expect
set server [lindex $argv 0]
stty -echo; #Disable echo. To avoid the password to get printed in the terminal
send_user "enter you password : "
# Using regex to grab all the input till user press 'Enter'
# Each submatch will be saved in the the expect_out buffer with the index of 'n,string'
# for the 'n'th submatch string
# expect_out(0,string) will have the whole expect match string including the newline
# The first submatch is nothing but the whole text without newline
# which is saved in the variable 'expect_out(1,string)
expect_user -re "(.*)\n" ;
stty echo; #Enable echo
set pwd $expect_out(1,string)
send $pwd\n;
expect "some-other-statment"
#Your further code here
您可以删除stty -echo
和stty echo
,如果你不操心密码越来越印在控制台
嗨Dinesh,我的意图是从用户动态获取信息。你的代码工作正常,更安全。但是,将其他人称为正确的,因为它完全符合我的条件。感谢您的帮助...... :) – 2014-10-07 08:39:49
@vijayjoshi,更新了我的回答 – Dinesh 2014-10-07 08:41:03
感谢Dinesh的回复。这对我帮助很大 – 2014-10-07 08:43:42
谢谢哥们这个工作精细。 – 2014-10-07 08:41:48