-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathssh_client.rs
84 lines (73 loc) · 2.24 KB
/
ssh_client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::consts::SSH_TIMEOUT;
use crate::errors::NetconfClientError;
use ssh2::{Channel, Session};
use std::io;
use std::io::{Read, Write};
use std::net::{IpAddr, SocketAddr, TcpStream};
use std::str::FromStr;
pub struct SSHClient {
host: String,
port: u16,
user: String,
password: String,
channel: Option<Channel>,
}
impl SSHClient {
pub fn create(host: &str, port: u16, user: &str, password: &str) -> SSHClient {
SSHClient {
host: host.to_owned(),
port,
user: user.to_owned(),
password: password.to_owned(),
channel: None,
}
}
pub fn connect(&mut self) -> Result<(), NetconfClientError> {
let ip_addr = IpAddr::from_str(&self.host)?;
let socket_address: SocketAddr = SocketAddr::from((ip_addr, self.port));
let tcp = TcpStream::connect(socket_address)?;
let mut session = Session::new()?;
session.set_timeout(SSH_TIMEOUT);
session.set_tcp_stream(tcp);
session.handshake()?;
session.userauth_password(&self.user, &self.password)?;
let mut channel = session.channel_session()?;
channel.subsystem("netconf")?;
self.channel = Some(channel);
Ok(())
}
pub fn eof(&self) -> io::Result<bool> {
Ok(self.channel.as_ref().unwrap().eof())
}
pub fn disconnect(&mut self) -> Result<(), NetconfClientError> {
let channel = self.channel.as_mut().unwrap();
channel.send_eof()?;
channel.wait_eof()?;
channel.close()?;
channel.wait_close()?;
self.channel = None;
Ok(())
}
}
impl Drop for SSHClient {
fn drop(&mut self) {
if self.channel.is_some() {
if let Result::Err(err) = self.disconnect() {
println!("SSH disconnect error: {}", err.to_string());
}
}
}
}
impl Write for SSHClient {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.channel.as_mut().unwrap().write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.channel.as_mut().unwrap().flush()
}
}
impl Read for SSHClient {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.channel.as_mut().unwrap().read(buf)
}
}