经典一:无交互密码登录并启动进程
#!/usr/local/bin/expect //调用expect命令工具
set timeout 30s
spawn su - hssale //spawn为expect命令,用来启动shell命令, 命令是以衍生子进程的方式来运行的
expect "*assword*" //expect用来等待上个命令进程的反馈,并接收反馈
send "hssale\r" //send命令接收一个字符串参数,并将该参数发送到进程。
expect "*$*" {send "cd /midapps/hssale/bin/\r"} //单一分支(疑问:为什么必须切换到执行文件目录,才能启动该asar_proxy)
expect {
"*#*" {send "echo "good""}
"*$*" {send "/midapps/hssale/bin/asar_proxy\r"}
} //多分支,从上到下匹配,匹配上后跳出,后续不执行
expect eof
exit
解释说明:
#!/usr/local/bin/expect //调用expect命令工具
set timeout 30s
spawn su - hssale //spawn为expect命令,用来启动shell命令, 命令是以衍生子进程的方式来运行的
expect "*assword*" //expect用来等待上个命令进程的反馈,并接收反馈
send "hssale\r" //send命令接收一个字符串参数,并将该参数发送到进程。
expect "*$*" {send "cd /midapps/hssale/bin/\r"} //单一分支(疑问:为什么必须切换到执行文件目录,才能启动该asar_proxy)
expect {
"*#*" {send "echo "good""}
"*$*" {send "/midapps/hssale/bin/asar_proxy\r"}
} //多分支,从上到下匹配,匹配上后跳出,后续不执行
expect eof
exit
解释说明:
send:用于向进程发送字符串 expect:从进程接收字符串 spawn:启动新的进程 interact:允许用户交互
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23890223/viewspace-1663787/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23890223/viewspace-1663787/