使用批处理脚本在 Windows 7 上自动化 telnet 端口测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20583686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automate telnet port testing on Windows 7 using batch script
提问by Shivanand
I am using Windows 7 x64. Using a bat script, I need to check whether I am able to connect to some specific ports on a server using telnet. If successfully connected, the server displays a menu, or else a message like this : Connecting To xxxxx...Could not open connection to the host, on port xxxxx: Connect failed.
我正在使用 Windows 7 x64。使用 bat 脚本,我需要检查是否能够使用 telnet 连接到服务器上的某些特定端口。如果连接成功,服务器会显示一个菜单,否则会显示如下消息:Connecting To xxxxx...无法在端口 xxxxx 上打开与主机的连接:连接失败。
For my purpose, the server has several ports to be tested and I don't want to complicate things by logging in or navigating menus. I just want a simple output indicating whether the connection was successful or not. Checking for exit status didn't work. I don't want to use Visual Basic. Any idea how check the connection status using a bat script? Currently I check visually and I use the below script to open connections
出于我的目的,服务器有几个端口需要测试,我不想通过登录或导航菜单来使事情复杂化。我只想要一个简单的输出,指示连接是否成功。检查退出状态不起作用。我不想使用 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.."
Here is a sample output of the script :
这是脚本的示例输出:
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..
It then opens 5 telnet windows in minimized state, each one with a menu on successful login
然后它会在最小化状态下打开 5 个 telnet 窗口,每个窗口都有一个成功登录的菜单
回答by Bill Agee
It's a shame you can't use netcat - are all open source options like it off-limits in your environment?
很遗憾您不能使用 netcat - 所有像它这样的开源选项在您的环境中都是禁止的吗?
Even without open source tools, you can still accomplish this task with PowerShell.
即使没有开源工具,您仍然可以使用 PowerShell 完成此任务。
Here's a sample PS script that will try to connect to port 23 on $remoteHost and exit 0 on success, and 1 on failure. (If you need to do more work once connected, a few examples of more complex PowerShell telnet clients are available on the web, such as this one.)
这是一个示例 PS 脚本,它将尝试连接到 $remoteHost 上的端口 23,成功时退出 0,失败时退出 1。(如果您在连接后需要做更多的工作,网络上提供了一些更复杂的 PowerShell telnet 客户端示例,例如这个。)
Place the code in the file "foo.ps1", then run in cmd.exe (or from a .bat file) with "powershell -File foo.ps1". When it exits, the exit code will be stored in %errorlevel%.
将代码放在文件“foo.ps1”中,然后使用“ powershell -File foo.ps1”在 cmd.exe(或从 .bat 文件)中运行。当它退出时,退出代码将存储在 %errorlevel% 中。
Note you may need to modify your PowerShell script execution policy (or use the "-ExecutionPolicy Bypass" cmdline option) to allow execution of the script - for more info see this documentation from MSFT.
请注意,您可能需要修改 PowerShell 脚本执行策略(或使用“-ExecutionPolicy Bypass”cmdline 选项)以允许执行脚本 - 有关更多信息,请参阅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
回答by Endoro
you can use netcat, session log:
您可以使用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
回答by Magoo
What do you mean by Checking for exit status didn't work.
?
你是什么意思Checking for exit status didn't work.
?
If you un-REM
your Exit-code print line, the response will be that ERRORLEVEL
will be the value of errorlevel
when the FOR
loop was parsed - prior to execution.
如果你取消REM
你的退出代码打印行,响应将是循环被解析时的ERRORLEVEL
值- 在执行之前。errorlevel
FOR
To display the value as it changes for each invocation of telnet
you'd need
要在每次调用时显示该值,telnet
您需要
...
"C:\Windows\System32\telnet.exe" %host_name% !mwa_port%%i!
echo Exit Code is !errorlevel!
where !errorlevel!
is the run-time value of errorlevel
.
!errorlevel!
的运行时值在哪里errorlevel
。
In all probability, you'd want to suppress the telnet
output, so >nul
and/or 2>nul
may be usefully attached to quieten it down. Possibly youd need to provide telnet with a response - not my area...
很有可能,您想要抑制telnet
输出,因此>nul
和/或2>nul
可能有用地附加以使其安静下来。可能您需要向 telnet 提供响应 - 不是我的区域...
Now - if errorlevel
doesn't return a usable value, is there any telnet response that can be observed to distinguish between the states in which you are interested?
现在 - 如果errorlevel
没有返回可用值,是否有任何可以观察到的 telnet 响应来区分您感兴趣的状态?