2017-04-04 46 views
-1

我有一個Windows應用程序,需要用戶名和密碼啓動,但我不希望用戶輸入用戶名/密碼,所以我創建了一個腳本來與CMD界面進行交互輸入密碼,在Windows 8.1上使用cygwin,但我無法在Cygwin上找到expect命令,並且我還想問我的腳本是否有任何問題。下面 是腳本我想用腳本運行應用程序

#!/usr/bin/expect 
sh C:\Users\Osama.Barakat\Desktop\batchfile.bat 
expect "Enter the password for administrator:" 
send "Pa$$w0rd" 
interact 
+1

我建議編輯您的問題的標題:恕我直言「如何結合批處理文件和期望腳本在cygwin?」或類似的東西會更精確。 – Scheff

回答

0

我確定你的問題中的兩個問題:

  1. 如何在Cygwin的/ bash中執行批處理文件?

  2. 如何獲得期望安裝/運行在cygwin/bash?

1.如何在cygwin/bash中執行批處理文件?

在示例代碼中,我發現

---8<--- 
sh C:\Users\Osama.Barakat\Desktop\batchfile.bat 
--->8--- 

恕我直言,這是一個錯誤。我相信bash將嘗試執行任何腳本,無論腳本文件擴展名如何。但是,如果batchfile.bat確實是批處理文件,則應該使用正確的解釋器(即cmd.exe)調用它。

我試過看看cygwin bash有沒有問題,但是它工作正常。我的樣品批處理文件test-expect.bat

@echo off 
rem output 
echo Hello %USERNAME% 
rem input 
set /p INPUT= Enter input: 
rem output 
echo Input: %INPUT% 

...在Cygwin上在bash測試:

$ cmd /c test-expect.bat 
Hello Scheff 
Enter input: Hello cmd 
Input: Hello cmd 

$ 

OK,完成。

2.如何獲得在cygwin/bash中安裝/運行的期望?

在cygwin上安裝expect我用google搜索了一下,發現Installation of Cygwin, expect and Ssh。於是,我開始將cygwin-設置setup-x86.exe和嘗試別的東西:

在「選擇包」頁面,我設置ViewFullSearchexpect。我只列出了四個條目,第一條是:「expect:自動交互式應用程序的工具」(類別:Tcl)。我選擇了這個安裝,並且必須確認另一個包是否依賴。

安裝完畢後,我在一個交互式的bash嘗試這樣做:

$ which expect 
/usr/bin/expect 

$ 

OK - 看起來相當不錯。

結合CMD。EXE和期待

把所有在一起,我寫了另一個測試腳本test-expect.exp

#!/usr/bin/expect 
spawn cmd /c test-expect.bat 
expect "Enter input: " 
send "Hello expect\r" 
interact 

測試在bash在Cygwin上:

$ ./test-expect.exp 
spawn cmd /c test-expect.bat 
Hello Scheff 
Enter input: Hello expect 
Input: Hello expect 

$ 

那麼,它似乎工作。

相關問題