1. 程式人生 > >expect 普通用戶自動輸入密碼到root下,執行命令

expect 普通用戶自動輸入密碼到root下,執行命令

expect

案例:當前服務器取消了直接使用root登錄服務器,只能使用普通用戶先登錄,然後再su - root 執行root下的命令。

shell腳本如下:

#!/usr/bin/expect -f
set password {root_password}
spawn su - root
expect "密碼:"
send "$password\r"
#send "sh ceshi.sh > temp.log &\r"
#send "timeout 120 tail -f temp.log\r"
send "pwd\r"

send "exit\r"
interact

exit 0

expect 普通用戶自動輸入密碼到root下,執行命令