2011-09-16 148 views
-1

天啊!我已經嘗試了一切有關預計。我想要做的是創建一個腳本,以便在終端中更改爲root時自動輸入密碼。我的代碼看起來像這樣Shell腳本終端中的自動化

expect -i 
spawn sudo pmset schedule sleep "{query}" expect { Password: send "password\r" }; interact;  
exit 

結果

expect1.1> spawn sudo pmset schedule sleep "06/19/11 23:40:00" expect { Password: echo "lovegun\r" }; interact; exit 
spawn sudo pmset schedule sleep 06/19/11 23:40:00 expect Password: send "password\r" 
Password: 

我被困在密碼: 有人可以看看我的代碼?這有什麼問題。

expect -c ' 
    proc abort {} { 
    puts "You have Authenticated\n" 
    exit 1 
    } 
    spawn sudo pmset schedule sleep "{query}" 
    expect { 
    Password:  { send "password\n" } 
    default   abort 
    } 
    puts "Finished OK\n" 
' 

最後我找到了答案

+0

'發送「密碼\ n」'不起作用。我現在正在猜測,所以我刪除了我的不準確的答案,你需要使用'puts'$ passwd \ n「',你看到http://stackoverflow.com/questions/5575702/help-with-expect -in-a-bash-script在你的研究中,看起來很有前途......另外,爲你的shell添加一個標籤,bash?你會對你的問題有更多的關注。祝你好運。 – shellter

+0

謝謝,它的工作原理。 :d – Lunayo

回答

2

如果你打算將密碼埋在一個expect腳本,你可以:1)改變pmset到讓你不運行設置用戶ID必須使用sudo,或者2)讓自己的NOPASSWD訪問sudo,因此您不需要輸入密碼。無論哪種方式會讓你不必將密碼存儲在一個文件中。