2014-10-07 98 views

回答

1

在代码中,你使用下面的代码像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 -echostty echo,如果你不操心密码越来越印在控制台

参考:http://www.tcl.tk/man/expect5.31/expect.1.html

+0

嗨Dinesh,我的意图是从用户动态获取信息。你的代码工作正常,更安全。但是,将其他人称为正确的,因为它完全符合我的条件。感谢您的帮助...... :) – 2014-10-07 08:39:49

+0

@vijayjoshi,更新了我的回答 – Dinesh 2014-10-07 08:41:03

+0

感谢Dinesh的回复。这对我帮助很大 – 2014-10-07 08:43:42