Socket
Socket
Sign inDemoInstall

@types/ssh2

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@types/ssh2

TypeScript definitions for ssh2


Version published
Weekly downloads
1.2M
increased by8.68%
Maintainers
1
Weekly downloads
 
Created

What is @types/ssh2?

@types/ssh2 provides TypeScript definitions for the ssh2 library, which is a client and server implementation of the SSH2 protocol. This package allows developers to use the ssh2 library with TypeScript, providing type safety and autocompletion features.

What are @types/ssh2's main functionalities?

Establishing an SSH Connection

This code sample demonstrates how to establish an SSH connection to a remote server and execute a command ('uptime') on that server. The connection is established using the ssh2 library, and the TypeScript definitions provided by @types/ssh2 ensure type safety.

const Client = require('ssh2').Client;
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: '127.0.0.1',
  port: 22,
  username: 'frylock',
  privateKey: require('fs').readFileSync('/here/is/my/key')
});

Setting up an SSH Server

This code sample demonstrates how to set up an SSH server that listens for incoming connections. The server authenticates clients using a username and password, and allows them to execute commands. The TypeScript definitions provided by @types/ssh2 ensure type safety for the ssh2 library's server functionality.

const { Server } = require('ssh2');
const fs = require('fs');
const server = new Server({
  hostKeys: [fs.readFileSync('host.key')]
}, (client) => {
  console.log('Client connected!');
  client.on('authentication', (ctx) => {
    if (ctx.method === 'password' && ctx.username === 'foo' && ctx.password === 'bar')
      ctx.accept();
    else
      ctx.reject();
  }).on('ready', () => {
    console.log('Client authenticated!');
    client.on('session', (accept, reject) => {
      const session = accept();
      session.on('exec', (accept, reject, info) => {
        console.log('Client wants to execute: ' + info.command);
        const stream = accept();
        stream.stderr.write('Oh no, the dreaded errors!
');
        stream.write('Just kidding about the errors!
');
        stream.exit(0);
        stream.end();
      });
    });
  }).on('end', () => {
    console.log('Client disconnected');
  });
}).listen(22, '127.0.0.1', () => {
  console.log('Listening on port 22');
});

Other packages similar to @types/ssh2

FAQs

Package last updated on 12 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc