2014-10-07 103 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