期望脚本用于远程ssh登录和执行命令

pra*_*tom 2 ssh expect

我使用以下expect脚本远程ssh登录到raspberry pi并执行命令

#!/usr/bin/expect
set timeout 60
spawn ssh [lindex $argv 1]@[lindex $argv 0]
expect "yes/no" {
    send "yes\r"
    expect "*?assword" { send "[lindex $argv 2]\r" }
    } "*?assword" { send "[lindex $argv 2]\r" }
expect "pi@raspberrypi ~ $ " {
    send "ls -la\r"
    }
interact
Run Code Online (Sandbox Code Playgroud)

问题是这个脚本能够登录到覆盆子但是当涉及执行"ls -la"命令的行时没有任何反应.任何人都可以帮我解决这个脚本吗?我在哪里弄错了?

好的,如果我放

exp_internal 1

在我的脚本中,我在匹配失败的地方得到以下输出

期待:确实是::\ r \nLinux raspberrypi 3.10.24+#614 PREEMPT Thu Dec 19 20:38:42 GMT 2013 armv6l\r \n\r \n Debian GNU/Linux系统附带的程序是免费软件;\r \n每个程序的确切分发术语在/usr/share/doc/*/copyright.\r\n\r.nDebian中的\ r \n个人文件中描述GNU/Linux附带绝对无保证,在某种程度上\ r \n适用法律规定.\ r \n最后登录:2014年3月3日19:00:11从192.168.1.200\r \n\r \n\u001b] 0; pi @ raspberrypi:〜\ u0007\u001b [01; 32mpi @ raspberrypi\u001b [00m\u001b [01; 34m~ $\u001b [00m"(spawn_id exp6)匹配glob模式"*pi @ raspberrypi~ $*"?没有

这种匹配不应该是真的吗?

Tim*_*mah 11

在密码和真实性检查完成后,您应该使用exp_continue重新输入您的expect循环,请尝试以下操作:

#!/usr/bin/expect

set prompt "pi@raspberrypi ~ $ "
spawn ssh [lindex $argv 1]@[lindex $argv 0]                                                         

set timeout 5
expect {
    timeout {
        puts "Connection timed out"
        exit 1
    }

    "yes/no" {
        send "yes\r"
        exp_continue
    }

    "assword:" {
        send -- "[lindex $argv 2]\r"
        exp_continue
    }

    "$prompt" {
        send "ls -la\r"
    }
}
Run Code Online (Sandbox Code Playgroud)

这是Don Libes关于exp_continue的Exploring Expect的摘录:

当作为expect动作执行时,命令exp_continue导致控制在当前expect命令内继续.期望继续尝试匹配模式,但从上一场比赛后的中断位置.期望有效地重复搜索,就像它再次被调用一样.