INNER CODE UNIT · JavaScript
execCommandWithTimeout
bvisible/mcp-ssh-manager · src/index.js:314
async function execCommandWithTimeout(ssh, command, options = {}, timeoutMs = 30000) {
// Pass through rawCommand and platform if specified
const { rawCommand, platform = 'linux', ...otherOptions } = options;
// Windows targets: encode the command as PowerShell -EncodedCommand (UTF-16
// LE base64). This is the standard approach (used by Ansible / Chef / Puppet)
// because cmd.exe's quoting rules are inconsistent across versions and break
// commands containing $vars, $(...) subexpressions, double-quoted strings,
// pipes, etc. Base64 sidesteps all escape issues entirely.
if (platform === 'windows' && !rawCommand) {
// Suppress progress (avoids CLIXML sentinels in stderr) + force UTF-8 stdout
const prelude = '$ProgressPreference=\'SilentlyContinue\'; [Console]::OutputEncoding=[System.Text.Encoding]::UTF8;';
const fullPSCommand = `${prelude} ${command}`;
const utf16le = Buffer.from(fullPSCommand, 'utf16le');
const b64 = utf16le.toString('base64');
// -OutputFormat Text prevents stderr/info streams from being CLIXML-encoded
const wrappedCommand = `powershell -NoProfile -OutputFormat Text -EncodedCommand ${b64}`;
return ssh.execCommand(wrappedCommand, { ...otherOptions, execOptions: { ...(otherOptions.execOptions || {}) } });