如何切换Tcl交互并期望实现telnet自动化? [英] How to toggle Tcl interact and expect for telnet automation?

查看:62
本文介绍了如何切换Tcl交互并期望实现telnet自动化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

autoexpect 类似,但与 autoexpect 不同之处在于, autoexpect 将始终为任何给定输入产生相同的输出.而尝试至少允许用户输入有时.

Similar to but different from autoexpect in that autoexpect will always produce the same output for any given input. Whereas, trying to allow for user input sometimes at least.

这可以通过以下方式将控制权传给用户:

This works in that it passes control back to the user as below:

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 运行的代码:

code which main.tcl runs:

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 不互动?

when using the above proc, and I'd put in more, how can I toggle interact on/off or even better mix interact with non-interact?

也许是拖延或某种无"或无动作"?

Perhaps putting a delay or a sort of "nothing" or "no action"?

这样,只有当期望没有找到任何东西时,然后通过交互,然后以某种方式将"期望"重新打开……?

So that only when expect doesn't find anything then pass to interact then turn expect back "on" somehow...?

推荐答案

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

interact can take patterns and actions something like expect can. In particular you can use the action return to leave interact and go on to the following statement. A useful pattern to match for is control-D which is often used to signal end-of-file. Eg

interact \004 return

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

will continue with the next statement if it sees control-D, ascii code 4 in octal.

这篇关于如何切换Tcl交互并期望实现telnet自动化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆