Skip to content

Commit

Permalink
Add - sshCmd
Browse files Browse the repository at this point in the history
  • Loading branch information
POPPIN-FUMI committed Nov 7, 2024
1 parent 4caee70 commit 90bc8c9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
54 changes: 50 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ handling errors gracefully.

## Features

- **Simple API**: Minimal and easy-to-use functions `exec` and `spawnSync` for
command execution.
- **Simple API**: Minimal and easy-to-use functions `exec`, `spawnSync`, and
`sshCmd` for command execution.
- **Error Handling**: Robust error handling with detailed messages and status
codes.
- **Argument Parsing**: Advanced argument parsing that correctly handles quoted
Expand All @@ -26,13 +26,17 @@ Registry (JSR) or from Deno Land:
### Using JSR

```ts
import { exec, spawnSync } from 'jsr:@elsoul/child-process'
import { exec, spawnSync, sshCmd } from 'jsr:@elsoul/child-process'
```

### Using Deno Land

```ts
import { exec, spawnSync } from 'https://deno.land/x/child_process/mod.ts'
import {
exec,
spawnSync,
sshCmd,
} from 'https://deno.land/x/child_process/mod.ts'
```

## Usage
Expand Down Expand Up @@ -86,6 +90,35 @@ if (result.success) {
Process completed successfully.
```

### Executing SSH Commands Remotely

The `sshCmd` function allows you to execute commands on a remote server via SSH
using an RSA key for authentication. It also allows specifying the working
directory on the remote server.

```ts
import { sshCmd } from '@elsoul/child-process'

const result = await sshCmd(
'solv',
'x.x.x.x',
'~/.ssh/id_rsa',
'solana --version',
)

if (result.success) {
console.log('Command Output:', result.message)
} else {
console.error('Command Failed:', result.message)
}
```

**Output:**

```
Command Output: solana-cli 1.8.5 (src:1a2bc3d4)
```

### Handling Errors

Both `exec` and `spawnSync` provide detailed error information when a command
Expand Down Expand Up @@ -155,6 +188,19 @@ Spawns a child process synchronously, inheriting standard I/O streams.
- `cmd: string` - The command to execute.
- **Returns:** `Promise<ExecResult>` - The result of the process execution.

#### `sshCmd(user: string, host: string, rsaKeyPath: string, execCmd: string, cwd = '~'): Promise<ExecResult>`

Executes an SSH command on a remote server synchronously.

- **Parameters:**
- `user: string` - The SSH username to use for connection.
- `host: string` - The hostname or IP address of the remote server.
- `rsaKeyPath: string` - The path to the RSA private key for authentication.
- `execCmd: string` - The command to execute on the remote server.
- `cwd: string` - The working directory on the remote server. Defaults to the
home directory.
- **Returns:** `Promise<ExecResult>` - The result of the SSH command execution.

## Contributing

Contributions are welcome! Please feel free to submit a pull request or open an
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elsoul/child-process",
"version": "1.0.0",
"version": "1.1.0",
"description": "Deno child process module",
"runtimes": ["deno"],
"exports": "./mod.ts",
Expand Down
35 changes: 35 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,38 @@ const handleError = (error: unknown): ExecResult => {
}
}
}

/**
* Executes an SSH command on a remote server synchronously.
*
* This function constructs and executes an SSH command using a specified RSA key,
* user credentials, and host address. It navigates to the specified working directory (cwd),
* sources the user profile, and executes the provided command.
*
* @param {string} user - The SSH username to use for connection.
* @param {string} host - The hostname or IP address of the remote server.
* @param {string} rsaKeyPath - The path to the RSA private key for authentication.
* @param {string} execCmd - The command to execute on the remote server.
* @param {string} [cwd='~'] - The working directory on the remote server. Defaults to the home directory.
* @returns {Promise<ExecResult>} - A Promise that resolves to an object containing the success status and message.
*
* @example
* // Executes 'solana --version' command on a remote server
* const user = 'solv'
* const host = '145.40.126.169'
* const rsaKey = '~/.ssh/id_rsa'
* const execCmd = `solana --version`
*
* await sshCmd(user, host, rsaKey, execCmd)
*/
export const sshCmd = async (
user: string,
host: string,
rsaKeyPath: string,
execCmd: string,
cwd = '~',
): Promise<ExecResult> => {
const cmd =
`ssh -i ${rsaKeyPath} -o StrictHostKeyChecking=no ${user}@${host} -p 22 'cd ${cwd} && source ~/.profile && ${execCmd}'`
return await spawnSync(cmd)
}

0 comments on commit 90bc8c9

Please sign in to comment.