CentOS 与 Windows 中的 Telnet 控制字符行为

Telnet control character behaviour in CentOS vs Windows

我一直在使用旧的 GSM 网关,文档向我展示了如何通过 telnet 连接到设备以发送 SMS 消息。我的计划是在我的 CentOS 服务器上编写一个 expect 脚本,它能够在需要时发送简单的警报消息。

当我测试这个时,我使用 Windows telnet 客户端连接到 PorTECH 设备,我必须输入以下命令:

module1
#The following command tells the device to echo back received commands to the telnet client
ate1

at+cmgf=1

at+cmgs="07XXXXXXXXX"

> Message to be sent

ctrl-z

在选择模块 1 后,您可以随时按 ctrl-x 释放模块 1,这很好用。

Ctrl-z 用于发送您输入的消息。

我写了一个 expect 脚本来处理这一系列事件,但是在发送 ctrl-z 消息时(我使用了 \032 并且在终端 window 当我运行脚本时)没有任何反应。

我有点莫名其妙,所以我在 CentOS 中加载了 telnet 并自己完成了这些步骤。我注意到当我按下 ctrl-x 来释放模块时,我必须有一个回车 return 才能观察命令,所以我尝试将它添加到我的脚本中(这没有帮助)。我还尝试运行上面提到的所有命令来发送短信,当它到达我输入后按 ctrl-z 发送消息的部分时,即使我跟随 ctrl-z 输入带回车 return 未观察到。

我还尝试使用 -8 标志(我无法通过登录提示)和允许我登录的 -7 标志运行 telnet,但没有解决 ctrl-z最后输入

我还尝试加载 telnet,运行 "toggle localchars",然后尝试执行与 ctrl-z.[=7= 得到相同结果的步骤]

我也看到 post 建议尝试 "toggle options" 和 "toggle termdata" 这可以帮助诊断问题,但我不确定我在看什么,当我有设置好这些选项,按ctrl-z后出现如下:

^ZSENT IAC SUSP
SENT DO TIMING MARK

我不确定这意味着什么,或者如何检查它与 Windows telnet 操作有何不同。谁能告诉我为什么这个输入会产生与在 Windows telnet 会话中按 ctrl-z 不同的效果?

编辑

据我所知,Microsoft telnet program sends the same octal values to the telnet server as the CentOS telnet 程序,所以我真的不明白为什么按 ctrl-z 可以在 Windows telnet 中工作,但在 CentOS telnet 中不起作用...根据 Zathrus 在 #centos irc 频道中的建议,我也尝试在我的 ctrl-z 之前发送 ctrl-v (在脚本和简单的 telnet 中),这也没有帮助...

进一步编辑

我今天一直在研究这个问题,我在我的 Windows 机器上运行 Wireshark,发现 Windows 按预期发送了一个带有 "Data: 2" 的数据包,但是当我捕获我的 CentOS 机器上 telnet 会话的 tcpdump 流量它发送 "Suspend Current Process" 以及 "Do Timing Mark".

运行我编写的脚本而不是手动连接到 telnet 似乎发送相同的信息,不管我发送的是文字 \032 表达式,它在同一个数据包中被翻译为 "Suspend Current Process"我还收到了在预期结束时发送的 \r\n 个字符。

为清楚起见,这是我用来发送消息的脚本:

#!/usr/bin/expect
# Test script for SMS sends automatically via expect & telnet

#Set a sensible timeout

set timeout 20

#Our target device

set target 10.0.0.114

#set username and password info for SMS sending

set user voip
set pword 1234



#spawn the telnet session

spawn telnet $target

#Set up the expects fo the login process

expect "username:"

send "$user\n"

expect "password:"

send "$pword\n"


#I've only put a SIM in module1 so far

expect "]"

#check state1 for free, make sure it isn't in use already, exit if returns none

send "state1\n"

expect {
  "free" { send "module1\n" }
  "none" { send "logout\n" }
}

expect "got!! press 'ctrl-x' to release module 1." { send "ate1\n" }

expect "0" { send "at+cmgf=1\n" }

expect "0" { send "at+cmgs=\"07XXXXXXXXX\"\ntesting252\n" }

#this should be the ctrl-z input as per http://www.unix-manuals.com/refs/misc/ascii-table.html
#send "2"

interact

我还尝试在进入 telnet 并在打开与 GSM 网关的连接之前提交 toggle localchars 命令后运行捕获,此捕获还显示发送的数据是 "Suspend Current Process" 后跟 "Do Timing Mark"

我还能做些什么来发送文字 \032 表达式而不对其进行解释吗?

进一步编辑:

这是从 Windows 和 Linux Telnet 会话

中捕获的数据包

这是为 Windows telnet 会话捕获的十六进制,数据包在 Wireshark 中显示为 "Data: 2"

0000   00 03 7e 00 44 4f 30 85 a9 0e a6 52 08 00 45 00  ..~.DO0....R..E.
0010   00 29 3d 0b 40 00 80 06 b1 32 0a 00 f8 1f 0a 00  .)=.@....2......
0020   00 72 10 02 00 17 cf 41 a8 01 b0 3e 52 22 50 18  .r.....A...>R"P.
0030   f9 1d 06 5f 00 00 1a                             ..._...

这是为 Linux telnet 会话捕获的十六进制,在 Wireshark 中显示为 "Suspend Current Process" 然后 "Do Timing Mark" :

0000   00 03 7e 00 44 4f 00 16 35 37 74 22 08 00 45 10  ..~.DO..57t"..E.
0010   00 2d 08 43 40 00 40 06 13 c6 0a 00 0a 41 0a 00  .-.C@.@......A..
0020   00 72 b1 46 00 17 b8 b3 01 36 a9 22 f2 7e 50 18  .r.F.....6.".~P.
0030   39 08 1e d2 00 00 ff ed ff fd 06                 9..........

没有足够的代表发表评论

你知道吗,行尾在(转义是八进制的)

  • Windows 为 CR+LF (52)
  • Linux只是LF(\012)

您必须在 CTRL-X 之后输入额外的 RETURN,可能在每次执行命令后都需要?

如果是这样,网关会遇到所有已发送字符串的串联,试图将它们不恰当地解析为单个命令(来自您的 CentOS [因此 Linux] 机器)