赞
踩
expect的作用不再累述
下面是一个简单的shell调用expect脚本的例子
作用是从一个普通用户切换到root用户并做相关操作(删除root家目录下的myfile),然后切换回来
shell 脚本: test.sh
#!/bin/sh
echo "test expect"
User=root
Passwd=mypass
File=myfile
./my.exp $User $Passwd $File
echo "test end"
expect 脚本:my.exp
#!/usr/bin/expect -f
set User [lindex $argv 0]
set Passwd [lindex $argv 1]
set File [lindex $argv 2]
spawn su - $User
expect "*password*"
send $Passwd
send "\n"
expect "*$"
send "rm -rf $File"
send "\n"
expect "*$"
send "exit\n"
expect eof
exit
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。