期望脚本的看跌期权打印在错误的位置

时间:2019-09-16 21:19:14

标签: tcl expect puts

我正在linux机器上使用以下Expect脚本,通过ssh在远程设备上运行命令,并将其输出打印回运行脚本的linux机器上。

#!/usr/bin/expect -f

set fp [open "~/passwfile" r]
set data [read $fp]

set host [lindex $argv 0];

set prompt [lindex $argv 1];

set timeout 10

spawn ssh $host
while {1} {
  expect \
  {
    "*(yes/no)? "      {send "yes\r"}
    "*assword:"        {send "$data\r"}
    "*$host*$prompt"   {break}
    timeout            {exit 2}
    eof                {puts "\rBreaking - EOF\r"; exit 1}
  }
}

for {set i 2} {$i <= [llength $argv]} {incr i 1} {
  if {$i < [llength $argv]} {puts "";puts ""; puts "\r## command [expr {$i - 1}] start ##"}
  send "\r"
  expect \
  {
    "*$host*$prompt"   {send "[lindex $argv $i]\r"}
    timeout            {exit 4}
    eof                {puts "\rBreaking - EOF\r"; exit 3}
  }

  while {1} {
    expect \
    {
      "*$host*$prompt"   {break}
      "Press <SPACE> to continue or <Q> to quit:" {send " "}
      "(more"          {send " "}
      "More-"          {send " "}
      "ESC->exit"      {send "\x1b\r"}
      timeout          {exit 4}
      eof              {puts "\rBreaking - EOF\r"; exit 3}
    }
  }
  if {$i < [llength $argv]} {puts "\r## command [expr {$i - 1}] finish ##"}
}

send "exit\r"
puts ""

exit 0

它将主机名,提示字符和命令作为参数。

示例

$ ./expectssh bcr03a.dal10 "#" "sh int et1/8"

我需要每个命令的输出在其自己的开始和结束行之间打印。对于上面的示例,这是预期的输出:

.
.
.
bcr03a.dal10# 

## command 1 start ##

bcr03a.dal10# sh int et1/8
Ethernet1/8 is up
admin state is up, Dedicated Interface
  Belongs to Po106
  Hardware: 40000/100000 Ethernet, address: 70d3.7962.898c (bia 70d3.7962.898c)
  MTU 9216 bytes, BW 100000000 Kbit, DLY 10 usec
  reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation ARPA, medium is broadcast
  Port mode is trunk
  full-duplex, 100 Gb/s, media type is 100G
  Beacon is turned off
  Auto-Negotiation is turned off
  Input flow-control is off, output flow-control is off
  Auto-mdix is turned off
  Rate mode is dedicated
  Switchport monitor is off 
  EtherType is 0x8100 
  EEE (efficient-ethernet) : n/a
    admin fec state is auto, oper fec state is cl91
  Last link flapped 59week(s) 4day(s)
  Last clearing of "show interface" counters never
  2 interface resets
  Load-Interval #1: 30 seconds
    30 seconds input rate 249767432 bits/sec, 25786 packets/sec
    30 seconds output rate 178732832 bits/sec, 25343 packets/sec
    input rate 249.77 Mbps, 25.79 Kpps; output rate 178.73 Mbps, 25.34 Kpps
  Load-Interval #2: 5 minute (300 seconds)
    300 seconds input rate 335318184 bits/sec, 24583 packets/sec
    300 seconds output rate 197360256 bits/sec, 25510 packets/sec
    input rate 335.32 Mbps, 24.58 Kpps; output rate 197.36 Mbps, 25.51 Kpps
  RX
    455840060118 unicast packets  436446550 multicast packets  87396954 broadcast packets
    456363082369 input packets  585827288945618 bytes
    37037870002 jumbo packets  0 storm suppression packets
    0 runts  0 giants  0 CRC/FCS  0 no buffer
    0 input error  0 short frame  0 overrun   0 underrun  0 ignored
    0 watchdog  0 bad etype drop  0 bad proto drop  0 if down drop
    0 input with dribble  0 input discard
    0 Rx pause
  TX
    431362512003 unicast packets  2143469193 multicast packets  555138516 broadcast packets
    434061134995 output packets  489379806337887 bytes
    19176210395 jumbo packets
    0 output error  0 collision  0 deferred  0 late collision
    0 lost carrier  0 no carrier  0 babble  0 output discard
    0 Tx pause

bcr03a.dal10#
## command 1 finish ##

有趣的是,有时我会收到此预期的输出,但是有些时候,“ ##命令1完成##”行会被提前打印。通常在“预期”摘要上方的“ RX”行上方2或3行。

您能帮我解决这种随机行为吗?

0 个答案:

没有答案