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

Posted

技术标签:

【中文标题】Tcl 预期生成 telnet 时出错:命令名称无效【英文标题】:Tcl expect error with spawning telnet: invalid command name 【发布时间】:2020-08-23 14:31:52 【问题描述】:

如何向这个 telnet 服务器发送一个“返回”:

got New York
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:invalid command name ""
    while executing
"[tutstack::connect "New York"]"
    (file "main.tcl" line 6)

代码:

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  "$arg1"

主要:

lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/api

package require weather 1.0


[tutstack::connect "New York"]

我在send "\r" 上尝试了variations,但没有成功。

最终,希望脚本仅 partially 自动响应并保持连接 open。

预期结果:

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 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--    

                 WEATHER UNDERGROUND MAIN MENU
                ******************************
                 1) U.S. forecasts and climate data
                 2) Canadian forecasts
                 3) Current weather observations
                 4) Ski conditions
                 5) Long-range forecasts
                 6) Latest earthquake reports
                 7) Severe weather
                 8) Hurricane advisories
                 9) Weather summary for the past month
                10) International data
                11) Marine forecasts and observations
                12) Ultraviolet light forecast
                 X) Exit program
                 C) Change scrolling to screen
                 H) Help and information for new users
                 ?) Answers to all your questions
                   Selection:

理想情况下,我希望 一些 自动响应,但最终也希望输入发送到 telnet 服务器的文本 - 取决于逻辑。但目前,只是尝试编写一些响应脚本。

另见:

expect - telnet connection

【问题讨论】:

当你send "\r"时发生了什么?有什么错误信息吗? 您能否详细说明一下“部分自动化响应并保持连接打开”是什么意思 删除[tutstack::connect "New York"] 周围的[] 以消除错误消息。您的 proc 正在完成并返回 main。然后您正在执行返回的空字符串。 这就是为什么minimal reproducible example 会有很大帮助的原因。 :) @meuh 你应该把它写下来作为答案。 【参考方案1】:

谢谢你:

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl 
got New York
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:thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat main.tcl 
lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/api

package require weather 1.0


tutstack::connect "New York"
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat api/weather.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  "$arg1"


thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 

【讨论】:

以上是关于Tcl 预期生成 telnet 时出错:命令名称无效的主要内容,如果未能解决你的问题,请参考以下文章

运行所选代码生成器时出错:“预期具有协定名称 "NuGet.VisualStudio.IVsPackageInstallerServices" 的1导出 ——VS2015错误记录

Tcl 错误:无效的命令名称 tcl_findLibrary

ValueError:检查输入时出错:预期 input_1 有 4 个维度,但得到的数组具有形状(无、无、无)

ValueError:检查目标时出错:预期activation_6具有形状(无,2)但得到的数组具有形状(5760,1)

值错误:检查目标时出错:预期dense_1具有形状(无,1)但得到的数组具有形状(6000,3)

ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到的数组具有形状(无,1)