> ## Documentation Index
> Fetch the complete documentation index at: https://dylankenneally-react-native-ssh-sftp-96.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# execute()

> Execute a command on the SSH server.

export const EditThisPage = ({filePath}) => {
  const REPO_EDIT_BASE_URL = 'https://github.com/dylankenneally/react-native-ssh-sftp/edit/master';
  return <div className="mt-10 rounded-2xl border border-zinc-950/10 bg-zinc-50 px-4 py-3 dark:border-white/10 dark:bg-white/5">
      <p className="m-0 text-sm text-zinc-600 dark:text-zinc-300">
        See something to improve?{' '}
        <a href={`${REPO_EDIT_BASE_URL}/${filePath}`}>Edit this page on GitHub</a>{' '}
        to suggest a change.
      </p>
    </div>;
};

## Overview

Executes a command on the connected SSH server and returns the output.

## Method Signature

```typescript theme={null}
execute(command: string, callback?: CallbackFunction<string>): Promise<string>
```

## Parameters

<ParamField path="command" type="string" required>
  The command to execute on the SSH server.
</ParamField>

<ParamField path="callback" type="CallbackFunction<string>" optional>
  Optional callback function to handle the result asynchronously.

  **Type Definition:**

  ```typescript theme={null}
  type CallbackFunction<T> = (error: any, response?: T) => void
  ```

  The callback receives:

  * `error`: Error object if the operation fails, otherwise `null` or `undefined`
  * `response`: The command output as a string (only present on success)
</ParamField>

## Return Value

<ResponseField name="Promise<string>" type="Promise<string>">
  Returns a Promise that resolves with the command output from the server.

  * **Resolves**: With the response string containing the command output
  * **Rejects**: With an error if the command execution fails
</ResponseField>

## Usage Example

```typescript theme={null}
import SSHClient from '@dylankenneally/react-native-ssh-sftp';

// Using Promise
const client = await SSHClient.connectWithPassword(
  'example.com',
  22,
  'username',
  'password'
);

try {
  const output = await client.execute('ls -la /home');
  console.log('Command output:', output);
} catch (error) {
  console.error('Execution failed:', error);
}

// Using callback
client.execute('pwd', (error, response) => {
  if (error) {
    console.error('Execution failed:', error);
    return;
  }
  console.log('Current directory:', response);
});
```

## Notes

* The command is executed in a non-interactive session
* Each call to `execute()` creates a new execution context
* For interactive shell sessions, use [startShell()](/api/ssh/shell) instead
* The method returns the combined stdout/stderr output from the command

<EditThisPage filePath="docs/api/ssh/execute.mdx" />
