How to use the Client function from ssh2

Find comprehensive JavaScript ssh2.Client code examples handpicked from public code repositorys.

ssh2.Client is a Node.js module that provides an SSH client implementation for establishing secure shell connections to remote servers.

17
18
19
20
21
22
23
24
25
26
  return err && err.stderr ? err.stderr : "<stderr empty>";
}

async connect(connectionInfo) {
  this.connectionInfo = connectionInfo;
  this.conn = new Client();

  return new Promise((resolve, reject) => {
    this.conn.on("error", (error) => {
      this.conn.end();
fork icon19
star icon43
watch icon4

+ 28 other calls in file

121
122
123
124
125
126
127
128
129
130
this.log.debug('try to read xml files');
if (this.config.ip === '127.0.0.1') {
	this.vcontrold_read(this.config.path + '/vcontrold.xml');
} else {
	//Create a SSH connection
	const ssh_session = new Client();
	this.log.debug('try to create a ssh session');
	ssh_session.connect({
		host: this.config.ip,
		username: this.config.user_name,
fork icon6
star icon16
watch icon8

+ 25 other calls in file

How does ssh2.Client work?

ssh2.Client is a module in Node.js that provides an easy-to-use interface for establishing SSH connections and executing commands on a remote server. It works by creating an SSH client instance that can be configured with connection options and event listeners. Once connected, you can execute commands and transfer files to and from the remote server. The module also supports advanced features such as agent forwarding, tunneling, and compression.

111
112
113
114
115
116
117
118
119
120
            "diffie-hellman-group14-sha1",
            "diffie-hellman-group1-sha1",
        ],
    },
};
var conn = new ssh2_1.Client();
conn
    .on("ready", function () {
    socket.emit("clear", true);
    socket.emit("data", "\r\n Connected to SSH Session: \r\n \r\n");
fork icon0
star icon3
watch icon3

7
8
9
10
11
12
13
14
15
16
17
const chokidar = require("chokidar");
const cliSpinners = require("cli-spinners");
const colors = require("colors");
const figlet = require("figlet");
const ssh2 = require('ssh2');
const SSH = ssh2.Client;


/**
 * Configuration.
 */
fork icon0
star icon2
watch icon2

+ 16 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const Client = require("ssh2").Client;

const conn = new Client();

conn
  .on("ready", function () {
    console.log("Client :: ready");
    conn.exec("uptime", function (err, stream) {
      if (err) throw err;
      stream
        .on("close", function (code, signal) {
          console.log(
            "Stream :: close :: code: " + code + ", signal: " + signal
          );
          conn.end();
        })
        .on("data", function (data) {
          console.log("STDOUT: " + data);
        })
        .stderr.on("data", function (data) {
          console.log("STDERR: " + data);
        });
    });
  })
  .connect({
    host: "example.com",
    port: 22,
    username: "username",
    privateKey: require("fs").readFileSync("/home/username/.ssh/id_rsa"),
  });

In this example, we create a new ssh2.Client object and connect to an SSH server with the provided credentials. Once the connection is established, we use the exec method to execute the uptime command on the server. We then listen for events on the resulting stream object to capture the output of the command. When the command finishes, we close the connection with conn.end().

-3
fork icon0
star icon0
watch icon1

+ 2 other calls in file

227
228
229
230
231
232
233
234
235
236
237
238
/*
* Attempts to connect to a host.
*/
function connect(nameOrInfo){


	var Client = ssh.Client;


	return getHost(nameOrInfo)
	.then(host => {
		return new Promise((success, rej) => {
fork icon0
star icon0
watch icon2

+ 23 other calls in file