Как переключить взаимодействие Tcl и ожидать автоматизации te lnet? - PullRequest
0 голосов
/ 08 мая 2020

Подобен 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 как-нибудь обратно ...?

1 Ответ

1 голос
/ 08 мая 2020

interact может выполнять шаблоны, а действия вроде expect могут. В частности, вы можете использовать действие return, чтобы оставить взаимодействие, и go для следующего оператора. Полезный шаблон для поиска - control-D , который часто используется для обозначения конца файла. Например,

interact \004 return

продолжит выполнение следующего оператора, если увидит control-D, код ascii 4 в восьмеричном формате.

...