在Windows 7自动telnet端口测试使用批处理脚本 [英] Automate telnet port testing on Windows 7 using batch script

查看:1812
本文介绍了在Windows 7自动telnet端口测试使用批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Windows 7 64位系统。使用蝙蝠剧本,我需要检查我是否能够连接到一些特定的端口使用telnet服务器上。如果连接成功,服务器显示一个菜单,否则这样的消息:连接到为XXXXX ......无法打开连接到主机,端口XXXXX:连接失败。

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.

有关我的目的,服务器有多个端口进行测试和我不希望通过登录或浏览菜单使事情变得复杂。我只想要一个简单的输出指示连接是否成功与否。检查退出状态没有工作。我不想使用Visual Basic。任何想法如何使用脚本蝙蝠检查连接状态?目前,我目视检查,我用下面的脚本打开连接

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

@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..

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

It then opens 5 telnet windows in minimized state, each one with a menu on successful login

推荐答案

这是你不能使用netcat的一种耻辱 - 就像所有开源选项禁地在您的环境

It's a shame you can't use netcat - are all open source options like it off-limits in your environment?

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

Even without open source tools, you can still accomplish this task with PowerShell.

下面是一个示例PS脚本,它将尝试连接到在$远程主机的端口23和成功退出0,1失败。 (如果你需要做更多的工作一旦连接成功,更复杂的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.)

将code在文件foo.ps1,然后输入cmd.exe(或.bat文件)与运行的PowerShell - 文件foo.ps1 。当它退出,退出code将存储在%ERRORLEVEL%。

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%.

请注意,你可能需要修改你的PowerShell脚本执行策略(或使用-ExecutionPolicy绕道CMDLINE选项)允许脚本执行 - 更多信息请参见从MSFT 本文档

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.

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

这篇关于在Windows 7自动telnet端口测试使用批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆