1

I am trying to telnet the port and want to get the output saved to the text file. I have looked and got two solutions but none of them are working for me through command prompt.

telnet 127.0.0.1 7000 -f output.txt

telnet 127.0.0.1 7000 >> output.txt

Also, checked this link for stdout and stderr with 1> and 2> respectively. Also tried to redirect 2 (stderr) to 1 (stdout) through 2>&1 as mentioned in this link.

File doesn't have any logs after running following code in batch file. But it stores the logs if it is able to telnet the port. But my requirement is to get the logs from cmd if telnet is unable to hit the connect as follows:

Connecting To 127.0.0.1...Could not open connection to the host, on port 7002: Connect failed

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
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Rohit Bagjani
  • 1,264
  • 2
  • 11
  • 14
  • 1
    You can't catch the built in telnet client output like that. MS telnet doesn't support stdin/out. Better think of third party telnet client or use sockets with powershell/c# – npocmaka May 29 '17 at 15:28
  • the `for port` portion you are showing in the code will never work in windows, those are Unix/Linux commands. Also, -f will log client side telnet errors only on telnet... nevertheless, do you want to run this purely for port testing? – Gerhard May 30 '17 at 06:33
  • thanks @npocmaka for your comment. – Rohit Bagjani May 30 '17 at 10:48
  • @GerhardBarnard for loop does work in windows, and yes I have mentioned your point (in a different way) that -f will log client side telnet errors only. And yes, I will I wanted to do this for port testing only. Do you have any workaround for that, that would be really helpful? – Rohit Bagjani May 30 '17 at 10:51
  • yes, using powershell. will post answer. – Gerhard May 30 '17 at 11:10

1 Answers1

0

as Administrator open cmd.exe and run:

powershell set-executionpolicy Bypass

create a new file and name it port-test.ps1

paste this in it.

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

now you can open cmd.exe and run it like this:

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

i.e powershell -file D:\test\telnetit.ps1 somehost.domain.com 23

If connection does not work, you will get a message like:

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"

If however it is successfull you will get:

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

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • I had this solution with me, I was looking for solution directly for batch file to run command line. Thanks anyways. – Rohit Bagjani May 30 '17 at 12:32
  • for batch you can wait for timeout and echo failure if you cannot connect or echo connect when you do, but this is a more reliable method. – Gerhard May 30 '17 at 13:12