使用批处理脚本在Windows 7上自动化Telnet端口测试

3
我正在使用Windows 7 x64操作系统。我需要使用一个批处理脚本检查我是否能够使用telnet连接到服务器上的一些特定端口。如果成功连接,服务器将显示一个菜单;否则会显示以下消息: 连接到xxxxx...无法打开与主机的连接,端口xxxxx上的连接失败。
对于我的需求,服务器有多个端口需要测试,我不想通过登录或导航菜单来复杂化事情。我只想要简单的输出,指示连接是否成功。检查退出状态并没有起作用。我不想使用Visual Basic。请问有什么方法可以使用批处理脚本检查连接状态吗?目前我进行可视化检查,并使用以下脚本打开连接。
@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窗口,每个窗口在成功登录后都有一个菜单


1
Windows的telnet.exe不支持脚本编写。此外,您无法通过find.exe管道传输输出,并且当连接成功时,窗口将挂起。有一个可编写脚本的telnet工具“Telnet Scripting Tool v.1.0”,可能会对您有所帮助。 - foxidrive
3个回答

4
很遗憾您无法使用netcat - 您的环境是否禁止所有类似它的开源选项?即使没有开源工具,您仍然可以使用PowerShell完成此任务。下面是一个示例PS脚本,将尝试连接到$remoteHost上的端口23,并在成功时退出0,在失败时退出1。(如果需要连接后执行更多工作,网上有一些更复杂的PowerShell telnet客户端示例,例如这个)。将代码放入文件“foo.ps1”,然后在cmd.exe中运行(或从.bat文件中运行)使用“powershell -File foo.ps1”。退出时,退出代码将存储在%errorlevel%中。请注意,您可能需要修改PowerShell脚本执行策略(或使用“-ExecutionPolicy Bypass”命令行选项)以允许执行脚本 - 有关更多信息,请参见来自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 - Shivanand
1
是的,这就是为什么我提到你需要使用“-ExecutionPolicy Bypass”或按照我发布的MS文档链接上的说明进行操作。 :) 请参见我发布的代码片段上方的段落。或者,您可以谷歌错误字符串以获取更多有关如何绕过策略的信息。 - Bill Agee
1
在从cmd控制台打开powershell时,只需使用“powershell -ExecutionPolicy ByPass”启动它,然后执行powershell命令即可。 - razvanone
如果不打算使用已连接的套接字,则最好关闭它 $socket.close();,在成功连接后这样做。 - Davut Gürbüz

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。还有其他方法吗? - Shivanand
只需从“可信源”sourceforge获取snetcat二进制文件,您将获得相同的“errorlevel”结果。 - Endoro
只允许可信来源的安全策略通常会阻止Sourceforge。 - karatedog

0

你说的“检查退出状态没有起作用”是什么意思?

如果你取消注释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
!errorlevel! 无效。即使有些连接成功,有些连接失败,所有端口都返回1。 - Shivanand

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接