新手问题 expect 的问题

lzm110 · March 06, 2013 · Last by wangjin replied at June 18, 2015 · 11338 hits

脚本如下: #!/usr/local/bin/expect

解释器声明

set timeout -1

设置超时时间,单位秒

#主机 IP set hostIp [lindex $argv 0]
#IP 的端口
set ipPort [lindex $argv 1]
#用户名 set userName [lindex $argv 2]
#密码 set userPassword [lindex $argv 3]
#远程主机目录 set remoteDir [lindex $argv 4]
#本地主机目录
set localDir [lindex $argv 5]

#scp 拷贝指令 spawn scp -P $ipPort -r $userName@$hostIp:$remoteDir/* $localDir

expect 也是内部命令,作用是监视终端输出是否包含后面的内容,有则执行下面的 send,没有就等待上面设置的 timeout 时间

expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send $userPassword; send "\r"} }

expect { "denied" { send "\r";exit -1;} }

expect eof

开始的时候,没有 expect { "denied" { send "\r";exit -1;} }这段代码,所以没有提示信息。一旦有了这段代码一直提示 send: spawn id exp4 not open。这个问题怎么处理啊?怎么样避免这个提示出现啊?

#!/usr/local/bin/expect

解释器声明

set timeout -1

设置超时时间,单位秒

#主机IP
set hostIp [lindex $argv 0]

#IP的端口

set ipPort [lindex $argv 1]

#用户名
set userName [lindex $argv 2]

#密码
set userPassword [lindex $argv 3]

#远程主机目录 
set remoteDir [lindex $argv 4]

#本地主机目录

set localDir [lindex $argv 5]

#scp拷贝指令
spawn scp -P $ipPort -r $userName@$hostIp:$remoteDir/* $localDir

expect也是内部命令,作用是监视终端输出是否包含后面的内容,有则执行下面的send,没有就等待上面设置的timeout时间

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send $userPassword; send "\r"}
}

expect {
"*denied*" { send "\r";exit -1;}
}

expect eof

开始的时候,没有expect {
"*denied*" { send "\r";exit -1;}
}

这样会好看一下,你要做什么事,这段脚本做什么用的?

#1 楼 @jimrokliu 拷贝数据用的

在密码出错输出 Permission denied (publickey,keyboard-interactive).的时候执行 scp 的那个进程已经死掉了,所以没办法再发送消息了,所以最后的 send "\r"没有进程接收

You need to Sign in before reply, if you don't have an account, please Sign up first.