2017-05-29 14 views
1

我想telnet端口,并希望得到的输出保存到文本文件。我查看了两个解决方案,但没有一个通过命令提示符为我工作。批处理文件,需要保存输出到文本文件的telnet命令在windows

的telnet 127.0.0.1 7000 -f output.txt的

的telnet 127.0.0.1 7000 >> output.txt的

另外,分别检查此link用于输出和错误用1>和2>。还试图通过2> & 1将2(stderr)重定向到1(stdout),如本链接中所述。

在批处理文件中运行以下代码后文件没有任何日志。但是,如果它能够远程登录端口,它将存储日志。但我的要求是从CMD获取日志如果Telnet无法击中连接如下:

连接到127.0.0.1 ......无法打开连接到主机,在端口7002:连接失败

for port in 7000 7001 7002 
do 
    start cmd.exe /k "telnet 127.0.0.1 $port -f C:\Users\rohit.bagjani\Desktop\result\telnetresult.txt" 
done 
+1

你不能赶上建在这样的Telnet客户端的输出。 MS telnet不支持标准输入/输出。更好地考虑第三方telnet客户端或使用powershell/c## – npocmaka

+0

您在代码中显示的'for port'部分永远不会在windows中工作,那些是Unix/Linux命令。此外,-f将仅在telnet上记录客户端端telnet错误...不过,您是否想纯粹运行此端口测试? –

+0

感谢@npocmaka的评论。 –

回答

0

以管理员身份打开的cmd.exe和运行:

powershell set-executionpolicy Bypass 

创建一个新的文件,并将其命名为port-test.ps1

粘贴在里面。

param(
    [string] $rHost = $1, 
    [int] $port = $2 
    ) 

    write-host "Connecting to $rHost on port $port" 
try { 
    $socket = new-object System.Net.Sockets.TcpClient($rHost, $port) 
} catch [Exception] { 
    write-host $_.Exception.GetType().FullName 
    write-host $_.Exception.Message 
    exit 1 
} 

write-host "Connected.`n" 
exit 0 

现在你可以打开的cmd.exe像这样运行:

powershell -file D:\test\port-test.ps1 hostnamename portnr 

即 的powershell -file d:\测试\ telnetit.ps1 somehost.domain.com 23

如果连接不起作用,您将收到如下消息:

Connecting to somehost.domain.com on port 23 system.Management.Automation.MethodInvocationException Exception calling ".ctor" with "2" argument(s): "No connection could be made because the target machine actively refused it 10.1.2.3:23"

然而,如果它是全成您将获得:

Connecting to somehost.domain.com on port 23 Connected.

+0

我与我有过这个解决方案,我正在寻找解决方案,直接批处理文件来运行命令行。不管怎么说,多谢拉。 –

+0

对于批处理,如果无法连接或回显连接,则可以等待超时和回显失败,但这是更可靠的方法。 –

相关问题