0

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.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
  • 3
    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`](https://the.earth.li/~sgtatham/putty/0.71/htmldoc/Chapter7.html#plink) from the [PuTTY suite](https://www.chiark.greenend.org.uk/~sgtatham/putty/). – Ansgar Wiechers Jul 11 '19 at 16:14
  • 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 Jul 11 '19 at 16:18
  • Hello, @AnsgarWiechers! Thanks for asking me, but could you explain me why is not right use **SenKeys** ? – Esmeralda G. Jul 11 '19 at 16:25
  • I think you need to launch CMD.EXE (the command processor) with the name of the file to run as a command line argument. – Steve Todd Jul 11 '19 at 16:27
  • When you call the Process class in c# the environmental variable PAT is null so any executables in the bat file must have full path names to any executable including ones in c:\Windows\System32 folder. – jdweng Jul 11 '19 at 16:28
  • 5
    `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 Jul 11 '19 at 16:31
  • `SendKeys` to another window is hacky – Eliahu Aaron Jul 11 '19 at 16:52
  • 1
    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](https://stackoverflow.com/questions/390188/) or [Telnet Scripting in C#](https://www.c-sharpcorner.com/article/telnet-scripting-in-C-Sharp/). 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 Jul 11 '19 at 18:39
  • Hey, did you get this working? – Hans Apr 25 '20 at 15:20

1 Answers1

0

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.

Szecska
  • 11
  • 1
  • 5