0

我想使用 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>

用户输入本质上是:

password
connect 10.1.1.2
uname -a
exit
quit

我写的脚本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."
        }
    }
}

我面临的问题是我与这个脚本的交互得到的输出是不一致的。

我调用脚本如下:expect connect.exp 10.1.1.1 8022 user 'pwd' 10.1.1.2 'uname -a'

输出一:

[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 #

输出二:

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

u3 行开头的 是输出的一部分,而不是拼写错误。

输出二的其他变体也存在。

我期望的输出是:

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 #

我在脚本中做错了什么?

4

1 回答 1

0

发送密码后,您实际上并没有在发送连接命令之前等待 NA 提示。将您的第一个期望命令更改为:

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 ...
于 2012-09-26T10:58:54.650 回答