Skip to content

Files

Latest commit

2afdde3 · Jun 4, 2023

History

History

example

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Jun 4, 2023
Jun 4, 2023
Jun 4, 2023

Example

This example accepts accepts two command line parameter that must be passed.

usage: node index.mjs [username] [host]
   or: cd .. && npm start -- [username] [host]
  1. Evaluates username and host
  2. Instantiates Scp Object
  3. Uploads in the currently logged in users home directory this file under the name testfile.md
  4. Downloads it again and saves it as testfile.md in the current working directory.

When you have to login with a password, you are able to enter it interactively direct to the remote shell.

The example/ directory should look like that

 .
 ├── README.md
 ├── index.cjs
 └── index.mjs
import { CreateScpConnection } from '../lib/index.mjs';

const urlPattern = /^[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/;
const ipPattern = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;

const host = process.argv.pop();
const user = process.argv.pop();

const is_valid = { host: false, user: false };

function printUsage() {
    console.error(`\n  usage: node . [username] [host]`);
    console.error(`     or: npm start -- [username] [host]`);
    process.exit(1);
}

async function testScp() {
    try {
        const scp = CreateScpConnection({host, user});
        const sendMsg = await scp.send({ destination: `/home/${user}/testfile.md`, source: './README.md'});
        const getMsg = await scp.get({ source: `/home/${user}/testfile.md`, destination: './testfile.md'});

        if (sendMsh || getMsg) {
            console.log(sendMsg, getMsg);
        }
    } catch(error) {
        console.error(error.message);
    }
}

if ((is_valid.host = urlPattern.test(host) || ipPattern.test(host)) 
&& (is_valid.user = (typeof user === 'string' && user.length > 0))) {
    testScp();
} else {
    let msg = !is_valid.host && `The hostname "${host}" is not a valid domain neither a valid IP address.\n`;
    msg = !is_valid.user ? `${msg} The provided username is not valid.\n` : msg; 

    console.error(msg);
    printUsage();
}

And something like this afterwards

 .
 ├── README.md
 ├── index.cjs
 ├── index.mjs
 └── testfile.md