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