2012-09-26 56 views
0

我想用自动以下交互的Tcl /期望期望输出不一致

[[email protected] 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 
[[email protected]:Standby] config # 
[[email protected]:Standby] config # 
[[email protected]:Standby] config # 
[[email protected]: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 
[[email protected]: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'

输出之一:

[[email protected]:Standby] config # 
[[email protected]: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 
[[email protected]:Standby] config # 

输出二:

<blank line> 
<blank line> 
u[[email protected]:Standby] config # 
[[email protected]:Standby] config # 

u在一行的开头3是输出的一部分,不是拼写错误。

输出2的其他变化也存在。

我预期的输出结果是:

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 
[[email protected]:Standby] config # 

什么我在我的脚本做不正确的?

回答

0

发送密码后,在发送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 ... 
+0

该脚本确实等待NA提示符并完全按照您提及的方式。我想我错过了复制粘贴。解决问题。 – Samveen