Struct ssh2::KnownHosts [] [src]

pub struct KnownHosts<'sess> { /* fields omitted */ }

A set of known hosts which can be used to verify the identity of a remote server.

Example

use std::env;
use std::path::Path;
use ssh2::{self, CheckResult, HostKeyType, KnownHostKeyFormat};
use ssh2::KnownHostFileKind;

fn check_known_host(session: &ssh2::Session, host: &str) {
    let mut known_hosts = session.known_hosts().unwrap();

    // Initialize the known hosts with a global known hosts file
    let file = Path::new(&env::var("HOME").unwrap()).join(".ssh/known_hosts");
    known_hosts.read_file(&file, KnownHostFileKind::OpenSSH).unwrap();

    // Now check to see if the seesion's host key is anywhere in the known
    // hosts file
    let (key, key_type) = session.host_key().unwrap();
    match known_hosts.check(host, key) {
        CheckResult::Match => return, // all good!
        CheckResult::NotFound => {}   // ok, we'll add it
        CheckResult::Mismatch => {
            panic!("host mismatch, man in the middle attack?!")
        }
        CheckResult::Failure => panic!("failed to check the known hosts"),
    }

    println!("adding {} to the known hosts", host);

    known_hosts.add(host, key, host, match key_type {
        HostKeyType::Rsa => KnownHostKeyFormat::SshRsa,
        HostKeyType::Dss => KnownHostKeyFormat::SshDss,
        HostKeyType::Unknown => panic!("unknown type of key!"),
    }).unwrap();
    known_hosts.write_file(&file, KnownHostFileKind::OpenSSH).unwrap();
}

Methods

impl<'sess> KnownHosts<'sess>
[src]

[src]

Reads a collection of known hosts from a specified file and adds them to the collection of known hosts.

[src]

Read a line as if it were from a known hosts file.

[src]

Writes all the known hosts to the specified file using the specified file format.

[src]

Converts a single known host to a single line of output for storage, using the 'type' output format.

Important traits for Hosts<'kh>
[src]

Create an iterator over all of the known hosts in this structure.

[src]

Delete a known host entry from the collection of known hosts.

[src]

Checks a host and its associated key against the collection of known hosts, and returns info back about the (partially) matched entry.

The host name can be the IP numerical address of the host or the full name. The key must be the raw data of the key.

[src]

Same as check, but takes a port as well.

[src]

Adds a known host to the collection of known hosts.

The host is the host name in plain text. The host name can be the IP numerical address of the host or the full name. If you want to add a key for a specific port number for the given host, you must provide the host name like '[host]:port' with the actual characters '[' and ']' enclosing the host name and a colon separating the host part from the port number. For example: "[host.example.com]:222".

The key provided must be the raw key for the host.

Trait Implementations

impl<'sess> Drop for KnownHosts<'sess>
[src]

[src]

Executes the destructor for this type. Read more

Auto Trait Implementations

impl<'sess> !Send for KnownHosts<'sess>

impl<'sess> !Sync for KnownHosts<'sess>