How to toggle Tcl interact and expect for telnet automation?

纵然是瞬间 提交于 2021-01-29 15:59:48

问题


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$ 

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
}

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...?


回答1:


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

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



来源:https://stackoverflow.com/questions/61674531/how-to-toggle-tcl-interact-and-expect-for-telnet-automation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!