Please what is the difference between these two code. code 1 gets stuck at this line
let mut channel = session.channel_open_session().await.unwrap();
while code 2 works fine. to me these two code are the same, only difference is just additional if else block added
Code 1
extern crate thrussh;
extern crate thrussh_keys;
// extern crate futures;
extern crate tokio;
// extern crate env_logger;
use std::sync::Arc;
use thrussh::*;
use thrussh::server::{Auth, Session};
use thrussh_keys::*;
// use futures::Future;
use std::io::Read;
use std::future;
use anyhow;
struct Client {
}
impl client::Handler for Client {
type Error = anyhow::Error;
type FutureUnit = future::Ready<Result<(Self, client::Session), anyhow::Error>>;
type FutureBool = future::Ready<Result<(Self, bool), anyhow::Error>>;
fn finished_bool(self, b: bool) -> Self::FutureBool {
future::ready(Ok((self, b)))
}
fn finished(self, session: client::Session) -> Self::FutureUnit {
future::ready(Ok((self, session)))
}
fn check_server_key(self, server_public_key: &key::PublicKey) -> Self::FutureBool {
println!("check_server_key: {:?}", server_public_key);
self.finished_bool(true)
}
fn channel_open_confirmation(self, channel: ChannelId, max_packet_size: u32, window_size: u32, session: client::Session) -> Self::FutureUnit {
println!("channel_open_confirmation: {:?}", channel);
self.finished(session)
}
fn data(self, channel: ChannelId, data: &[u8], session: client::Session) -> Self::FutureUnit {
println!("data on channel {:?}: {:?}", channel, std::str::from_utf8(data));
self.finished(session)
}
}
#[tokio::main]
async fn main() {
let config = thrussh::client::Config::default();
let config = Arc::new(config);
let sh = Client{};
// let key = thrussh_keys::key::KeyPair::generate_ed25519().unwrap();
// let mut agent = thrussh_keys::agent::client::AgentClient::connect_env().await.unwrap();
// agent.add_identity(&key, &[]).await.unwrap();
let mut session = thrussh::client::connect(config, "localhost", sh).await.unwrap();
if session.authenticate_password("username", "password").await.unwrap() {
println!("check_server_key::::::");
}
let mut channel = session.channel_open_session().await.unwrap();
println!("It did not get here, instead it get stuck at the above line");
channel.data(&b"Hello, world!"[..]).await.unwrap();
if let Some(msg) = channel.wait().await {
println!("{:?}", msg)
}
}
Code 2
use thrussh::{
ChannelId,
ChannelOpenFailure,
client::{self, Config, Handle},
};
use thrussh_keys::key;
use thrussh::client::Session;
struct SSHClient {}
impl client::Handler for SSHClient {
type Error = anyhow::Error;
type FutureBool = future::Ready<Result<(Self, bool), anyhow::Error>>;
type FutureUnit = future::Ready<Result<(Self, client::Session), anyhow::Error>>;
fn finished_bool(self, b: bool) -> Self::FutureBool {
future::ready(Ok((self, b)))
}
fn finished(self, session: client::Session) -> Self::FutureUnit {
future::ready(Ok((self, session)))
}
fn check_server_key(self, server_public_key: &key::PublicKey) -> Self::FutureBool {
self.finished_bool(true)
}
fn channel_open_failure(self, channel: ChannelId, reason: ChannelOpenFailure, description: &str, language: &str, mut session: Session) -> Self::FutureUnit {
println!("Failed to open channel");
future::ready(Err(anyhow::anyhow!("Failed")))
}
fn data(self, channel: ChannelId, data: &[u8], session: Session) -> Self::FutureUnit {
println!("message received {:?}", data);
self.finished(session)
}
}
#[tokio::main]
async fn main() -> (){
// use default configuration
let config = Config::default();
let config = Arc::new(config);
let mvp: SSHClient = SSHClient{};
let mut session: Handle<SSHClient>;
// connect to ssh
match thrussh::client::connect(config, "localhost:22", mvp).await {
Ok(sess) => {
session = sess;
println!("Success");
},
Err(err) => {
println!("Failed {}", err);
std::process::exit(1);
}
}
if session.authenticate_password("username", "password").await.is_ok() {
println!("logged in successfully");
} else {
println!("login failed", );
}
let mut channel = session.channel_open_session().await.unwrap();
channel.exec(false, "tail -f /var/log/messages").await.unwrap();
let chmsg = channel.wait().await.unwrap();
println!("{:?}", chmsg);
println!("It go here");
}