C # remote command execution memo

Source: Internet
Author: User

In C #, You Can Use WMI to execute commands on a remote machine (WMI: Windows Management Interface) to access the components of different operating systems (Windows) through a public interface, it can be used to efficiently manage remote and local computers. It is also the control core of w2k3, w2k8, and XP management systems. The following is a demonstration of this work.Code:

//////////////////////////////////////// //////////////////////////////////////// //////////////////

// Connectionoptions specifies the settings required to generate the WMI connection

Connectionoptions connoption = new connectionoptions ();
Connoption. Username = domain + @ "\" + username;
Connoption. Password = password;

// Managementpath encapsulates the path for generating and analyzing WMI objects

Managementpath mngpath = new managementpath (@ "\" + serverhostname + @ "\ Root \ cimv2: win32_process ");
Managementscope scope = new managementscope (mngpath, connoption );
Scope. Connect ();

// The objectgetoptions class specifies the options used to obtain management objects.

Objectgetoptions objoption = new objectgetoptions ();

// Managementclass indicates the Common Information Model (CIM) management class. Members of this class can use specific WMI class paths to access WMI data.
Managementclass classinstance = new managementclass (scope, mngpath, objoption );

Int processid = 0;
Object [] cmdline = {"CMD/C" + strcommand, path, null, processid };

// Call the method for executing the command
Classinstance. invokemethod ("CREATE", cmdline );

Specifically, domain is the domain name used to log on to the remote machine, username, and password are the account and password used to log on to the remote machine.

Serverhostname is the name or IP address of the remote server to be accessed.

Strcommand is a command that needs to be executed on a remote machine.

//////////////////////////////////////// //////////////////////////////////////// //////////////////

 

In C #, you can use the client channel for transmitting messages over HTTP to implement remote calls. The following is the sample code:

// First establish a channel and register the channel service

Httpchannel c = new httpchannel ();
Channelservices. registerchannel (C, false );

// Call the executable file to perform the operation

Object remoteobject = activator. GetObject (type. GetType (remoteobject), remoteobjecturl );
Remoteobject jsonalobj = (remoteobject) remoteobject;
Export alobj. runcommand (exefilepath );

// Disable the channel

Channelservices. unregisterchannel (C );

Public class remoteobject: marshalbyrefobject
{
Public String runcommand (string cmd)
{
PROCESS p = new process ();
P. startinfo. filename = "cmd.exe ";
P. startinfo. useshellexecute = false;
P. startinfo. redirectstandardinput = true;
P. startinfo. redirectstandardoutput = true;
P. startinfo. redirectstandarderror = true;
P. startinfo. createnowindow = true;
P. Start ();
P. standardinput. writeline (CMD );
P. standardinput. writeline ("exit ");
P. Close ();

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.