期望脚本用于远程SSH登录和执行命令 [英] Expect script for remote SSH login and executing commands

查看:95
本文介绍了期望脚本用于远程SSH登录和执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下Expect脚本将SSH远程登录到Raspberry Pi,并正在执行命令:

I am using the following Expect script for remote SSH login to a Raspberry Pi and am executing the commands:

#!/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

问题在于该脚本能够登录到Raspberry Pi,但是在执行"ls -la"命令的行中却没有提到.命令,什么也没发生.如何解决此脚本?我在哪里犯错了?

The problem is that this script is able to log in into the Raspberry Pi, but when it comes to the line for executing the "ls -la" command, nothing happens. How can I fix this script? Where am I making the mistake?

好吧,如果我放

exp_internal 1

exp_internal 1

在脚本中的

行,在匹配失败的地方得到以下输出:

line in my script, I get the following output in the where matching fails:

期望:不会:\ r \ nLinux raspberrypi 3.10.24+#614 PREEMPT Thu Dec 19 20:38:42 GMT 2013 armv6l \ r \ n \ r \ nDebian GNU/Linux系统随附的程序是免费软件; \ r \ n/usr/share/doc/*/copyright中的\ r \ n单个文件中描述了每个程序的确切分发条款.\ r \ n \ r \ nDebian GNU/Linux附带绝对没有担保,\ r \ n适用法律允许的范围.\ r \ n最后登录时间:2014年3月3日,星期一,19:00:11,地址为192.168.1.200 \ r \ r \ n \ u001b] 0; pi @ raspberrypi:〜\ u0007 \ u001b [01; 32mpi @ raspberrypi \ u001b [00m \ u001b [01; 34m〜$ \ u001b [00m"(spawn_id exp6)匹配glob模式"* pi @ raspberrypi〜$ *"?否

expect: does ": \r\nLinux raspberrypi 3.10.24+ #614 PREEMPT Thu Dec 19 20:38:42 GMT 2013 armv6l\r\n\r\nThe programs included with the Debian GNU/Linux system are free software;\r\nthe exact distribution terms for each program are described in the\r\nindividual files in /usr/share/doc/*/copyright.\r\n\r\nDebian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent\r\npermitted by applicable law.\r\nLast login: Mon Mar 3 19:00:11 2014 from 192.168.1.200\r\r\n\u001b]0;pi@raspberrypi: ~\u0007\u001b[01;32mpi@raspberrypi\u001b[00m \u001b[01;34m~ $\u001b[00m " (spawn_id exp6) match glob pattern "*pi@raspberrypi ~ $*"? no

这种匹配不是真的吗?

推荐答案

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

You should be using exp_continue to reenter your Expect loop after the password and authenticity checks are done. Try the below:

#!/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"
    }
}

这是Don Libes从 Exploring Expect 中获得的关于exp_continue的摘录:

This is an extract taken from Exploring Expect regarding exp_continue by Don Libes:

当作为Expect操作执行时,命令exp_continue使控制在当前Expect命令中继续进行. Expect会继续尝试匹配该模式,但是从上次匹配后该模式会中断. Expect有效地重复了它的搜索,就好像再次被调用一样.

When executed as an Expect action, the command exp_continue causes control to be continued inside the current Expect command. Expect continues trying to match the pattern, but from where it left off after the previous match. Expect effectively repeats its search as if it had been invoked again.

这篇关于期望脚本用于远程SSH登录和执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆