在Expect脚本中使用if/else [英] Using if/else in expect script

查看:1600
本文介绍了在Expect脚本中使用if/else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新并重新格式化了/etc/hosts文件,并希望在每个系统(大约1000个)中运行以刷新我的known_hosts文件.我正在使用期望脚本向RSA指纹问题发送是".很简单.但是,我知道其中一些系统对我来说是全新的,并且尚未设置我的密码.这产生了两种可能性:

I recently updated and reformatted my /etc/hosts file and would like to run through every system (roughly 1000) to refresh my known_hosts file. I'm looking at using an expect script to send "yes" to the RSA fingerprint question. Simple enough. However, I know some of these systems are completely new to me and my password has not been set. This creates two possibilities:

  1. 是"发送到RSA指纹问题,我已登录 服务器.然后,我需要发送出口以关闭连接 在移至下一个主机之前.或者...

  1. "yes" is sent to the RSA fingerprint question and I'm logged into the server. I'll then need to send an exit to close the connection before moving onto the next host. Or...

是"被发送到RSA指纹问题,并向我显示 提示您从当前密码和当前密码开始更新我的密码 然后输入两次新密码.连接将 密码更新后自动关闭 下一位主持人.

"yes" is sent to the RSA fingerprint question and I'm presented with the prompts to update my password starting with the current and followed by the new password entered twice. The connection will automatically close after the password is updated moving onto the next host.

我认为我对"if/else"的概念有基本的了解,但是我不完全了解如何嵌套它们,如果有更好的方法,或者我完全脱离了基础首先.

I think I have a basic grasp of the concept of "if/else" in expect, but I don't fully understand how to nest them, if there is a better way, or if I'm completely off-base to begin with.

这就是我现在拥有的:

set file1 [open [lindex $argv 0] r]

set pw1 [exec cat /home/user/.pw1.txt]
set pw2 [exec cat /home/user/.pw2.txt]

while {[gets $file1 host] != -1} {

    puts $host
    spawn -noecho "ssh $host"
    expect {
        "continue connecting"{
            send "yes\r"
            expect {
                "current" {
                    send $pw2\r
                } "New password" {
                    send $pw1\r
                } "Retype new password" {
                    send $pw1\r
                }
            }
        expect "msnyder"
        send "exit\r"
    }
    interact
}

file1变量是运行脚本的主机列表.

The file1 variable is the list of hosts to run the script against.

我知道它不准确,因为它在第22行出错.但是,我不知道需要解决什么.

I know it isn't accurate because it errors on line 22. But, I have no idea what needs to be fixed.

推荐答案

我发现了两个错误:

  1. 缺少右括号,可能是因为继续连接"块
  2. 继续连接"的开放括号之前缺少空间. Tcl(因此期望)对空格非常敏感,因为在评估命令之前将其解析为 words .有关极少的细节,请参阅 12条Tcl语法规则.
  1. missing close brace, probably for the "continue connecting" block
  2. missing space before the open brace of "continue connecting". Tcl (hence Expect) is very sensitive to whitespace as it is parsed into words before the commands are evaluated. For the very few gory details, see the 12 syntax rules of Tcl.

您的代码可能如下:

while {[gets $file1 host] != -1} {
    puts $host
    spawn -noecho "ssh $host"
    expect {
        "continue connecting" {
            send "yes\r"
            expect {
                "current" {
                    send -- $pw2\r
                    exp_continue
                } 
                "New password" {
                    send -- $pw1\r
                    exp_continue
                } 
                "Retype new password" {
                    send -- $pw1\r
                    exp_continue
                }
                msnyder
            }
            send "exit\r"
        }
    }
    interact
}

注意:

  • exp_continue用于循环"回到expect语句:在这种情况下,您将期望看到"current","new"和"retype"的所有内容,因此您不需要纾困,直到看到提示.
  • 养成键入send -- something的习惯.如果没有双破折号,那么当有人键入带有破折号的密码时,您会感到惊讶.
  • exp_continue is used to "loop" back up to the expect statement: in this case, you will expect to see all of "current", "new" and "retype", so you don't want to bail out until you see your prompt.
  • get into the habit of typing send -- something. Without the double dash, you'll be surprised the day someone types in a password with a leading dash.

这篇关于在Expect脚本中使用if/else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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