2015-10-19 63 views
0

我的TCL文件中有這樣的代碼下面,

proc executeCmd {cmd {file ""}} { 
    set out "" 
    set output "" 
    set send_slow {20 0.1} 
    set adminFlag 0 
    exp_send -s "$cmd\r" 
    for {set i1 0} {$i1 < 12} {incr i} { 
      set intimeout 0 
      expect { 
        # other options to check 'hostname', 'more', 'press any to continue' regexes 
        # ... 
        -regexp {^(.*)Press any key to continue.*$} { 
         set output [cleanOutput $expect_out(buffer)] 
         if {[regexp -- {\w+} $file]} { 
          append out $output 
          flush $fo 
          flush $clf 
         } else { 
          append out $output 
         } 
         exp_sleep 0.1 
         exp_send -s " " 
         exp_continue 
        } 

        timeout { 
          #log_msg INFO "TIMED OUT...." 
          puts "TIMED OUT" 
          set intimeout 1 
          puts "Executing $cmd >>> waiting for response from $hostname" 
        } 
      } 
      if {$intimeout} { 
        exp_send -s " " 
      } else { 
        break 
      } 
    } 
    return $out 
} 

spawn $plinkLoc -telnet $routerIP -P $routerPort 
set out [executeCmd "term width 0"] 

通過命令執行這個TCL之後提示我面對錯誤的說法,

send: spawn id exp4 not open 
    while executing 
"exp_send -s "$cmd\r"" 
    (procedure "executeCmd" line 28) 
    invoked from within 
"executeCmd "term width 0"" 
    invoked from within 
"set out [executeCmd "term width 0"]"" 

這在TCL行代碼28「設置」聲明這是之前「executeCmd」 PROC,已經更新了查詢目的的文件。

+0

你生成一個腳本?什麼是'$ plinkLoc'? – Dinesh

+0

$ plinkLoc是plink.exe的路徑,m試圖通過產卵連接到路由器。 –

+0

相同的腳本之前工作完全正常,在我的窗口更新時,我不確定它是因爲那個問題還是任何TCL補丁過期。即使經過如此多的挖掘,也無法追蹤它... –

回答

0

您必須將spawn_id傳遞給過程,或聲明spawn_id爲global

添加此行內部程序

global spawn_id 
+0

期望變量作用域與Tcl不同:如果在本地作用域中找不到變量,則期望值將在全局作用域中查找。由於'spawn'命令出現在主(全局)命名空間中,executeCmd proc中的expect和send命令將能夠找到spawn_id。檢查[期望手冊頁]的「警告」部分(http://manpages.ubuntu.com/manpages/trusty/en/man1/expect.1.html)。 –

+0

@glennjackman:謝謝先生,糾正我。我希望我提到的任何內容適用於在過程中使用'spawn'命令本身。 – Dinesh

+0

是的,當在proc中產卵時,需要使spawn_id爲全局。 –

相關問題