2013-12-18 18 views
1

我正在創建一個簡單的自動腳本。這裏是代碼我在使用expect時需要使用spawn嗎?

#!/bin/sh  
echo "testing expect"  
/usr/bin/expect <<delim 
    expect "password for teamer:" 
    send "itsme\r"  
    expect eof 
delim  
sudo apt-get update 

我指的是各種文件和博客,他們正在使用產卵。所以我有問題:是否有必要每次都使用spawn?我在本地機器上執行更新。

不使用產卵我得到這個錯誤:

send: spawn id exp0 not open while executing 

還是我失去了一些東西?

回答

1

expect的expect命令正在監視等待指定模式的衍生進程的IO通道。如果你不給它一些東西來看,它會坐在那裏直到它超時,然後把密碼發送到什麼都沒有。

這就是你需要做什麼:

#!/bin/sh  
echo "testing expect"  
/usr/bin/expect <<delim 
    exp_internal 1   ;# remove this when your satisfied it's working 
    spawn sudo apt-get update 
    expect "password for teamer:" 
    send "itsme\r"  
    expect eof 
delim 

要小心,你沒有在與定界符分隔符行開頭或結尾的空白。

如果你不關心你的密碼是純文本(你應該),你不需要使用期望:

#!/bin/sh  
echo "itsme" | sudo -S apt-get update 

什麼你應該做的是編輯sudoers文件到讓自己sudo apt-get,而無需提供密碼,然後:

#!/bin/sh  
sudo apt-get update 

讀取系統上的sudoers手冊頁。

相關問題