async-ssh2 icon indicating copy to clipboard operation
async-ssh2 copied to clipboard

How to obtain the exit_signal structure?

Open araujobsd opened this issue 3 years ago • 1 comments

Hello,

I'm running a remote command and I'm wondering how to obtain the exit_signal and error_message when calling channel.exit_signal().

Would you have any examples?

araujobsd avatar Dec 07 '21 05:12 araujobsd

Here is a simple quick & dirty example:

use std::net::TcpStream;
use async_io::Async;
use async_std::task;
use async_std::net::ToSocketAddrs;
use async_std::io::ReadExt;
use async_ssh2::Session;

fn main() {

    task::block_on(async {
        // Connect to the local SSH server
        let addr = "127.0.0.1:22".to_socket_addrs().await
            .unwrap()
            .next()
            .unwrap();
        let tcp = Async::<TcpStream>::connect(addr).await.unwrap();
        let mut sess = Session::new().unwrap();
        sess.set_tcp_stream(tcp).unwrap();
        sess.handshake().await.unwrap();
        sess.userauth_password("username", "password").await.unwrap();

        let mut channel = sess.channel_session().await.unwrap();
        println!("Executing");
        channel.exec("sleep 90").await.unwrap();
        let mut s = String::new();
        channel.read_to_string(&mut s).await.unwrap();
        println!("{}", s);
        channel.wait_close().await.unwrap();
        let exit_sig = channel.exit_signal().unwrap();
        println!("Signal: {:?} Message: {:?}", exit_sig.exit_signal, exit_sig.error_message);
    });
}

If I run that and then kill the spawned 'sleep 90' command from another terminal, it prints this:

Signal: Some("TERM") Message: None

If I let it run to completion, I get this:

Signal: None Message: None

harmic avatar Dec 07 '21 07:12 harmic