Coder Social home page Coder Social logo

Comments (1)

harmic avatar harmic commented on June 27, 2024

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

from async-ssh2.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.