java通过winrm实现remote powershell

java通过winrm实现remote powershell

来源:https://github.com/cloudsoft/winrm4j

winrm4j is a project which enables Java applications to execute batch or PowerShell commands on a remote Windows server using WinRM

You can download the latest binaries here, which also gives the details for adding winrm4j as a dependency to your project.

If you wish to build the binaries yourself, you can clone this project, and build it using Maven:
winrm4j是一个使Java应用程序能够使用WinRM在远程Windows服务器上执行批处理或PowerShell命令的项目

您可以在此处下载最新的二进制文件,其中还提供了将winrm4j作为依赖项添加到项目中的详细信息。

如果您希望自己构建二进制文件,可以克隆此项目,并使用Maven进行构建:

	 mvn clean install

Maven dependency
Add the following to your pom.xml:

<dependency>
  <groupId>io.cloudsoft.windows</groupId>
  <artifactId>winrm4j</artifactId>
  <version>0.13.0-SNAPSHOT</version> 
</dependency>

Java client usage

To use winrm4j in Java code, you first create a WinRmTool object via the static WinRmTool.Builder class. WinRmTool.Builder has several options which you might consider setting before instantiating a WinRmTool:

Java客户端使用

要在Java代码中使用winrm4j,首先通过静态WinRmTool.Builder类创建WinRmTool对象。Builder有几个选项,您可以在实例化WilrMoToT之前考虑设置:

 workingDirectory(String)
 environment(Map<String,String>)
 authenticationScheme(String)
 disableCertificateChecks(boolean)
 useHttps(boolean)
 port(int)

A WinRmTool instance supports the basic executeCommand(String) method which executes windows native commands. Through executeCommand(String) method supports:

executeCommand(List<String>) execute a list of commands concatenated with &
executePs(String) execute a PowerShell command with the native windows command
executePs(List<String>) execute a list of PowerShell commands
WinRmClientContext context = WinRmClientContext.newInstance();

WinRmTool tool = WinRmTool.Builder.builder("my.windows.server.com", "Administrator", "pa55w0rd!")
    .authenticationScheme(AuthSchemes.NTLM)
    .port(5985)
    .useHttps(false)
    .context(context)
    .build();

tool.executePs("echo hi");

context.shutdown();

WinRM 如何设置 TrustedHosts

如果希望 WinRM 能够建立连接,需要将 IP 地址设置为白名单。

查看当前 WinRM 的白名单
通过在命令行工具中运行下面的命令:

Get-Item WSMan:\localhost\Client\TrustedHosts

设置所有地址
下面的命令将会设置能够接受所有的配置。(双引号内可以设置ip)

Set-Item WSMan:\localhost\Client\TrustedHosts -Value '*'

添加到列表尾部
如果希望添加不同的地址到当前的信任列表中,但不修改当前的配置的话,需要使用关键字

-Concatenate

才可以。

例如可以使用下面的命令来进行添加。

Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'machineC' -Concatenate

遇到问题及解决:

服务端winrm修改设置:

winrm set winrm/config/service '@{AllowUnencrypted="true"}'
Java中使用WinRM技术需要使用WinRM4j库,下面是一个简单的示例代码: ```java import com.github.kubesys.winrm.WinRMFactory; import com.github.kubesys.winrm.WinRmTool; import com.github.kubesys.winrm.exception.WinRMException; import com.github.kubesys.winrm.ssl.TrustAllCerts; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URI; import java.nio.charset.StandardCharsets; public class WinRMExample { public static void main(String[] args) throws IOException, WinRMException { String hostname = "remote-server.example.com"; String username = "remote-user"; String password = "remote-password"; String endpoint = String.format("https://%s:5986/wsman", hostname); WinRmTool.Builder builder = WinRMFactory.builder(endpoint, username, password); builder.setAuthenticationScheme(WinRmTool.AuthenticationScheme.NTLM); builder.setHttpsNoVerification(); builder.setListener(new WinRmTool.Listener() { @Override public void onStdout(String message) { System.out.println("STDOUT: " + message); } @Override public void onStderr(String message) { System.err.println("STDERR: " + message); } }); WinRmTool tool = builder.build(); tool.executeCommand("ipconfig"); // 上传文件 String remoteFile = "C:\\test\\file.txt"; String localFile = "/path/to/local/file.txt"; byte[] content = "Hello, World!".getBytes(StandardCharsets.UTF_8); try (FileOutputStream out = new FileOutputStream(new File(localFile))) { out.write(content); } tool.copyToRemote(localFile, remoteFile); // 下载文件 byte[] remoteContent = tool.copyFromRemote(remoteFile); String downloadedFile = "/path/to/downloaded/file.txt"; try (FileOutputStream out = new FileOutputStream(new File(downloadedFile))) { out.write(remoteContent); } } } ``` 在上面的示例代码中,我们使用WinRMFactory创建WinRmTool对象,并设置了远程服务器的连接参数、身份验证方式、HTTPS证书验证方式和监听器。然后我们调用executeCommand方法执行命令,并使用copyToRemote和copyFromRemote方法上传和下载文件。需要注意的是,如果远程服务器没有安装WinRM服务,需要先安装WinRM服务并启用WinRM服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值