#! /usr/bin/expect
set command [lindex $argv 0]
set passwd [lindex $argv 1]
spawn su - root -c $command
expect "password:|口令:"
send $passwd
expect eof
exit
#! /usr/bin/expect
set passwd [ lindex $argv 0 ]
set shell_path [ lindex $argv 1 ]
spawn su - root
expect "口令:|Password:"
send $passwd
expect {
"su*" {
#expect eof
#exit 24
#send "echo password error
"
expect eof
exit 10
}
"*#*" {
#send "echo login succ
"
#expect eof
#exit 11
send "echo
"
send "$shell_path
"
}
}
expect eof
#exit
例子:$argv 0 等是脚本传参数
#! /usr/bin/expect
set user_passwd [ lindex $argv 0 ]
set user_ip [ lindex $argv 1 ]
set user_path [ lindex $argv 2 ]
spawn scp ../config.xml user@$user_ip:$user_path/user/
expect "?*"
send $user_passwd
expect eof