From 3872d9fae805d83f33c3ac053b6301ea65a744c3 Mon Sep 17 00:00:00 2001 From: Matteo Bigoi Date: Mon, 16 Nov 2020 12:50:49 +0000 Subject: Update documentation to ensure sending and receiving files via SCP wait for the transfer to finish. Add Netconf example Signed-off-by: Bigo --- src/lib.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 036a769..f74e01e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,9 +101,15 @@ //! sess.handshake().unwrap(); //! sess.userauth_agent("username").unwrap(); //! +//! // Write the file //! let mut remote_file = sess.scp_send(Path::new("remote"), //! 0o644, 10, None).unwrap(); //! remote_file.write(b"1234567890").unwrap(); +//! // Close the channel and wait for the whole content to be tranferred +//! remote_file.send_eof().unwrap(); +//! remote_file.wait_eof().unwrap(); +//! remote_file.close().unwrap(); +//! remote_file.wait_close().unwrap(); //! ``` //! //! ## Download a file @@ -125,7 +131,83 @@ //! println!("remote file size: {}", stat.size()); //! let mut contents = Vec::new(); //! remote_file.read_to_end(&mut contents).unwrap(); -//! // ... +//! +//! // Close the channel and wait for the whole content to be tranferred +//! remote_file.send_eof().unwrap(); +//! remote_file.wait_eof().unwrap(); +//! remote_file.close().unwrap(); +//! remote_file.wait_close().unwrap(); +//! ``` +//! +//! ## Execute a Netconf XML payload +//! +//! ```no_run +//! use ssh2::{Channel, Session}; +//! use std::error::Error; +//! use std::io::prelude::*; +//! use std::net::TcpStream; +//! +//! const HELLO: &str = " +//! +//! urn:ietf:params:netconf:base:1.1 +//! +//! +//! ]]>]]>"; +//! +//! const PAYLOAD: &str = " +//! +//! EXECshow version +//! "; +//! +//! fn read(channel: &mut Channel) -> Result> { +//! let mut result = String::new(); +//! loop { +//! // If you plan to use this, be aware that reading 1 byte at a time is terribly +//! // inefficient and should be optimized for your usecase. This is just an example. +//! let mut buffer = [1u8; 1]; +//! let bytes_read = channel.read(&mut buffer[..])?; +//! let s = String::from_utf8_lossy(&buffer[..bytes_read]); +//! result.push_str(&s); +//! if result.ends_with("]]>]]>") { +//! println!("Found netconf 1.0 terminator, breaking read loop"); +//! break; +//! } +//! if result.ends_with("##") { +//! println!("Found netconf 1.1 terminator, breaking read loop"); +//! break; +//! } +//! if bytes_read == 0 || channel.eof() { +//! println!("Buffer is empty, SSH channel read terminated"); +//! break; +//! } +//! } +//! Ok(result) +//! } +//! +//! fn main() -> Result<(), Box> { +//! let tcp = TcpStream::connect("127.0.0.1:830")?; +//! let mut sess = Session::new()?; +//! sess.set_tcp_stream(tcp); +//! sess.handshake().unwrap(); +//! sess.userauth_password("user", "pass")?; +//! +//! let mut channel = sess.channel_session()?; +//! channel.subsystem("netconf")?; +//! let result = read(&mut channel)?; +//! println!("Result from connection:\n{}", result); +//! +//! let payload = format!("{}\n#{}\n{}\n##\n", HELLO, PAYLOAD.len(), PAYLOAD); +//! let a = channel.write(payload.as_bytes())?; +//! println!("Written {} bytes payload", a); +//! let result = read(&mut channel)?; +//! println!("Result from payload execution:\n{}", result); +//! +//! channel.send_eof()?; +//! channel.wait_eof()?; +//! channel.close()?; +//! channel.wait_close()?; +//! Ok(()) +//! } //! ``` #![doc(html_root_url = "https://docs.rs/ssh2")] -- cgit v1.2.3