2024-08-23 04:43:26 -07:00
|
|
|
// @ts-check
|
|
|
|
import { $ } from 'zx';
|
|
|
|
|
|
|
|
export class SshClient {
|
|
|
|
/**
|
|
|
|
*
|
2024-09-02 04:58:24 -07:00
|
|
|
* @param {{ privateKeyPath: string; ip: string; username: string; verbose?: boolean }} param0
|
2024-08-23 04:43:26 -07:00
|
|
|
*/
|
2024-09-02 04:58:24 -07:00
|
|
|
constructor({ privateKeyPath, ip, username, verbose = false }) {
|
2024-08-23 04:43:26 -07:00
|
|
|
this.verbose = verbose;
|
2024-09-02 04:58:24 -07:00
|
|
|
this.privateKeyPath = privateKeyPath;
|
|
|
|
this.ip = ip;
|
|
|
|
this.username = username;
|
2024-08-23 04:43:26 -07:00
|
|
|
|
|
|
|
this.$$ = $({
|
|
|
|
verbose,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} command
|
|
|
|
* @param {{ verbose?: boolean }} [options]
|
|
|
|
*/
|
|
|
|
async ssh(command, options = {}) {
|
|
|
|
const $$ = options?.verbose ? $({ verbose: true }) : this.$$;
|
|
|
|
|
2024-09-02 04:58:24 -07:00
|
|
|
const target = `${this.username}@${this.ip}`;
|
|
|
|
|
|
|
|
await $$`ssh -i ${this.privateKeyPath} -o StrictHostKeyChecking=accept-new ${target} ${command}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async scp(source, destination) {
|
|
|
|
const target = `${this.username}@${this.ip}:${destination}`;
|
|
|
|
await this
|
|
|
|
.$$`scp -i ${this.privateKeyPath} -o StrictHostKeyChecking=accept-new ${source} ${target}`;
|
2024-08-23 04:43:26 -07:00
|
|
|
}
|
|
|
|
}
|