Modules@robinpath/ssh
ssh

@robinpath/ssh

0.1.1Node.jsPublic

Remote server command execution and file management via SSH and SFTP

SSH

Remote server command execution and file management via SSH and SFTP

Package: @robinpath/ssh | Category: Web | Type: Utility

Authentication

ssh.connect "server" {"host": "example.com", "username": "admin", "password": "..."}

Call this once at the start of your script before using any other function. Credentials persist for the duration of the script execution.

Use Cases

Use the ssh module when you need to:

  • Execute a command on the remote server -- Use ssh.exec to perform this operation
  • Upload a local file to the remote server via SFTP -- Use ssh.upload to perform this operation
  • Download a remote file to local filesystem via SFTP -- Use ssh.download to perform this operation
  • Create a directory on the remote server -- Use ssh.mkdir to perform this operation
  • List files in a remote directory -- Use ssh.ls to perform this operation

Quick Reference

FunctionDescriptionReturns
connectConnect to an SSH serverConnection id
execExecute a command on the remote server{stdout, stderr, code}
uploadUpload a local file to the remote server via SFTP{uploaded}
downloadDownload a remote file to local filesystem via SFTP{downloaded}
mkdirCreate a directory on the remote serverTrue on success
lsList files in a remote directoryArray of {name, size, modifyTime, isDirectory}
rmRemove a file on the remote serverTrue on success
rmdirRemove a directory on the remote serverTrue on success
statGet file or directory stats from the remote server{size, modifyTime, accessTime, isDirectory, isFile}
readFileRead the contents of a remote file as a stringFile content as UTF-8 string
writeFileWrite string content to a remote fileTrue on success
closeClose an SSH connectionTrue if closed, false if not found
isConnectedCheck if an SSH connection is aliveTrue if connected

Functions

connect

Connect to an SSH server

Module: ssh | Returns: string -- Connection id

ssh.connect "server" {"host": "example.com", "username": "admin", "password": "..."}
ParameterTypeRequiredDescription
idstringYesConnection identifier
optionsobjectYes{host, port, username, password, privateKey, privateKeyPath, passphrase}

exec

Execute a command on the remote server

Module: ssh | Returns: object -- {stdout, stderr, code}

ssh.exec "server" "ls -la /var/log"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
commandstringYesShell command to execute

upload

Upload a local file to the remote server via SFTP

Module: ssh | Returns: object -- {uploaded}

ssh.upload "server" "./deploy.tar.gz" "/opt/app/deploy.tar.gz"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
localPathstringYesLocal file path
remotePathstringYesRemote destination path

download

Download a remote file to local filesystem via SFTP

Module: ssh | Returns: object -- {downloaded}

ssh.download "server" "/var/log/app.log" "./app.log"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
remotePathstringYesRemote file path
localPathstringYesLocal destination path

mkdir

Create a directory on the remote server

Module: ssh | Returns: boolean -- True on success

ssh.mkdir "server" "/opt/app/logs"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
remotePathstringYesRemote directory path

ls

List files in a remote directory

Module: ssh | Returns: array -- Array of {name, size, modifyTime, isDirectory}

ssh.ls "server" "/var/log"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
remotePathstringNoRemote directory path (default /)

rm

Remove a file on the remote server

Module: ssh | Returns: boolean -- True on success

ssh.rm "server" "/tmp/old-file.txt"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
remotePathstringYesRemote file path

rmdir

Remove a directory on the remote server

Module: ssh | Returns: boolean -- True on success

ssh.rmdir "server" "/tmp/old-dir"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
remotePathstringYesRemote directory path

stat

Get file or directory stats from the remote server

Module: ssh | Returns: object -- {size, modifyTime, accessTime, isDirectory, isFile}

ssh.stat "server" "/var/log/app.log"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
remotePathstringYesRemote path

readFile

Read the contents of a remote file as a string

Module: ssh | Returns: string -- File content as UTF-8 string

ssh.readFile "server" "/etc/hostname"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
remotePathstringYesRemote file path

writeFile

Write string content to a remote file

Module: ssh | Returns: boolean -- True on success

ssh.writeFile "server" "/tmp/config.txt" "key=value"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier
remotePathstringYesRemote file path
contentstringYesContent to write

close

Close an SSH connection

Module: ssh | Returns: boolean -- True if closed, false if not found

ssh.close "server"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier

isConnected

Check if an SSH connection is alive

Module: ssh | Returns: boolean -- True if connected

ssh.isConnected "server"
ParameterTypeRequiredDescription
connectionIdstringYesConnection identifier

Error Handling

All functions throw on failure. Common errors:

ErrorCause
SSH connection "..." not foundCheck the error message for details
@desc "Exec and validate result"
do
  set $result as ssh.exec "server" "ls -la /var/log"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step SSH workflow

Chain multiple ssh operations together.

@desc "Connect, exec, and more"
do
  set $r_connect as ssh.connect "server" {"host": "example.com", "username": "admin", "password": "..."}
  set $r_exec as ssh.exec "server" "ls -la /var/log"
  set $r_upload as ssh.upload "server" "./deploy.tar.gz" "/opt/app/deploy.tar.gz"
  print "All operations complete"
enddo

2. Safe connect with validation

Check results before proceeding.

@desc "Connect and validate result"
do
  set $result as ssh.connect "server" {"host": "example.com", "username": "admin", "password": "..."}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/ssh

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.5 KB
Versions1
Weekly Downloads23
Total Downloads23
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

web