0
votes

I'm developing an application in C#. The main idea is:

  1. Press button
  2. Open .bat file
  3. The .bat file opens Telnet [IP] [Port]
  4. .bat file executes the VBScript
  5. VBScript writes some commands to the telnet window

When I run this batch file by double-clicking on it, it works fine. However, wen I try to run it from a C# app, it doesn't work.

I already tried many methods.

Here is a few examples about what I tried:

Is it possible to use a batch file to establish a telnet session, send a command and have the output written to a file?

C# Winforms and command line batch files

batch works fine but "windows cannot find devcon.exe" in c# program

When I tried this:

string cmd = @"path";
var m_command = new System.Diagnostics.Process();
m_command.StartInfo.FileName = @"file.bat";
m_command.StartInfo.Arguments = cmd;
m_command.Start();

I got the error:

windows cannot find .exe make sure you typed the name correctly and then try again

And when I tried this:

string cmd = @"path";
var m_command = new System.Diagnostics.Process();
m_command.StartInfo.FileName = @"file.bat";
m_command.StartInfo.Arguments = cmd;
m_command.Start();

It works, but just opens telnet, the VBScript doesn't works.

This is the code in the .bat file:

:: Open a Telnet window
start C:\Windows\System32\telnet.exe 192.168.0.198 49211
:: Run the script 
cscript SendKeys.vbs 

This is the code in the .svs file:

set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 500
OBJECT.SendKeys "T{ENTER}" 
WScript.sleep 1000
OBJECT.SendKeys "T{ENTER}"
OBJECT.SendKeys " "

I expect the .VBS to write the command to the telnet window, however when I click the button in c# form, it just opens telnet, the VBScript doesn't works.

1
Don't do that. NEVER use SendKeys for automating the Windows telnet command. Instead use a telnet command that is actually scriptable, e.g. plink from the PuTTY suite. - Ansgar Wiechers
I do not write in C# nor even have Windows, but why not use Expect with TCL which was invented for just such tasks "talks" with terminal programs eg. telnet? - Slawomir Dziuba
Hello, @AnsgarWiechers! Thanks for asking me, but could you explain me why is not right use SenKeys ? - Esmeralda G.
SendKeys requires an interactive GUI (can't use it in a background process), and it will send keystrokes to whatever is the active window. Which may or may not be what you think it is. It's very unreliable and should not be used unless there are absolutely no other ways of dealing with the problem at hand. And perhaps not even then. - Ansgar Wiechers
I recommend to look on C# code posted on Stack Overflow or on other websites about telnet based access on other computers. For example C# Telnet Library or Telnet Scripting in C#. I have implemented a telnet client in an application written in C++ and compiled with nowadays very old Visual Studio 6 using a code found in world wide web and this C++ coded application still works from Windows 95 to Windows 10 without calling any other executable and without any additional DLL. - Mofi

1 Answers

0
votes

What you could do is find the cscript.exe file and execute it directly,like this:

for /f "tokens=*" %%C in ('where csscript.exe') do (%%C Sendkeys.vbs&goto Next)
:Next
rem This is only necessary because we want to run the script once

This migth work, but as the commenters said,using sendkeys is really not recommended,because it can cause major problems, once the windows are set incorrectly, or some of the progresses get delayed.