1、创建一个test.sh,写入:
/usr/bin/expect << EOF
set timeout 10
spawn sudo -s
expect "*密码*"
send "password\r"
expect "#*"
# send "systemctl status AuthorizeServer\r"
# 大括号必须跟在expect (此处有一个空格)后面,如果大括号换行了,执行不了
expect {
"*active (running)*" { send "systemctl stop AuthorizeServer\r" }
"*failed*" { send "systemctl start AuthorizeServer\r" }
"*inactive (dead)" { send "systemctl start AuthorizeServer\r" }
}
# set timeout 3
# send_user "waiting..."
# set timeout 5
expect eof
EOF
2、Dos窗口执行:bash ./test.sh bash后面接test.sh的路径