Linux期望发送奇怪的构造 [英] Linux expect send weird constructions

查看:79
本文介绍了Linux期望发送奇怪的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚期望如何工作. AFAIK期望脚本由期望"和发送"语句组成.因此,对于屏幕上出现的每个适当的期望"语句,都会调用发送"语句.同样,命令交互"意味着控制权被传回给用户,并且他能够与终端进行交互.如果我错了,请纠正我.这两个语句就像一个咒语.

I am trying to figure out how expect working. AFAIK the expect script consists of "expect" and "send" statements. So for each approporiate "expect" statement which appears on screen the "send" statement is called. Also the command "interact" means that controlling is passed back to user and he is able to interact with the terminal. Correct me if I am wrong. Those two statements works like a charm.

第一:

#!/usr/bin/expect
spawn ssh -q localhost;

# Handles following message: "Are you sure you want to continue connecting (yes/no)?"
expect "yes";
send "yes\r";
interact;

第二:

#!/usr/bin/expect
spawn ssh -q localhost;

# Handles following message: "pista@localhost's password:" 
expect "assword";
send "password\r";
interact;

我在互联网上发现类似以下代码的东西应该将这两个示例组合为一个:

I've found on internet that something like following code should combine those two examples into one:

#!/usr/bin/expect
spawn ssh -q localhost "uname -a";
expect {
    "*yes/no*" { send "yes\r" ; exp_continue }
    "*assword:" { send "password\r"; interact }
}

但是此示例在成功登录后立即退出(似乎"interact"在这里不起作用,请参见下面的输出)

But this example exits immediatelly after sucesfull login (seems like "interact" is not workig here, see output bellow)

[pista@HP-PC .ssh]$ ./fin.exp
spawn ssh -q localhost uname -a
pista@localhost's password: 
Linux HP-PC 3.6.6-1.fc16.x86_64 #1 SMP Mon Nov 5 16:56:43 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
[pista@HP-PC .ssh]$ set | grep SHLV
SHLVL=2

三个问题:

  1. 那些奇怪的期望语法是什么意思,对我唯一可能的解释是,在这种大"期望中没有强调模式顺序吗?
  2. 能否请您弄清楚exp_continue到底在做什么,在我看来像是"goto"语句,期望哪个调用了此函数?
  3. 为什么在这里交互不起作用?

非常感谢

推荐答案

1..这种语法意味着您可以使用连续的Expect语句,这更容易.例如,如果SSH失败,它将尝试SSH或telnet

1. This syntax means that you use consecutive expect statements, it's easier. For example, this will try SSH or telnet if SSH fails

#!/usr/bin/expect
set remote_server [lrange $argv 0 0]
set timeout 10
spawn ssh -M username@$remote_server
while 1 {
  expect {
    "no)?"      {send "yes\r"}
    "denied" {
                log_file /var/log/expect_msg.log
                send_log "Can't login to $remote_server. Check username and password\n";
                exit 1
             }
    "telnet:" {
                log_file /var/log/expect_msg.log
                send_log "Can't connect to $remote_server via SSH or Telnet. Something went definitely wrong\n";
                exit 2
              }
    "failed" {
                log_file /var/log/expect_msg.log
                send_log "Host $remote_server exists. Check ssh_hosts file\n";
                exit 3
             }
    timeout {
                log_file /var/log/expect_msg.log
                send_log "Timeout problem. Host $remote_server doesn't respond\n";
                exit 4
            }
    "refused" {
                log_file /var/log/expect_msg.log
                send_log "Host $remote_server refused to SSH. That is insecure.\n"
                log_file
                spawn telnet $remote_server
              }
    "sername:" {send "username\r"}
    "assword:" {send "password\r"}
    "#"        {break}
  }
}

2..exp_continue告诉期望继续期望",即继续进行事件处理.没有此指令,您的期望{...}块将停止.在上面的示例中是以下行:

2. exp_continue is telling expect to "continue to expect", that is to continue with the event processing. Without this instruction your expect { ... } block will stop. In the example above it is the line:

"#" {break}

首先,它从while循环中中断,然后在没有exp_continue的情况下,它停止执行Expect {...}块并继续执行下一条指令(在上面的示例中未显示).

First it breaks from the while loop, and then without exp_continue it stops executing expect { ... } block and going for the next instructions (not shown in the example above).

3..您的代码中有错误.我对您的代码做了一些修改,以使其正常工作.

3. You have an error in your code. I've slightly modified your code, to make it work.

#!/usr/bin/expect
spawn ssh -q localhost
expect {
    "*yes/no*" { send "yes\r" ; exp_continue }
    "*assword:" { send "password\r"; exp_continue;}
    "~" {send "uname -a\r"; interact;}
}

这篇关于Linux期望发送奇怪的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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