2016-04-20 95 views
0

這是最終目標:在提示時使用expect腳本執行mysql命令以發送密碼。總共有期望腳本不能正確發送密碼

  1. test.sh 3個文件
  2. test1.sh
  3. test.exp來

這裏是代碼:

test.sh(返回需要連接mysql數據庫的密碼)

grep hibernate.connection.password /opt/cisco/cpam/properties/vx.hibernate.properties | sed 's/hibernate.connection.password=//g' 

test1.sh(需要被excecuted MySQL命令)

mysql -u cpam -p --protocol=tcp vxdb < /opt/cisco/cpam/import/MySQL_Views.sql 

test.exp來

(即在執行MySQL的命令發送密碼expect腳本)
set password [exec ./test.sh] 
exec ./test1.sh 
expect "Enter password: " 
send "$password\r" 
expect eof 

運行expect test.exp的結果是我只看到提示符:

Enter password: 

任何專家都可以幫我看看發生了什麼問題嗎?這肯定意味着test1.sh已經成功運行了,並且我也嘗試了很多次,包括使用靜態密碼。仍然只是提示。

非常感謝!

回答

1

要通過expectsend與程序進行交互,您需要使用spawn而不是exec啓動它。試試這個:

set password [exec ./test.sh] 
spawn ./test1.sh 
expect "Enter password: " 
send "$password\r" 
expect eof 
+0

我感覺非常愚蠢..謝謝 – Tony