hello,
I want to use windows' command line(cmd.exe) and executing some commands from there. But I couldnt send 2 or more parameter. My all codes are below;
-------------------------
string parametre1 = "telnet"
Process islem = new Process();
islem.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe";
islem.StartInfo.Arguments = parametre1;
islem.StartInfo.RedirectStandardOutput = true;
islem.StartInfo.UseShellExecute = false;
islem.Start();
islem.WaitForExit();
string output = islem.StandardOutput.ReadToEnd();
textBox1.text = output;
string snmp_parametre2 = "telnet 11.11.11.11"
...
------------------------
How can I execute "telnet.exe" and send parameters to it? Then how can I send more parameters?
---------
telnet 11.11.11.11
user:admin
pass:12345
...
---------
Loading
VulpesPosted May 27, 2011, 9:05 AM
I'm sure about this because the code I posted earlier (my second post in the thread) worked fine when I tried it. sleepy.bat contained the following commands:
dir
cd c:\
dir
The textbox contained the directory listings for both the current directory and the root directory.
Although you're right that you can feed commands interactively to the console by redirecting StandardInput, the synchronization issues can be challenging and it may therefore be difficult to get it to work correctly.
yokzuPosted May 31, 2011, 8:05 AM
Anyway Ill try other solutions. Thanks for your helps.
VulpesPosted May 30, 2011, 8:29 AM
Try instead:
string snmp_parametre1 = "[email protected]";
yokzuPosted May 30, 2011, 7:42 AM
----------
C:\>ssh [email protected]
[email protected]'s password:*****
Last login: Mon May 30 14:23:18 2011 from 172.31.1.195
root@ubuntu:~#
----------
Then I am using "ssh.exe" for connection. So I used the codes below;
---------
string snmp_parametre1 = "ssh [email protected]";
Process islem = new Process();
islem.StartInfo.FileName = "ssh";
islem.StartInfo.Arguments = snmp_parametre1;
islem.StartInfo.RedirectStandardOutput = true;
islem.StartInfo.UseShellExecute = false;
islem.Start();
islem.WaitForExit();
string output = islem.StandardOutput.ReadToEnd();
textBox1.Text = output;
----------
But I couldnt send parameter or command. Is it impossible to send a parameter to an .exe file like ssh?
(I could send the parameters to cmd.exe like you say.)
Sam HobbsPosted May 27, 2011, 6:50 AM
Try redirecting output from a command line; in other words, verify your assumption that the output can be redirected. I think it cannot be. I am unable to. Perhaps I am just doing it wrong but I think before you spend more time it is best to ensure that it is not a waste of time.
By definition of what telnet is, it uses formatting commands. It makes sense to me that it is not using standard output. Telnet is a terminal emulator, which is inconsistent with standard output. Telnet's output in a Unix system might use standard output since the command window in Unix systems emulate dumb terminals. Window's command windows do not emulate dumb terminals so I assume that Microsoft is converting the dumb terminal formatting commands to console API functions to do the formatting.
So if telnet won't work, what is the alternative? I think it is possible to use the networking classes to write something that does enough of the equivalent to get what is needed. It is nearly certain that there are already samples that can be used, at least to start with.
VulpesPosted May 27, 2011, 6:00 AM
So your first line should be:
string snmp_parametre1 = "/C C:\\command.bat";
yokzuPosted May 27, 2011, 2:14 AM
-------------------
string snmp_parametre1 = "C:\\command.bat";
Process islem = new Process();
islem.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe";
islem.StartInfo.Arguments = snmp_parametre1;
islem.StartInfo.RedirectStandardOutput = true;
islem.StartInfo.UseShellExecute = false;
islem.Start();
string output = islem.StandardOutput.ReadToEnd();
islem.WaitForExit();
textBox1.Text = output;
-------------------
Sam HobbsPosted May 26, 2011, 5:59 PM
Sam HobbsPosted May 26, 2011, 5:53 PM
For some bizarre reason I can't get telnet to execute from within my program, even if I execute it within cmd.exe. I can execute telnet outside my program but if I execute cmd.exe from within my program and then execute telnet it just keeps saying it cannot find it. I can exeute other programs that way but not telnet. It is strange. So the following is a sample Console program that uses the sort program and I hope it shows how to do what you need to do using telnet.
p.StartInfo.FileName = @"C:\Windows\System32\sort.exe";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
try { p.Start(); }
catch (Exception ex)
{
Console.WriteLine(ex);
return;
}
StreamWriter sw = p.StandardInput;
sw.WriteLine("zz");
sw.WriteLine("35345");
sw.WriteLine("dc");
sw.Close();
p.WaitForExit();
Console.WriteLine(p.StandardOutput.ReadToEnd());
VulpesPosted May 26, 2011, 10:38 AM
yokzuPosted May 26, 2011, 9:24 AM
For example I want to send 2 commands to "cmd.exe" and I wrote the codes below. But I can see just "Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved."
I cant see the command's output.
--------------
string snmp_parametre1 = "dir";
string snmp_parametre2 = "cd windows";
Process islem = new Process();
islem.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe";
islem.StartInfo.Arguments = snmp_parametre1;
islem.StartInfo.Arguments = snmp_parametre2;
islem.StartInfo.RedirectStandardOutput = true;
islem.StartInfo.UseShellExecute = false;
islem.Start();
islem.WaitForExit();
string output = islem.StandardOutput.ReadToEnd();
textBox1.Text = output;
----------
VulpesPosted May 26, 2011, 8:58 AM
If you wanted to send more than one parameter in the same session then just separate the parameters with a space.