Skip to main content
The execute() method allows you to run individual SSH commands on a remote server and receive the output. This is ideal for one-off commands that don’t require an interactive session.

Basic Usage

Once you have an established SSH connection, you can execute commands using the execute() method:
const command = 'ls -l';
client.execute(command)
  .then(output => console.log(output))
  .catch(error => console.error('Command failed:', error));

Common Examples

// List files in a directory
const files = await client.execute('ls -la /var/www');

// Check disk usage
const diskUsage = await client.execute('df -h');

// Find files modified in last 24 hours
const recentFiles = await client.execute('find /home -mtime -1');

Using Callbacks

You can also use the optional callback parameter instead of promises:
client.execute('whoami', (error, output) => {
  if (error) {
    console.error('Command failed:', error);
    return;
  }
  console.log('Current user:', output);
});

Chaining Commands

1

Chain with shell operators

You can chain multiple commands using shell operators like &&, ||, or ;:
// Execute commands sequentially (all must succeed)
await client.execute('cd /var/www && ls -l && pwd');

// Execute with fallback
await client.execute('command1 || command2');

// Execute regardless of previous command status
await client.execute('command1; command2; command3');
2

Use pipes

Pipe output between commands:
const result = await client.execute('ps aux | grep node | wc -l');
console.log('Number of Node processes:', result.trim());

Error Handling

Commands that fail will reject the promise. Always implement proper error handling:
try {
  const output = await client.execute('cat /etc/shadow');
  console.log(output);
} catch (error) {
  console.error('Permission denied or file not found:', error);
}

Best Practices

  • Keep it simple: The execute() method is designed for single commands. For complex interactions, use interactive shell instead.
  • Handle timeouts: Long-running commands may timeout. Consider using shell sessions for commands that take significant time.
  • Parse output: Command output is returned as a string. Parse it appropriately for your use case.
  • Security: Avoid passing user input directly into commands without sanitization to prevent command injection.

Processing Output

Here’s an example of parsing command output:
const output = await client.execute('ls -la');
const lines = output.split('\n').filter(line => line.trim());

const files = lines.slice(1).map(line => {
  const parts = line.split(/\s+/);
  return {
    permissions: parts[0],
    owner: parts[2],
    size: parts[4],
    name: parts.slice(8).join(' ')
  };
});

console.log('Files:', files);

Limitations

  • Commands are executed in a non-interactive environment
  • No persistent state between execute() calls (each command runs in a fresh context)
  • Standard output and error are combined in the response
  • For interactive commands or maintaining session state, use startShell() instead

Next Steps