---
title: "@push.rocks/smartssh"
---
# @push.rocks/smartssh
setup SSH quickly and in a painless manner

## Install

To begin using `@push.rocks/smartssh` in your project, you'll need to install it via npm or yarn. You can do so by running one of the following commands:

```bash
npm install @push.rocks/smartssh --save
```

or

```bash
yarn add @push.rocks/smartssh
```

## Usage

`@push.rocks/smartssh` is a powerful package designed to simplify SSH configurations, key management, and interaction in a Typescript environment, using ESM syntax. This guide will cover how to utilize the primary functionalities provided by the package.

#### Setting Up an SSH Instance

An SSH instance represents your SSH configurations, including the keys and the SSH directory. Here's how to create an instance:

```typescript
import { SshInstance } from '@push.rocks/smartssh';

const mySshInstance = new SshInstance({
  sshDirPath: '/path/to/.ssh', // Optional: specify SSH directory path
  sshSync: true, // Optional: keep the instance in sync with the SSH directory automatically
});
```

#### Working with SSH Keys

SSH keys can be managed using the `SshKey` class. You can add, remove, or retrieve keys from your SSH instance.

```typescript
import { SshKey } from '@push.rocks/smartssh';

// Creating a new SSH key
const mySshKey = new SshKey({
  host: 'github.com', // Hostname
  private: 'privateKeyString', // Private key string
  public: 'publicKeyString', // Optional: public key string
  authorized: false // Optional: Is this key authorized? Defaults to false
});

// Adding the SSH key to the instance
mySshInstance.addKey(mySshKey);

// Getting an SSH key by host
const githubKey = mySshInstance.getKey('github.com');

// Removing an SSH key by instance
mySshInstance.removeKey(githubKey);
```

#### Syncing Keys with the File System

`@push.rocks/smartssh` makes it easy to synchronize your SSH keys with the file system, keeping your actual SSH configuration and your program state in alignment.

```typescript
// To write the current state to the SSH directory
mySshInstance.writeToDisk();

// To read and synchronize the state from the SSH directory
mySshInstance.readFromDisk();
```

#### Advanced Key Management

- **Encoding and Decoding**: Keys can be encoded in `base64` for easier environment variable storage.
- **Key Type Detection**: The package can detect and handle private, public, or both keys present scenarios (`duplex`).
- **Custom SSH Directory**: Support for custom SSH directory locations.
- **Automatic Syncing**: Optionally keep the SSH instance automatically synced with the SSH directory on modifications.

### Comprehensive Example

Below is a comprehensive example demonstrating SSH instance creation, adding a new SSH key, and syncing with the filesystem.

```typescript
import { SshInstance, SshKey } from '@push.rocks/smartssh';

async function setupSsh() {
  // Initialize the SSH instance
  const sshInstance = new SshInstance({
    sshDirPath: '/custom/path/to/.ssh',
    sshSync: true,
  });

  // Create a new SSH key
  const newSshKey = new SshKey({
    host: 'my.custom.server.com',
    private: 'myPrivateKeyInBase64',
    public: 'myPublicKeyInBase64',
  });

  // Add the new key to the instance
  sshInstance.addKey(newSshKey);

  // Optionally, write to disk immediately
  sshInstance.writeToDisk();
}

// Running the SSH setup
setupSsh().then(() => {
  console.log('SSH setup complete.');
}).catch((error) => {
  console.error('SSH setup failed:', error);
});
```

This guide should provide a robust start to managing SSH configurations using `@push.rocks/smartssh`. Whether for individual projects or shared across a team, this package offers a streamlined approach to handling SSH keys, config synchronization, and more, all within a TypeScript project.

## License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. 

**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

### Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

### Company Information

Task Venture Capital GmbH  
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
