1 package org.metricshub.winrm.command;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.IOException;
24 import java.nio.charset.Charset;
25 import java.nio.file.Path;
26 import java.util.List;
27 import java.util.concurrent.TimeoutException;
28 import java.util.stream.Collectors;
29 import org.metricshub.winrm.TimeoutHelper;
30 import org.metricshub.winrm.Utils;
31 import org.metricshub.winrm.WinRMHttpProtocolEnum;
32 import org.metricshub.winrm.WindowsRemoteCommandResult;
33 import org.metricshub.winrm.WindowsRemoteProcessUtils;
34 import org.metricshub.winrm.exceptions.WindowsRemoteException;
35 import org.metricshub.winrm.exceptions.WqlQuerySyntaxException;
36 import org.metricshub.winrm.service.WinRMEndpoint;
37 import org.metricshub.winrm.service.WinRMService;
38 import org.metricshub.winrm.service.client.auth.AuthenticationEnum;
39 import org.metricshub.winrm.shares.SmbTempShare;
40
41 public class WinRMCommandExecutor {
42
43 private WinRMCommandExecutor() {}
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public static WindowsRemoteCommandResult execute(
85 final String command,
86 final WinRMHttpProtocolEnum protocol,
87 final String hostname,
88 final Integer port,
89 final String username,
90 final char[] password,
91 final String workingDirectory,
92 final long timeout,
93 final List<String> localFileToCopyList,
94 final Path ticketCache,
95 final List<AuthenticationEnum> authentications
96 ) throws IOException, TimeoutException, WindowsRemoteException {
97 Utils.checkNonNull(command, "command");
98 Utils.checkArgumentNotZeroOrNegative(timeout, "timeout");
99
100 final long start = System.currentTimeMillis();
101
102 final WinRMEndpoint winRMEndpoint = new WinRMEndpoint(protocol, hostname, port, username, password, null);
103
104 if (localFileToCopyList == null || localFileToCopyList.isEmpty()) {
105 try (
106 final WinRMService winRMService = WinRMService.createInstance(
107 winRMEndpoint,
108 timeout,
109 ticketCache,
110 authentications
111 )
112 ) {
113 final Charset charset = WindowsRemoteProcessUtils.getWindowsEncodingCharset(
114 winRMService,
115 TimeoutHelper.getRemainingTime(timeout, start, "No time left to retrieve the code set")
116 );
117
118 return winRMService.executeCommand(command, workingDirectory, charset, timeout);
119 } catch (final WqlQuerySyntaxException e) {
120 throw new IOException(e);
121 }
122 }
123
124 try (
125 final SmbTempShare smbTempShare = SmbTempShare.createInstance(
126 winRMEndpoint,
127 timeout,
128 ticketCache,
129 authentications
130 )
131 ) {
132 smbTempShare.checkConnectedFirst();
133
134 final List<String> localFiles = localFileToCopyList
135 .stream()
136 .filter(Utils::isNotBlank)
137 .collect(Collectors.toList());
138
139
140 final String localFilesUpdatedCommand = WindowsRemoteProcessUtils.copyLocalFilesToShare(
141 command,
142 localFiles,
143 smbTempShare.getUncSharePath(),
144 smbTempShare.getRemotePath()
145 );
146
147 final Charset charset = WindowsRemoteProcessUtils.getWindowsEncodingCharset(
148 smbTempShare.getWindowsRemoteExecutor(),
149 TimeoutHelper.getRemainingTime(timeout, start, "No time left to retrieve the code set")
150 );
151
152 return smbTempShare
153 .getWindowsRemoteExecutor()
154 .executeCommand(
155 String.format("CMD.EXE /C (%s)", localFilesUpdatedCommand),
156 null,
157 charset,
158 TimeoutHelper.getRemainingTime(timeout, start, "No time left to execute command")
159 );
160 } catch (final WqlQuerySyntaxException e) {
161 throw new IOException(e);
162 }
163 }
164 }