如何切换Tcl交互和期望的telnet自动化?

问题描述 投票:0回答:1

类似于但不同于 autoexpect 在那 autoexpect 对任何给定的输入都会产生相同的输出。 而试图允许用户输入 有时 至少是这样。


它的工作原理是将控制权传回给用户,如下所示。

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl 
got nyc
spawn telnet rainmaker.wunderground.com
Trying 35.160.169.47...
Connected to rainmaker.wunderground.com.
Escape character is '^]'.
------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: [email protected]                              *
------------------------------------------------------------------------------

Press Return to continue:

Press Return for menu
or enter 3 letter forecast city code-- nyc
Weather Conditions at 02:51 AM EDT on 08 May 2020 for New York JFK, NY.
Temp(F)    Humidity(%)    Wind(mph)    Pressure(in)    Weather
========================================================================
  54          55%         NW at 16       29.83      Mostly Cloudy

Forecast for New York, NY
327 am EDT Fri may 8 2020

.Today...Cloudy. A slight chance of rain this morning, then rain
this afternoon. Highs in the upper 50s. Northwest winds around
5 mph, becoming south this afternoon. Chance of rain 80 percent. 
.Tonight...Rain in the evening, then rain likely with a slight
chance of snow after midnight. Cold with lows in the upper 30s.
East winds 5 to 10 mph with gusts up to 20 mph, increasing to
northwest 15 to 20 mph with gusts up to 30 mph after midnight.
Chance of precipitation 90 percent. 
.Saturday...Partly sunny. A slight chance of showers in the
afternoon. Windy with highs around 50. Northwest winds 20 to
30 mph with gusts up to 40 mph. Chance of rain 20 percent. 
.Saturday night...Partly cloudy with a slight chance of showers
in the evening, then mostly clear after midnight. Breezy with
lows in the upper 30s. West winds 15 to 25 mph with gusts up to
40 mph. Chance of rain 20 percent. 
   Press Return to continue, M to return to menu, X to exit: x
Connection closed by foreign host.
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 

代码 main.tcl 运行。

package provide weather  1.0
package require Tcl      8.5
package require Expect


namespace eval ::tutstack {
}

proc ::tutstack::connect {arg1} {
puts "got $arg1"
spawn telnet rainmaker.wunderground.com
set telnet $spawn_id
expect -nocase "Press Return to continue:"
send  ""
interact
}

当使用上述 proc我还想再多放点,我怎么才能切换到 interact 开、关,甚至更好的混合 interact 与非互动?

也许放一个延迟或某种 "无 "或 "无行动"?

这样,只有当期望没有找到任何东西的时候 然后 传递互动,然后转 expect 以某种方式重新 "开启"?

automation tcl expect telnet mud
1个回答
1
投票

interact 可以采取类似的模式和动作 expect 可以。特别是你可以使用 return 离开互动,继续下面的语句。 一个有用的匹配模式是 控制-D 通常用于发出文件结束的信号。例如

interact \004 return

如果看到control-D,ascii码4为八进制,将继续下一条语句。

© www.soinside.com 2019 - 2024. All rights reserved.