【问题标题】:Automate telnet port testing on Windows 7 using batch script使用批处理脚本在 Windows 7 上自动执行 telnet 端口测试
【发布时间】:2014-01-02 05:40:06
【问题描述】:

我使用的是 Windows 7 x64。使用 bat 脚本,我需要检查是否能够使用 telnet 连接到服务器上的某些特定端口。如果连接成功,服务器会显示一个菜单,或者类似这样的消息:Connecting To xxxxx...Could not open connection to the host, on port xxxxx: Connect failed。

出于我的目的,服务器有几个端口要测试,我不想通过登录或导航菜单使事情复杂化。我只想要一个简单的输出来指示连接是否成功。检查退出状态不起作用。我不想使用 Visual Basic。知道如何使用 bat 脚本检查连接状态吗?目前我目视检查并使用以下脚本打开连接

@ECHO OFF
setlocal EnableDelayedExpansion

echo.
SET /P host_name="Please enter the hostname: "
echo Please enter the port numbers followed by the Enter key: 
echo.
set num=0

:Loop
set /a num=num+1
SET /P mwa_port%num%=""
if "!mwa_port%num%!"=="" (goto Start)
goto Loop

:Start
set /a num=num-1
for /L %%i in (1,1,%num%) do (
 echo Trying to log into port !mwa_port%%i! of %host_name%
 start /min "" "C:\Windows\System32\telnet.exe" %host_name% !mwa_port%%i!
 REM echo Exit Code is %errorlevel%
)

:End
endlocal
echo.
SET /P leave="Press any key to exit.."

这是脚本的示例输出:

Please enter the hostname : abcdefg.hijkl.mnop
Please enter the port numbers followed by the Enter key:

10001
10002
10003
10004
10005

Trying to log into port 10001 of abcdefg.hijkl.mnop
Trying to log into port 10002 of abcdefg.hijkl.mnop
Trying to log into port 10003 of abcdefg.hijkl.mnop
Trying to log into port 10004 of abcdefg.hijkl.mnop
Trying to log into port 10005 of abcdefg.hijkl.mnop

Press any key to exit..

然后它会在最小化状态下打开 5 个 telnet 窗口,每个窗口在成功登录时都有一个菜单

【问题讨论】:

  • Windows telnet.exe 不可编写脚本。此外,您无法通过 find.exe 管道输出,当连接成功时,窗口将挂起。有一个可编写脚本的 telnet 工具 Telnet Scripting Tool v.1.0 可以帮助您。

标签: windows batch-file automation telnet


【解决方案1】:

很遗憾您不能使用 netcat - 在您的环境中是否所有类似它的开源选项都被禁止使用?

即使没有开源工具,您仍然可以使用 PowerShell 完成这项任务。

这是一个示例 PS 脚本,它将尝试连接到 $remoteHost 上的端口 23,成功时退出 0,失败时退出 1。 (如果您需要在连接后做更多工作,可以在网上找到一些更复杂的 PowerShell telnet 客户端示例,例如 this one。)

将代码放在文件“foo.ps1”中,然后使用“powershell -File foo.ps1”在 cmd.exe(或从 .bat 文件)中运行。退出时,退出码会保存在%errorlevel%中。

请注意,您可能需要修改 PowerShell 脚本执行策略(或使用 "-ExecutionPolicy Bypass" cmdline option)以允许执行脚本 - 有关详细信息,请参阅 this documentation from MSFT

param(
    [string] $remoteHost = "arbitrary-remote-hostname",
    [int] $port = 23
     )

# Open the socket, and connect to the computer on the specified port
write-host "Connecting to $remoteHost on port $port"
try {
  $socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
} catch [Exception] {
  write-host $_.Exception.GetType().FullName
  write-host $_.Exception.Message
  exit 1
}

write-host "Connected.`n"
exit 0

【讨论】:

  • D:\*********>powershell -File test.ps1 File D:\**********\test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details. + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : RuntimeException
  • 是的,这就是为什么我提到您需要使用“-ExecutionPolicy Bypass”或按照我发布的 MS 文档链接上的说明进行操作。 :) 请参阅我发布的代码 sn-p 上方的段落。或者,您可以通过谷歌搜索错误字符串以了解更多关于如何绕过该政策的帐户。
  • 从 cmd 控制台打开 powershell 时,只需使用“powershell -ExecutionPolicy ByPass”启动它 - 然后执行 powershell 命令。
【解决方案2】:

你可以使用netcat,会话日志:

    C:\Users\User>nc -v -t stackoverflow.com 23
    stackoverflow.com [198.252.206.16] 23 (telnet): TIMEDOUT

    C:\Users\User>echo %errorlevel%
    1

    C:\Users\User>nc -v -t stackoverflow.com 25
    stackoverflow.com [198.252.206.16] 25 (smtp) open
    220 SMTP Relay SMTP
    421 SMTP Relay SMTP session timeout. Closing connection

    C:\Users\User>echo %errorlevel%
    0

【讨论】:

  • 由于工作场所的安全政策,我只能使用来自受信任来源的工具。我将无法使用netcat。有没有其他办法?
  • 只需从“受信任的来源”获取snetcat 二进制文件:sourceforge,您会得到相同的errorlevel 结果。
  • 安全策略只允许受信任的来源通常会阻止 Sourceforge。
【解决方案3】:

Checking for exit status didn't work. 是什么意思?

如果您取消REM 您的退出代码打印行,则响应将是ERRORLEVEL 在解析FOR 循环时将是errorlevel 的值 - 在执行之前。

在每次调用 telnet 时显示值的变化,您需要

 ...
 "C:\Windows\System32\telnet.exe" %host_name% !mwa_port%%i!
 echo Exit Code is !errorlevel!

其中!errorlevel!errorlevel 的运行时值。

您很可能希望抑制telnet 输出,因此可能会有效地附加>nul 和/或2>nul 以使其安静下来。可能您需要向 telnet 提供响应 - 不是我的区域...

现在 - 如果 errorlevel 没有返回可用值,是否可以观察到任何 telnet 响应来区分您感兴趣的状态?

【讨论】:

  • !错误级别!没用。所有端口都返回 1,即使有些连接有些不连接
猜你喜欢
  • 1970-01-01
  • 2015-06-28
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 2016-09-04
  • 2019-06-18
相关资源
最近更新 更多