#ssh-client #ssh-config #client-server #bindings #automatic #part #protocols

sys ssh

Bindings to libssh, a library to write clients and servers for both version 1 and 2 of the protocol. It can also parse SSH config files and handle ProxyCommand automatically. Only the client part is implemented for now.

5 releases

Uses old Rust 2015

0.1.4 Feb 7, 2016
0.1.3 Feb 4, 2016
0.1.2 Jan 31, 2016
0.1.1 Jan 12, 2016
0.1.0 Jan 12, 2016

#15 in #ssh-config

Download history 27/week @ 2024-03-25 44/week @ 2024-04-01 17/week @ 2024-04-08 23/week @ 2024-04-15 30/week @ 2024-04-22 16/week @ 2024-04-29 24/week @ 2024-05-06 29/week @ 2024-05-13 28/week @ 2024-05-20 23/week @ 2024-05-27 28/week @ 2024-06-03 23/week @ 2024-06-10 19/week @ 2024-06-17 23/week @ 2024-06-24 12/week @ 2024-07-08

57 downloads per month

MIT/Apache

655KB
598 lines

Contains (ELF exe/lib, 625KB) build

Bindings to libssh. Unrelated to libssh2 (which also has rust bindings, see the "ssh2" crate),

Libssh is a client and server library supporting both versions 1 and 2 of the SSH protocol. The client part follows the behavior of openssh closely, in particular it parses ~/.ssh/config, and accepts ProxyCommand directives automatically.

Although this binding is Apache/MIT-licensed, libssl itself is released under the LGPL. Make sure you understand what it means if you plan to link statically (this crate links dynamically by default).

Client examples

use ssh::*;

let mut session=Session::new().unwrap();
session.set_host("pijul.org").unwrap();
session.parse_config(None).unwrap();
session.connect().unwrap();
println!("{:?}",session.is_server_known());
session.userauth_publickey_auto(None).unwrap();

Running a command on a remote server

 use ssh::*;
 use std::io::Read;

 let mut session=Session::new().unwrap();
 session.set_host("pijul.org").unwrap();
 session.parse_config(None).unwrap();
 session.connect().unwrap();
 println!("{:?}",session.is_server_known());
 session.userauth_publickey_auto(None).unwrap();
 {
     let mut s=session.channel_new().unwrap();
     s.open_session().unwrap();
     s.request_exec(b"ls -l").unwrap();
     s.send_eof().unwrap();
     let mut buf=Vec::new();
     s.stdout().read_to_end(&mut buf).unwrap();
     println!("{:?}",std::str::from_utf8(&buf).unwrap());
 }

Creating a remote file

 use ssh::*;
 use std::io::Write;

 let mut session=Session::new().unwrap();
 session.set_host("pijul.org").unwrap();
 session.parse_config(None).unwrap();
 session.connect().unwrap();
 println!("{:?}",session.is_server_known());
 session.userauth_publickey_auto(None).unwrap();
 {
     let mut scp=session.scp_new(WRITE,"/tmp").unwrap();
     scp.init().unwrap();
     let buf=b"blabla blibli\n".to_vec();
     scp.push_file("blublu",buf.len(),0o644).unwrap();
     scp.write(&buf).unwrap();
 }

Creating a remote directory with a file inside

 use ssh::*;
 use std::io::Write;

 let mut session=Session::new().unwrap();
 session.set_host("pijul.org").unwrap();
 session.parse_config(None).unwrap();
 session.connect().unwrap();
 println!("{:?}",session.is_server_known());
 session.userauth_publickey_auto(None).unwrap();
 {
     let mut scp=session.scp_new(RECURSIVE|WRITE,"/tmp").unwrap();
     scp.init().unwrap();
     scp.push_directory("testdir",0o755).unwrap();
     let buf=b"blabla\n".to_vec();
     scp.push_file("test file",buf.len(),0o644).unwrap();
     scp.write(&buf).unwrap();
 }

Reading a remote file

 use ssh::*;
 use std::io::Read;

 let mut session=Session::new().unwrap();
 session.set_host("pijul.org").unwrap();
 session.parse_config(None).unwrap();
 session.connect().unwrap();
 println!("{:?}",session.is_server_known());
 session.userauth_publickey_auto(None).unwrap();
 {
     let mut scp=session.scp_new(READ,"/tmp/blublu").unwrap();
     scp.init().unwrap();
     loop {
         match scp.pull_request().unwrap() {
             Request::NEWFILE=>{
                 let mut buf:Vec<u8>=vec!();
                 scp.accept_request().unwrap();
                 scp.reader().read_to_end(&mut buf).unwrap();
                 println!("{:?}",std::str::from_utf8(&buf).unwrap());
                 break;
             },
             Request::WARNING=>{
                 scp.deny_request().unwrap();
                 break;
             },
             _=>scp.deny_request().unwrap()
         }
     }
 }

Dependencies