-1

automated telnet session getting the weather:

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl 


connect to wunderground with:
-----------------
1)  noControlFlow
2)  connect


1
spawn telnet rainmaker.wunderground.com
getting weather for nyc
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 03:51 AM EDT on 10 May 2020 for New York JFK, NY.
Temp(F)    Humidity(%)    Wind(mph)    Pressure(in)    Weather
========================================================================
  40          55%         WEST at 20       30.08      Partly Cloudy

Forecast for New York, NY
319 am EDT sun may 10 2020

.Today...Sunny. Highs in the lower 60s. West winds 15 to 20 mph. 
.Tonight...Mostly cloudy. A chance of showers after midnight.
Lows in the upper 40s. Southwest winds 15 to 20 mph, diminishing
to 5 to 10 mph after midnight. Chance of rain 30 percent. 
.Monday...Mostly cloudy with a chance of showers. A slight chance
of thunderstorms in the afternoon. Highs in the lower 60s. West
winds 10 to 15 mph. Chance of rain 50 percent. 
.Monday night...Mostly cloudy with a slight chance of showers in
the evening, then mostly clear after midnight. Lows in the lower
40s. Northwest winds 15 to 20 mph. Chance of rain 20 percent. 
.Tuesday...Sunny. Highs in the upper 50s. Northwest winds 10 to
15 mph. 
.Tuesday night...Partly cloudy. Lows in the lower 40s. 
.Wednesday...Sunny. Highs in the lower 60s. 
.Wednesday night...Mostly clear in the evening, then becoming
   Press Return to continue, M to return to menu, X to exit: thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 

code:

package provide weather  1.0
package require Expect

namespace eval ::wunderground {
}

#works
proc ::wunderground::noControlFlow {city} {
    variable telnet [spawn telnet rainmaker.wunderground.com]
    puts "getting weather for $city"

    expect "Press Return to continue:"
    send "\r"

    expect "or enter 3 letter forecast city code--"
    send "$city\r"

    expect "X to exit"
    send "x"


}

How can I add a tad of logic or control flow to the above script?

For example, to print a few hard coded cities from the proc itself and present a menu to select from them numerically?

Something like the main script:

lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/weather
package require weather 1.0


puts "\n\nconnect to wunderground with:"
puts "-----------------"
puts "1)\tnoControlFlow"
puts "2)\tconnect\n\n"


set a [gets stdin]


if {$a == 1 } {
   wunderground::noControlFlow "nyc"
} else {
   wunderground::connect "nyc"
}

Yes, obviously wunderground presents a menu of cities to select from. Just looking to add some branching for edification.

Looking to dip in and out of interactive mode with, I believe, a -i switch on the expect statements.

I've sent the man page for expect to my kindle, and am considering buying the book on expect.

Thufir
  • 8,216
  • 28
  • 125
  • 273

1 Answers1

1

You can use normal Tcl control flow constructs like if and foreach with Expect commands. (You've already been doing some of this by using proc.) The only really tricky bit (especially with looping and Expect) is that you need to think about what you're doing with what's going on with what you're automating.

What you end up with might look something like this.

proc ::wunderground::multipleCities {args} {
    variable telnet [spawn telnet rainmaker.wunderground.com]
    puts "getting weather for $city"

    expect "Press Return to continue:"
    send "\r"

    foreach city $args {
        expect "or enter 3 letter forecast city code--"
        send "$city\r"
    }

    expect "X to exit"
    send "x"
}

Except I think you probably ought to somehow structure the expect in the loop to be after the send, conceptually so that you can process the results of fetching the values for a particular city. You probably need to identify a common stable prompt that you come back to after getting the data.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • 1
    If you want more help than this, you'll need to say what you want to do. You might well be interested in using multiple patterns in an `expect` command, for example; that often substitutes for using `if` in expect scripts, at least when matching _this_, or _that_ or _the other_. – Donal Fellows May 10 '20 at 21:24
  • It's just a personal project to do something like: https://en.wikipedia.org/wiki/MUD_client – Thufir May 11 '20 at 05:40