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

Posted

技术标签:

【中文标题】如何切换 Tcl 交互并期望 telnet 自动化?【英文标题】:How to toggle Tcl interact and expect for telnet automation? 【发布时间】:2020-08-23 17:41:56 【问题描述】:

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: jmasters@wunderground.com                              *
------------------------------------------------------------------------------

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 没有找到任何东西时然后通过交互然后以某种方式将 expect 重新“打开”...?

【问题讨论】:

为一个重要的服务设计一个好的交互模式可能需要相当多的工作,因为需要考虑很多。 Don Libes 所著的“Exploring Expect”一书中很好地涵盖了这个主题。我强烈建议您找到一份副本并通读一遍。 【参考方案1】:

interact 可以采取类似expect 的模式和操作。特别是,您可以使用操作return 离开交互并继续执行以下语句。一个有用的匹配模式是 control-D,它通常用于表示文件结束。例如

interact \004 return

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

【讨论】:

还有expect_background

以上是关于如何切换 Tcl 交互并期望 telnet 自动化?的主要内容,如果未能解决你的问题,请参考以下文章

expect自动化交互式操作

Tcl 预期生成 telnet 时出错:命令名称无效

期望脚本自动进行 telnet 登录

在启动的 telnet 会话中使用期望脚本

expect工具实现脚本的自动交互

如何使用 Expect 自动化 telnet 会话?