<commit_before># NMSSH

NMSSH is a clean, easy-to-use, test driven framework for iOS and OSX that wraps libssh2.

The framework was initially built for usage in [Kleio](http://9muses.se/kleio) – a Mac OSX application that simplifies continuous deployment.

**Using libssh2 version:** `1.4.2`

Are you using NMSSH for something cool? [Let me know](http://twitter.com/Lejdborg).

## Usage

### Install the framework

* Build the framework and add it to your project. Consult the Wiki for detailed information about how to [build for OSX](https://github.com/Lejdborg/NMSSH/wiki/Build-and-use-in-your-OSX-project) or [build for iOS](https://github.com/Lejdborg/NMSSH/wiki/Build-and-use-in-your-iOS-project).
* Add `#include <NMSSH/NMSSH.h>` to your source file.

### Connect to a server

#### 1. Create a new session

    NMSSHSession *session = [NMSSHSession connectToHost:@"127.0.0.1:22"
                                           withUsername:@"user"];

    if ([session isConnected]) {
        NSLog(@"Successfully created a new session");
    }

#### 2.1. Authenticate by password

    [session authenticateByPassword:@"<PASSWORD>"];

#### 2.2. Or by public key

    // Explicitly set the public key that should be used...
    // Pass nil as password parameter for unprotected keys
    [session authenticateByPublicKey:@"~/.ssh/id_rsa.pub"
                         andPassword:@"<PASSWORD>"];

#### 2.3. Or connect to a SSH agent

    [session connectToAgent];

#### 3. Check if authentication was successful

    if ([session isAuthorized]) {
        NSLog(@"Authentication succeeded");
    }

#### 4. Don't forget to disconnect

    [session disconnect];
    session = nil;

### Using Channels

#### Executing shell commands

    NSError *error = nil;
    NSString *response = [[session channel] execute:@"echo foo" error:&error];
    NSLog(@"Response: %@", response);

#### SCP file transfer

The SCP API provides a simple way to upload or download files.

The `to:` parameter is flexible in that if you provide a directory, it will keep the same filename as in the from-parameter. But if you provide a complete file name you may name the transferred file anything you want.

#### Uploading files from your local computer

    BOOL success = [[session channel] uploadFile:@"~/my-local-file.txt" to:@"/var/www/"];

#### Downloading files from a server

    BOOL success = [[session channel] downloadFile:@"/var/www/my-remote-file.txt" to:@"~/"];

### SFTP

#### Connect to SFTP

    NMSFTP *sftp = [NMSFTP connectWithSession:session];

    if ([sftp isConnected]) {
        // do stuff...
    }

    [sftp disconnect];

#### Check if directory exists

    BOOL exits = [sftp directoryExistsAtPath:@"/var/www/"];

#### Create directory

    BOOL success = [sftp createDirectoryAtPath:@"/var/www/new-dir/"];

#### Delete directory

    BOOL success = [sftp removeDirectoryAtPath:@"/var/www/dir/"];

#### List contents of a directory

    NSArray *entries = [sftp contentsOfDirectoryAtPath:@"/var/www/"];
<commit_msg>Fix README spelling: "exits" -> "exists"<commit_after># NMSSH

NMSSH is a clean, easy-to-use, test driven framework for iOS and OSX that wraps libssh2.

The framework was initially built for usage in [Kleio](http://9muses.se/kleio) – a Mac OSX application that simplifies continuous deployment.

**Using libssh2 version:** `1.4.2`

Are you using NMSSH for something cool? [Let me know](http://twitter.com/Lejdborg).

## Usage

### Install the framework

* Build the framework and add it to your project. Consult the Wiki for detailed information about how to [build for OSX](https://github.com/Lejdborg/NMSSH/wiki/Build-and-use-in-your-OSX-project) or [build for iOS](https://github.com/Lejdborg/NMSSH/wiki/Build-and-use-in-your-iOS-project).
* Add `#include <NMSSH/NMSSH.h>` to your source file.

### Connect to a server

#### 1. Create a new session

    NMSSHSession *session = [NMSSHSession connectToHost:@"127.0.0.1:22"
                                           withUsername:@"user"];

    if ([session isConnected]) {
        NSLog(@"Successfully created a new session");
    }

#### 2.1. Authenticate by password

    [session authenticateByPassword:@"<PASSWORD>"];

#### 2.2. Or by public key

    // Explicitly set the public key that should be used...
    // Pass nil as password parameter for unprotected keys
    [session authenticateByPublicKey:@"~/.ssh/id_rsa.pub"
                         andPassword:@"<PASSWORD>"];

#### 2.3. Or connect to a SSH agent

    [session connectToAgent];

#### 3. Check if authentication was successful

    if ([session isAuthorized]) {
        NSLog(@"Authentication succeeded");
    }

#### 4. Don't forget to disconnect

    [session disconnect];
    session = nil;

### Using Channels

#### Executing shell commands

    NSError *error = nil;
    NSString *response = [[session channel] execute:@"echo foo" error:&error];
    NSLog(@"Response: %@", response);

#### SCP file transfer

The SCP API provides a simple way to upload or download files.

The `to:` parameter is flexible in that if you provide a directory, it will keep the same filename as in the from-parameter. But if you provide a complete file name you may name the transferred file anything you want.

#### Uploading files from your local computer

    BOOL success = [[session channel] uploadFile:@"~/my-local-file.txt" to:@"/var/www/"];

#### Downloading files from a server

    BOOL success = [[session channel] downloadFile:@"/var/www/my-remote-file.txt" to:@"~/"];

### SFTP

#### Connect to SFTP

    NMSFTP *sftp = [NMSFTP connectWithSession:session];

    if ([sftp isConnected]) {
        // do stuff...
    }

    [sftp disconnect];

#### Check if directory exists

    BOOL exists = [sftp directoryExistsAtPath:@"/var/www/"];

#### Create directory

    BOOL success = [sftp createDirectoryAtPath:@"/var/www/new-dir/"];

#### Delete directory

    BOOL success = [sftp removeDirectoryAtPath:@"/var/www/dir/"];

#### List contents of a directory

    NSArray *entries = [sftp contentsOfDirectoryAtPath:@"/var/www/"];

{'fraction_non_alphanumeric': 0.13345430550973275, 'fraction_numerical': 0.007588254701418674, 'mean_word_length': 3.365010799136069, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 4, 'lorem ipsum': 0, 'www.': 12, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6356469', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1758, 'n_words': 614}