nodejs series - how to use JS code to connect to a remote server and execute commands to create files - ssh2

what is ssh2?

  • The SSH2 client and server modules are written in pure JavaScript for node.js.
  • We can use it in the code to connect to the remote server and perform some necessary operations

Why use ssh2?

As a front-end, in some cases, we need to use code to log in to the Linux server and execute commands. What should we do at this time? Here I recommend a library-ssh2 for everyone. After using it, the front-end can also easily use code to handle some server command requirements~

start using

Come to a case~

Create an empty folder, create a new app.js, and put the following code

const { readFileSync } = require('fs');

const { Client } = require('ssh2');

const conn = new Client();
conn.on('ready', () => {
  console.log('Client :: ready');
  conn.exec('uptime', (err, stream) => {
    if (err) throw err;
    stream.on('close', (code, signal) => {
      console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
      conn.end();
    }).on('data', (data) => {
      console.log('STDOUT: ' + data);
    }).stderr.on('data', (data) => {
      console.log('STDERR: ' + data);
    });
  });
}).connect({
  host: '192.168.100.100',
  port: 22,
  username: 'frylock',
  privateKey: readFileSync('/path/to/my/key')
});

// example output:
// Client :: ready
// STDOUT:  17:41:15 up 22 days, 18:09,  1 user,  load average: 0.00, 0.01, 0.05
//
// Stream :: exit :: code: 0, signal: undefined
// Stream :: close

download dependencies

npm i fs ssh2 --save

Change the client configuration to our own server configuration

{
  host: '112.126.61.91',
  port: 22,
  username: 'root',
  privateKey: readFileSync('./key/ali/id_rsa')
}

For privateKey, please refer to my article Create and use the private key privateKey to log in to the SSH server

Execute the command and run successfully~

Read file directory example

const { readFileSync } = require('fs');

const { Client } = require('ssh2');

const conn = new Client();
conn.on('ready', () => {
  console.log('Client :: ready');
  conn.sftp((err, sftp) => {
    if (err) throw err;
    sftp.readdir('/home', (err, list) => {
      if (err) throw err;
      console.dir(list);
      conn.end();
    });
  });
//   conn.exec('uptime', (err, stream) => {
//     if (err) throw err;
//     stream.on('close', (code, signal) => {
//       console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
//       conn.end();
//     }).on('data', (data) => {
//       console.log('STDOUT: ' + data);
//     }).stderr.on('data', (data) => {
//       console.log('STDERR: ' + data);
//     });
//   });
}).connect({
  host: '112.126.61.91',
  port: 22,
  username: 'root',
  privateKey: readFileSync('./key/ali/id_rsa')
});

// example output:
// Client :: ready
// STDOUT:  17:41:15 up 22 days, 18:09,  1 user,  load average: 0.00, 0.01, 0.05
//
// Stream :: exit :: code: 0, signal: undefined
// Stream :: close

Enter the file directory and execute the command to create the file

const { readFileSync } = require('fs');

const { Client } = require('ssh2');

const conn = new Client();
conn.on('ready', () => {
  console.log('Client :: ready');
  conn.shell((err, stream) => {
    if (err) throw err;
    stream.on('close', () => {
      console.log('Stream :: close');
      conn.end();
    }).on('data', (data) => {
      console.log('OUTPUT: ' + data);
    });
    stream.end('cd /home\nls\ntouch xiaojin.txt\n');
  });
}).connect({
  host: '112.126.61.91',
  port: 22,
  username: 'root',
  privateKey: readFileSync('./key/ali/id_rsa')
});

// example output:
// Client :: ready
// STDOUT:  17:41:15 up 22 days, 18:09,  1 user,  load average: 0.00, 0.01, 0.05
//
// Stream :: exit :: code: 0, signal: undefined
// Stream :: close

I will write here today~

  • Friends, ( ̄ω ̄( ̄ω ̄〃 ( ̄ω ̄〃)ゝ See you tomorrow~~
  • Everyone be happy every day

Everyone is welcome to point out where the article needs to be corrected~
Learning is endless, cooperation is win-win

insert image description here

Welcome the little brothers and sisters passing by to put forward better opinions~~

Guess you like

Origin blog.csdn.net/tangdou369098655/article/details/128895184