Tcl 预期输出不一致

Tcl 预期输出不一致,tcl,expect,Tcl,Expect,我想使用Tcl/expect自动化以下交互 [root@mgmt NAS]# ssh -q -p 8022 -l user 10.1.1.1 Password: HP Network Automation Version 9.10.02 Type "HELP connect" to see how to connect to a device. Type "HELP" to view a list of available commands. NA>connect 10.1.1.

我想使用Tcl/expect自动化以下交互

[root@mgmt NAS]# ssh -q -p 8022 -l user 10.1.1.1
Password:


HP Network Automation Version 9.10.02

Type "HELP connect" to see how to connect to a device.
Type "HELP" to view a list of available commands.


NA>connect 10.1.1.2
WARNING: You do not have an approved reservation for this device at this time.
Attempting to connect to device bigip1.network.company.net (10.1.1.2).

Last login: Wed Sep 26 08:11:42 2012 from 10.2.1.1
Last login: Wed Sep 26 08:11:42 2012 from 10.2.1.1
[root@bigip1:Standby] config #
[root@bigip1:Standby] config #
[root@bigip1:Standby] config #
[root@bigip1:Standby] config # uname -a
Linux bigip1.network.company.net 2.6.18-164.11.1.el5.1.0.f5app #1 SMP Thu Apr 8 18:26:58 PDT 2010 i686 i686 i386 GNU/Linux
[root@bigip1:Standby] config # exit
logout

Disconnected from device bigip1.network.company.net (10.1.1.2).
NA>quit
Logging out of the NA Proxy Interface.
<Blank Line: couldn't show it with simple formatting>
我编写的脚本,
connect.exp
,如下所示:

#!/usr/local/bin/expect

# Set the input parameters
set nashost [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set passw [lindex $argv 3]
set device [lindex $argv 4]
set cmd [lindex $argv 5]

set binpath /usr/bin

log_user 0

# Set timeout to 45 seconds
set timeout 45

#check if all were provided
if { $nashost == "" || $port == "" || $user == "" || $passw == "" || $device == "" || $cmd == "" } {
    puts "Usage: <nashost> <port> <user> <passw> <device> <command>\n"
    exit 1
}

# String Variables
set nasprompt "NA>$"
set prompt "config # $"

# Flag Variables
set running 1
set count 0

# SSH to specified NAS host
if { [catch {spawn $binpath/ssh -q -p $port -o "StrictHostKeyChecking no" -l $user $nashost} error] } {
    puts "Spawn: SSH failed: $error"
    exit
}

expect {
    "assword: " {
        send "$passw\r"
        incr count
        if {$count > 3} {
            puts "SSH failed on authentication after 3 tries"
            set running 0
        } else {
            exp_continue
        }
    }
    -re "$nasprompt" {
        set running 1
    }
    "Connection refused" {
        puts "$expect_out(buffer)"
        set running 0
    }
    "Offending key" {
        puts "Host key verification failed."
        set running 0
    }
    eof {
        puts -nonewline "Connection terminated unexpectedly:\n$expect_out(buffer)"
        set running 0
    }
    timeout {
        puts "ssh: connect to NAS host $host: Connection timed out"
        set running 0
    }
}
if {$running == 1} {
    send "connect $device\r"
    expect {
        -re "$nasprompt" {
            if {$running > 0} {
                puts "connect to Device $device failed:\n$expect_out(buffer)"
            }
            send "quit\r"
        }
        -re "$prompt" {
            if {$running > 0} {
                send "$cmd\r"
                set running 0
                exp_continue
            } else {
                puts "$expect_out(buffer)"
                send "exit\r"
            }
        }
        full_buffer {
            puts "$expect_out(buffer)"
            exp_continue
        }
        eof {
            puts "ssh: Connection terminated unexpectedly during command execution: $host."
        }
        timeout {
            puts "ssh: Connection timed out during command execution: $host."
        }
    }
}
产出二:

<blank line>
<blank line>
u[root@bigip1:Standby] config #
[root@bigip1:Standby] config #

脚本中有什么不正确的地方?

在发送密码后,您实际上不会在发送connect命令之前等待NA提示。将第一个expect命令更改为:

set running false

expect {
    "assword: " {
        incr count
        if {$count > 3} {
            puts "SSH failed on authentication after 3 tries"
        } else {
            send "$passw\r"
            exp_continue
        }
    }
    "Connection refused" {
        puts "$expect_out(buffer)"
    }
    "Offending key" {
        puts "Host key verification failed."
    }
    eof {
        puts -nonewline "Connection terminated unexpectedly:\n$expect_out(buffer)"
    }
    timeout {
        puts "ssh: connect to NAS host $host: Connection timed out"
    }
    -re "$nasprompt" {
        set running true
    }
}
if {$running} {
    send "connect ...

脚本确实按照您提到的方式等待NA提示。我想我在复制粘贴中错过了。修正问题。
<blank line>
<blank line>
u[root@bigip1:Standby] config #
[root@bigip1:Standby] config #
Linux bigip1.network.company.net 2.6.18-164.11.1.el5.1.0.f5app #1 SMP Thu Apr 8 18:26:58 PDT 2010 i686 i686 i386 GNU/Linux
[root@bigip1:Standby] config #
set running false

expect {
    "assword: " {
        incr count
        if {$count > 3} {
            puts "SSH failed on authentication after 3 tries"
        } else {
            send "$passw\r"
            exp_continue
        }
    }
    "Connection refused" {
        puts "$expect_out(buffer)"
    }
    "Offending key" {
        puts "Host key verification failed."
    }
    eof {
        puts -nonewline "Connection terminated unexpectedly:\n$expect_out(buffer)"
    }
    timeout {
        puts "ssh: connect to NAS host $host: Connection timed out"
    }
    -re "$nasprompt" {
        set running true
    }
}
if {$running} {
    send "connect ...