SSH

Utilities functions for SSH

Description:
  • Utilities functions for SSH

Source:
Example
import {
  getSSHClient,
  sshConnect,
  joinPath,
  zipFolder,
  sshPutFile,
  sshExecCommand,
} from "nsuite";
import { PATH_ROOT } from "#scripts/ConstantUtils";
import { sshConfig } from "#hosts/Shanghai-Tencent/nginx/build/config";

const sshClient = getSSHClient();
await sshConnect({
  ssh: sshClient,
  ...sshConfig,
});
const pathDist = joinPath(PATH_ROOT, "apps-home/blog", "dist");
const pathDistZip = joinPath(pathDist, "../dist.zip");
await zipFolder({
  pathFolder: pathDist,
  pathOutputFile: pathDistZip,
});

const pathRemote = "/www/sites/www.orzzone.com/public";
const pathRemoteZip = `${pathRemote}/dist.zip`;
await sshPutFile({
  ssh: sshClient,
  localFile: pathDistZip,
  remoteFile: pathRemoteZip,
});

const execCommand = async (command: string): Promise<void> => {
  await sshExecCommand({
    ssh: sshClient,
    cwd: pathRemote,
    command,
  });
};
await execCommand("rm dist");
await execCommand("unzip -o dist.zip");
await execCommand("rm dist.zip");

process.exit(0);

Methods

(static) getSSHClient() → {module:SSH~SSH}

Description:
  • Get SSH instance

Source:
Returns:
Type
module:SSH~SSH

(static) sshConnect(payload) → {Promise.<void>}

Description:
  • Connect to SSH

Source:
Parameters:
Name Type Description
payload module:SSH~ParamsConnect
Returns:
Type
Promise.<void>

(static) sshExecCommand(payload) → {Promise.<void>}

Description:
  • Execute command

Source:
Parameters:
Name Type Description
payload module:SSH~ParamsExecCommand
Returns:
Type
Promise.<void>

(static) sshGetDirectory(payload) → {Promise.<boolean>}

Description:
  • Download directory from remote server

Source:
Parameters:
Name Type Description
payload module:SSH~ParamsSSHGetDir
Returns:
Type
Promise.<boolean>

(static) sshGetFile(payload) → {Promise.<void>}

Description:
  • Download file from server

Source:
Parameters:
Name Type Description
payload module:SSH~ParamsSSHGetFile
Returns:
Type
Promise.<void>

(static) sshPutDirectory(payload) → {Promise.<module:SSH~ReturnPutDir>}

Description:
  • Put directory

Source:
Parameters:
Name Type Description
payload module:SSH~ParamsPutDir
Returns:
Type
Promise.<module:SSH~ReturnPutDir>

(static) sshPutFile(payload) → {Promise.<void>}

Description:
  • Put file to server

Source:
Parameters:
Name Type Description
payload module:SSH~ParamsSSHGetFile
Returns:
Type
Promise.<void>

(static) sshPutFiles(payload) → {Promise.<void>}

Description:
  • Put files

Source:
Parameters:
Name Type Description
payload module:SSH~ParamsPutFiles
Returns:
Type
Promise.<void>

Type Definitions

ParamsConnect

Source:
Properties:
Name Type Description
ssh module:SSH~SSH
host string
port number
username string
password string
Type:
  • Object

ParamsExecCommand

Source:
Properties:
Name Type Attributes Description
ssh module:SSH~SSH
cwd string
command string
onStdout function <optional>
onStderr function <optional>
Type:
  • Object

ParamsPutDir

Source:
Properties:
Name Type Attributes Description
ssh module:SSH~SSH
fromPath string
toPath string
options module:SSH~GetPutDirectoryOptions <optional>
uploadCallback SSHUploadFileCallback <optional>

callback function for upload progress

Type:
  • Object

ParamsPutFiles

Source:
Properties:
Name Type Attributes Description
ssh module:SSH~SSH
files Array.<module:SSH~PathPair>
options module:SSH~PutFilesOptions <optional>
Type:
  • Object

ParamsSSHGetDir

Source:
Properties:
Name Type Attributes Description
ssh module:SSH~SSH
localDirectory string
remoteDirectory string
options module:SSH~GetPutDirectoryOptions <optional>
Type:
  • Object

ParamsSSHGetFile

Source:
Properties:
Name Type Attributes Description
ssh module:SSH~SSH
localFile string
remoteFile string
givenSftp module:ssh2~SFTPWrapper | null <optional>
transferOptions module:ssh2~TransferOptions <optional>
Type:
  • Object

PathPair

Source:
Properties:
Name Type Description
local string
remote string
Type:
  • Object

PutFilesOptions

Source:
Type:
  • module:node-ssh~NodeSSH

ReturnPutDir

Source:
Properties:
Name Type Description
success boolean
failItems Array.<module:SSH~PathPair>
successItems Array.<module:SSH~PathPair>
Type:
  • Object

SSHUploadFileCallback(local, remote, error)

Source:
Parameters:
Name Type Description
local string
remote string
error Error | null