Hi friends,
I am trying to execute a command on a remote computer, a netstat call with specified port to be precise, in order to have an overview of who is connected to this computer via VNC.
I do have a (somewhat) functioning code using VB6 and PsExec,the performance of which is however rather poor and the code clunky.
Now that I am warming up to C#, I'd like to achieve a more elegant - and faster! - solution.
Here's my approach so far:
"CanPing" is a simple little function that returns true if a ping returns "Success":
5900 is the VNC port, M drive is mapped on the computer that shall run the command, temp folder exists.
My problem now is: The computer can be pinged, the code is being executed and I have a returnValue of 0 (which seems to be good according to Google) and a processID which gives me the idea that something is being done.
The output file however is not being created, so clearly something is wrong!
I have no idea what.
By the way: I made sure that the firewall options are properly set on my computer as well as on the destination.
Can anyone tell me whether my general approach will work at all or if I am missing something hopefully simple?
Thanks a bunch!
MakeItSo
“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
I am trying to execute a command on a remote computer, a netstat call with specified port to be precise, in order to have an overview of who is connected to this computer via VNC.
I do have a (somewhat) functioning code using VB6 and PsExec,the performance of which is however rather poor and the code clunky.
Now that I am warming up to C#, I'd like to achieve a more elegant - and faster! - solution.
Here's my approach so far:
Code:
/[COLOR=#73D216]* some stuff here
*/[/color]
using System.Management;
using WbemScripting;
[COLOR=#73D216]// form stuff[/color]
private void button1_Click(object sender, EventArgs e)
{
if(CanPing("servername"))
{
string remoteMachine = "servername";
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", remoteMachine), connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
[COLOR=#EF2929]inParams["CommandLine"] = "netstat -an | find \":5900\" >M:\temp\flog.txt";[/color]
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
String outMess= "Creation of the process returned: " + outParams["returnValue"] + "\n";
outMess=outMess + "Process ID: " + outParams["processId"];
MessageBox.Show(outMess);
}
}
"CanPing" is a simple little function that returns true if a ping returns "Success":
Code:
Boolean CanPing(String compi)
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply reply = ping.Send(compi);
return (reply.Status.ToString() == "Success");
}
5900 is the VNC port, M drive is mapped on the computer that shall run the command, temp folder exists.
My problem now is: The computer can be pinged, the code is being executed and I have a returnValue of 0 (which seems to be good according to Google) and a processID which gives me the idea that something is being done.
The output file however is not being created, so clearly something is wrong!
I have no idea what.
By the way: I made sure that the firewall options are properly set on my computer as well as on the destination.
Can anyone tell me whether my general approach will work at all or if I am missing something hopefully simple?
Thanks a bunch!
MakeItSo
“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.