Execute remote command (SSH)

Prerequisites

Modules

Checkout branches

git checkout origin/osp-scripts-configuration .

Description

In this tutorial you will learn how to enable a script to execute a command on a remote device. This method can be used for example to fetch information on a remote host, or launch some external process.

Logical schema

@startuml
skinparam backgroundColor transparent

    node script as "osp-scripts"
    node host as "remote-host"

script -[#black]> host : <size:11><color:black>**Via SSH execute command "ls / | wc -l"**
@enduml

Configuration

@startuml
skinparam backgroundColor transparent
package "module/script/script-1" {
        node ospscripts as "osp-scripts"
    [resources] as "module.resources"
}

package "root/script/" {
[script.js] as script
[detached.scripts] as detached.scripts
}

package "root/ssh/" {
[test] as "ssh-key"
[test.pub] as "ssh-pub-key"
}

resources -[#black]> test : <size:11><color:black>**ref**
resources -[#black]> test.pub : <size:11><color:black>**ref**

detached.scripts -[#black]> ospscripts : <size:11><color:black>**ref**
detached.scripts -[#black]> script : <size:11><color:black>**ref**
ospscripts -[#black]d-> script : <size:11><color:black>**Execute at schedule**
@enduml

Steps

1. Create a detached script

root/script/detached.scripts

The detached script has the task to execute the script every 30 seconds.

{
    "moduleId" : "modules.scripts.scripts-1",
    "sourceFile" : "root/script/script.js",
    "accessedValues" : [],
    "scheduledExecutions" : ["0/10 * * ? * * *"]
}

1. Create a script to execute on a remote target (via SSH)

root/script/script.js

Note

Change the IP/hostname and check if the SSH is enabled on your test target.

main();

function main() {
    
    log.info("Execute example script with schedule execution and certificate authentification");
    // Create the runner for SSH with CERTIFICATE
    let runner = ssh.certCmdBuilder("10.100.0.77", 51234, "osp", "test")
    // Set the maximum time to establish the connection (keep it low)
    .setConnectionTimeout(5)
    // Set a maximum time after witch the SSH drop the line.
    .setMaxCommandTime(5)
    .build();

    // Establish a SSH connexion and run a command.
    let res = runner.run(["echo \"executed\"", ">", "/tmp/script-execution-result"]);

    // If SSH failure
    if (res.exceptionCode !== 0) 
    {
        log.error("Fail to establish the SSH connection with error code [" + res.exceptionCode + "]");    
        return;
    }

    if (res.returnCode !== 0) 
    {
        log.error("Fail to execute the command error code [" + res.exceptionCode + "] and stdin [" + res.stdIn + "] and stderr [" + res.stdErr +"]" );    
        return;
    }
    
    log.info("Execution success output is [" + res.stdIn + "]");
}

3. Add the SSH certificates as resource

Note

The certificate must be generated in PEM format. The OPENSSH format is not supported by the lib used to establish the SSH connection. You can for example use ssh-keygen -t rsa -m PEM

modules/script/scripts-1/module.resources

{
    "resources": [
        {
            "source": "root/ssh/",
            "destination": "/certs"
        }
    ]
}

root/ssh/test

-----BEGIN RSA PRIVATE KEY-----
MIIG5AIBAAKCAYEAty6s/gRHdVxAky00IIxL8rIXNZYlhx9hhS7ptvIMd4vNSS0u
fBEaTDvI97xuz8B2QdrTKqR2JUWfI6VlSYD3Kc7ZZC61wQmLa2yqbKa6/b3AQ2pC
Wm5mmfw1aYvNrNuVgMwrLkXD2Bv4+qx8qVS8+oDBe8xd2YwmZXjJCMjHw/RpDhtl
r3Vj8+PbO2+vKojgd0XRoyRJUR6Kp0mUBTlhJQ+Vcq2/HUbVnxiS/AwHx0iqKsEB
d02kSFkWOJsVN2G4bsV0YFOoEDAkx54RexRvP/GoADt+4yneet2aj3/L5tsCAH3c
05lCxKFa3aW8Qn5m8DlrmJDNBWUg9beZFpjkFWlSrcOxWxuZaCk8L0DBKEcIWvDd
bFY6di+kTPPkWv069HvfKHk7DvHQekOlNIItDb66fM9cJ+K7unkYDLmB9KJYM21n
dKJW7NL0VzFGmRfLgSkZyMfl+SMsjy2R33CM41jz7BgVnelqiATBW6kpWZKGyccb
I632wGrIPO6oBYd3AgMBAAECggGAf2uNU+iAGGfCV14DO3y7u6cbs9+sXRB8Eltu
oR4/g8yQUI2IHy76XvrbMVA2uHaFDAv+BVR/BjAmoXIEQZve0Ysb9q7zKMLN4zyk
U42q1qPuIDbCzwqqfgRM1w0zVUEs/nmGs9o9f479osjmshbKmWmErOucgJ4Xe1AI
bv7SzkCt7u0pz2HV1Cm0LuTiMLEjinPbfiLYiP+4rOLmfrfKFjB/4VRwcxn5sIA0
hfSMWJf4HmNA9M7i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BWd6WqIBMFbqSlZkobJxxsjrfbAasg87qgFh3c= manticore@manticore

1. Deploy the SSH key on host

To authenticate with SSH key the host must have the public key listed inside its ~/.ssh/authorized_keys file. Use one of the following method :

use ssh-copy-id from a linux host

ssh-copy-id -i [] user@host

write the public key inside the file

cat [pubkey.pem] >> ~/.ssh/authorized_keys