0

When I open a command prompt manually and type the following command I get a response from the server I am pinging.

telnet <server> <port>

When I open the command prompt with Ruby, using the following command, and run the same telnet command above, I get ''telnet' is not recognized as an internal or external command, operable program or batch file.'

Ruby command:

system('start cmd.exe)

It opens the prompt fine, but the command itself is not working, and I am not sure to why that is.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • 2
    You will need to me more specific. What is run by `cmd.exe`? How are you invoking the command or starting ruby? Can you break this issue down into smaller steps? What if you open an `irb` instance and type in your command there? – Puhlze Jun 05 '17 at 17:25
  • when I am running system('start cmd.exe') it is executing the command prompt executable (to open the command prompt). I then manually enter the telnet command in the command prompt opened with previous command, and I see the error message – aswerlein511 Jun 05 '17 at 18:20

2 Answers2

3

Some good next steps are:

  • Locate telnet.exe using WHERE, then invoke system with the path to telnet.exe.
  • Instead of system, I recommend using Open3.popen3() so that you can interact with the actual telnet program via STDIN, STDOUT, and STDERR.
  • Lastly, instead of any of the above options I would suggest that you use net-telnet as it exposes an easier API to use versus wrapping the Windows Telnet client.
David S.
  • 730
  • 1
  • 7
  • 23
  • 1
    Thanks for the response David. I was not able to execute telnet.exe from within Ruby...but I did use the net-telnet gem you mentioned, and it was able to do what I needed. Thanks! – aswerlein511 Jun 08 '17 at 12:03
0

cmd.exe is not located at the place from which you have run irb (or whatever is your ruby shell.

You need to pass the full-path to cmd.exe, include escapes for any illegal path characters, e.g. C:\some\folder\path\to\cmd.exe

system('start C:\some\folder\path\to\cmd.exe')
New Alexandria
  • 6,951
  • 4
  • 57
  • 77
  • 1
    Don't use back-slashes with a double-quoted string. You're asking for problems as several of the backslash+letter combinations are interpreted as escaped characters. Instead, do the easy thing and use forward slashes and let Ruby handle them for you. `"\some\folder\path\to\cmd.exe" # => " ome\folderpath\to\rd.exe"` – the Tin Man Jun 05 '17 at 20:27
  • 1
    I have tried using both the full path with the following command: system("start C:/\Windows/\System32/\/cmd.exe") that opens the prompt as expected, but again, the telnet issues still arises – aswerlein511 Jun 06 '17 at 11:35
  • @aswerlein511 sure, then you'll need to follow David S's solution for debugging that using `Open3` ruby lib – New Alexandria Jun 06 '17 at 14:54