|
67 | 67 | //!
|
68 | 68 | //! ```no_run
|
69 | 69 | //! use std::io::prelude::*;
|
70 |
| -//! use std::net::{TcpStream}; |
| 70 | +//! use std::net::TcpStream; |
71 | 71 | //! use ssh2::Session;
|
72 | 72 | //!
|
73 | 73 | //! // Connect to the local SSH server
|
|
140 | 140 | //! ```
|
141 | 141 | //!
|
142 | 142 | //! ## Execute a Netconf XML payload
|
143 |
| -//! |
| 143 | +//! |
144 | 144 | //! ```no_run
|
145 | 145 | //! use ssh2::{Channel, Session};
|
146 | 146 | //! use std::error::Error;
|
147 | 147 | //! use std::io::prelude::*;
|
148 | 148 | //! use std::net::TcpStream;
|
149 |
| -//! |
| 149 | +//! |
150 | 150 | //! const HELLO: &str = "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">
|
151 | 151 | //! <capabilities>
|
152 | 152 | //! <capability>urn:ietf:params:netconf:base:1.1</capability>
|
153 | 153 | //! </capabilities>
|
154 | 154 | //! </hello>
|
155 | 155 | //! ]]>]]>";
|
156 |
| -//! |
| 156 | +//! |
157 | 157 | //! const PAYLOAD: &str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
158 | 158 | //! <rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.1\" message-id=\"2\">
|
159 | 159 | //! <cli xmlns=\"http://cisco.com/ns/yang/cisco-nx-os-device\"><mode>EXEC</mode><cmdline>show version</cmdline></cli>
|
160 | 160 | //! </rpc>";
|
161 |
| -//! |
| 161 | +//! |
162 | 162 | //! fn read(channel: &mut Channel) -> Result<String, Box<dyn Error>> {
|
163 | 163 | //! let mut result = String::new();
|
164 | 164 | //! loop {
|
|
183 | 183 | //! }
|
184 | 184 | //! Ok(result)
|
185 | 185 | //! }
|
186 |
| -//! |
| 186 | +//! |
187 | 187 | //! fn main() -> Result<(), Box<dyn Error>> {
|
188 | 188 | //! let tcp = TcpStream::connect("127.0.0.1:830")?;
|
189 | 189 | //! let mut sess = Session::new()?;
|
190 | 190 | //! sess.set_tcp_stream(tcp);
|
191 | 191 | //! sess.handshake().unwrap();
|
192 | 192 | //! sess.userauth_password("user", "pass")?;
|
193 |
| -//! |
| 193 | +//! |
194 | 194 | //! let mut channel = sess.channel_session()?;
|
195 | 195 | //! channel.subsystem("netconf")?;
|
196 | 196 | //! let result = read(&mut channel)?;
|
197 | 197 | //! println!("Result from connection:\n{}", result);
|
198 |
| -//! |
| 198 | +//! |
199 | 199 | //! let payload = format!("{}\n#{}\n{}\n##\n", HELLO, PAYLOAD.len(), PAYLOAD);
|
200 | 200 | //! let a = channel.write(payload.as_bytes())?;
|
201 | 201 | //! println!("Written {} bytes payload", a);
|
202 | 202 | //! let result = read(&mut channel)?;
|
203 | 203 | //! println!("Result from payload execution:\n{}", result);
|
204 |
| -//! |
| 204 | +//! |
205 | 205 | //! channel.send_eof()?;
|
206 | 206 | //! channel.wait_eof()?;
|
207 | 207 | //! channel.close()?;
|
|
0 commit comments