// create a wsman Shell instance; @see https://msdn.microsoft.com/en-us/library/cc251739.aspx public String openShell() { final boolean noprofile = false; // @TODO@ make param final int codepage = 437; // @TODO@ make param String messageId = UUID.randomUUID().toString(); HashMap<String, String> options = new HashMap<>(); options.put("WINRS_NOPROFILE", noprofile ? "TRUE" : "FALSE"); options.put("WINRS_CODEPAGE", String.valueOf(codepage)); prepareRequest(URI_ACTION_CREATE, null, messageId, options); // add SOAP body Shell shell = new Shell(); shell.getOutputStreams().add("stdout stderr"); shell.getInputStreams().add("stdin"); // shell.setEnvironment(); // shell.setIdleTimeout(); // shell.setWorkingDirectory(); // ws call CreateResponseType response = wsmanService.create(shell); Shell sh = (Shell) response.getAny(); return sh.getShellId(); // @TODO@ get shellId (from response) }
public void closeShell(String shellId) { String messageId = UUID.randomUUID().toString(); HashMap<String, String> options = null; // new HashMap<>(); prepareRequest(URI_ACTION_DELETE, shellId, messageId, options); // no SOAP body AnyXmlType response = wsmanService.delete(); }
public void cleanupCommand(String shellId, String commandId) { String messageId = UUID.randomUUID().toString(); HashMap<String, String> options = null; // new HashMap<>(); prepareRequest(URI_ACTION_SHELL_SIGNAL, shellId, messageId, options); // add SOAP body Signal signal = new Signal(); signal.setCommandId(commandId); signal.setCode(URI_SHELL_CODE_TERMINATE); // ws call SignalResponse response = wsmanService.signal(signal); }
public CommandOutput getCommandOutput(String shellId, String commandId) { String messageId = UUID.randomUUID().toString(); HashMap<String, String> options = null; // new HashMap<>(); prepareRequest(URI_ACTION_SHELL_RECEIVE, shellId, messageId, options); // add SOAP body Receive recv = new Receive(); DesiredStreamType stream = new DesiredStreamType(); stream.setCommandId(commandId); stream.getValue().add("stdout stderr"); recv.setDesiredStream(stream); CommandOutput commandOutput = new CommandOutput(); // fetch output in a loop until command finishes while (true) { ReceiveResponse response = wsmanService.receive(recv); // ws call for (StreamType streamItem : response.getStream()) { if (streamItem.getName().equals("stdout")) { try { commandOutput.std_out += Base64Utility.decode(new String(streamItem.getValue())); } catch (Base64Exception ex) { Logger.getLogger(Protocol.class.getName()).log(Level.SEVERE, null, ex); } } else if (streamItem.getName().equals("stderr")) { try { commandOutput.std_err += Base64Utility.decode(new String(streamItem.getValue())); } catch (Base64Exception ex) { Logger.getLogger(Protocol.class.getName()).log(Level.SEVERE, null, ex); } } } // quit loop when command output says 'done' if (response.getCommandState().getState().equals(COMMAND_STATE_DONE)) { commandOutput.statusCode = response.getCommandState().getExitCode().intValue(); break; } } return commandOutput; }
public String runCommand(String shellId, String command, String[] args) { final boolean consoleModeStdin = true; final boolean skipCmdShell = false; String messageId = UUID.randomUUID().toString(); HashMap<String, String> options = new HashMap<>(); options.put("WINRS_CONSOLEMODE_STDIN", consoleModeStdin ? "TRUE" : "FALSE"); options.put("WINRS_SKIP_CMD_SHELL", skipCmdShell ? "TRUE" : "FALSE"); prepareRequest(URI_ACTION_SHELL_COMMAND, shellId, messageId, options); // add SOAP body CommandLine theCommand = new CommandLine(); theCommand.setCommand(command); theCommand.getArguments().addAll(Arrays.asList(args)); // ws call CommandResponse response = wsmanService.command(theCommand); return response.getCommandId(); }