Struct Session

Source
pub struct Session<S: AsyncRead + AsyncWrite>(/* private fields */);
Expand description

An established and authenticated SSH session.

You can use this session to execute commands on the remote host using Session::open_exec. This will give you back a [Channel], which can be used to read from the resulting process’ STDOUT, or to write the the process’ STDIN.

Implementations§

Source§

impl<S: AsyncRead + AsyncWrite + Tcp + 'static> Session<S>

Source

pub fn new( stream: S, handle: &Handle, ) -> Result<NewSession<S>, HandlerError<()>>

Establish a new SSH session on top of the given stream.

The resulting SSH session is initially unauthenticated (see NewSession), and must be authenticated before it becomes useful.

Note that the reactor behind the given handle must continue to be driven for any channels created from this Session to work.

Examples found in repository?
examples/ls.rs (line 24)
12fn main() {
13    // async:
14
15    let mut core = tokio_core::reactor::Core::new().unwrap();
16    let handle = core.handle();
17
18    let cmd = ::std::env::args().skip(1).next().unwrap();
19    let key = thrussh_keys::load_secret_key("/home/jon/aws-test.pem", None).unwrap();
20
21    let ls_out = TcpStream::connect(&"52.23.157.12:22".parse().unwrap(), &handle)
22        .map_err(thrussh::Error::IO)
23        .map_err(thrussh::HandlerError::Error)
24        .and_then(|c| Session::new(c, &handle))
25        .and_then(|session| session.authenticate_key("ec2-user", key))
26        .and_then(|mut session| session.open_exec(&cmd));
27
28    let channel = core.run(ls_out).unwrap();
29    let (channel, data) = core.run(tokio_io::io::read_to_end(channel, Vec::new()))
30        .unwrap();
31    println!("{}", ::std::str::from_utf8(&data[..]).unwrap());
32    let status = core.run(channel.exit_status()).unwrap();
33    println!("{}", status);
34
35    /*
36
37    // sync:
38
39    // Connect to the local SSH server
40    let tcp = TcpStream::connect("127.0.0.1:22").unwrap();
41    let mut sess = Session::new().unwrap();
42    sess.handshake(&tcp).unwrap();
43    sess.userauth_agent("username").unwrap();
44
45    let mut channel = sess.channel_session().unwrap();
46    channel.exec("ls").unwrap();
47    let mut s = String::new();
48    channel.read_to_string(&mut s).unwrap();
49    println!("{}", s);
50    channel.wait_close();
51    println!("{}", channel.exit_status().unwrap());
52    */
53}
Source

pub fn last_error(&mut self) -> Option<HandlerError<()>>

Retrieve the last error encountered during this session.

Note that it is unlikely you will be able to use any items associated with this session once it has returned an error.

Calling this method clears the error.

Source

pub fn open_exec<'a>(&mut self, cmd: &'a str) -> ChannelOpenFuture<'a, S>

Establish a new channel over this session to execute the given command.

Note that any errors encountered while operating on the channel after it has been opened will manifest only as reads or writes no longer succeeding. To get the underlying error, call Session::last_error.

Examples found in repository?
examples/ls.rs (line 26)
12fn main() {
13    // async:
14
15    let mut core = tokio_core::reactor::Core::new().unwrap();
16    let handle = core.handle();
17
18    let cmd = ::std::env::args().skip(1).next().unwrap();
19    let key = thrussh_keys::load_secret_key("/home/jon/aws-test.pem", None).unwrap();
20
21    let ls_out = TcpStream::connect(&"52.23.157.12:22".parse().unwrap(), &handle)
22        .map_err(thrussh::Error::IO)
23        .map_err(thrussh::HandlerError::Error)
24        .and_then(|c| Session::new(c, &handle))
25        .and_then(|session| session.authenticate_key("ec2-user", key))
26        .and_then(|mut session| session.open_exec(&cmd));
27
28    let channel = core.run(ls_out).unwrap();
29    let (channel, data) = core.run(tokio_io::io::read_to_end(channel, Vec::new()))
30        .unwrap();
31    println!("{}", ::std::str::from_utf8(&data[..]).unwrap());
32    let status = core.run(channel.exit_status()).unwrap();
33    println!("{}", status);
34
35    /*
36
37    // sync:
38
39    // Connect to the local SSH server
40    let tcp = TcpStream::connect("127.0.0.1:22").unwrap();
41    let mut sess = Session::new().unwrap();
42    sess.handshake(&tcp).unwrap();
43    sess.userauth_agent("username").unwrap();
44
45    let mut channel = sess.channel_session().unwrap();
46    channel.exec("ls").unwrap();
47    let mut s = String::new();
48    channel.read_to_string(&mut s).unwrap();
49    println!("{}", s);
50    channel.wait_close();
51    println!("{}", channel.exit_status().unwrap());
52    */
53}

Auto Trait Implementations§

§

impl<S> Freeze for Session<S>

§

impl<S> !RefUnwindSafe for Session<S>

§

impl<S> !Send for Session<S>

§

impl<S> !Sync for Session<S>

§

impl<S> Unpin for Session<S>

§

impl<S> !UnwindSafe for Session<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.