|
| 1 | +use async_trait::async_trait; |
| 2 | +use log::{error, info, LevelFilter}; |
| 3 | +use russh::*; |
| 4 | +use russh_keys::*; |
| 5 | +use russh_sftp::client::SftpSession; |
| 6 | +use std::sync::Arc; |
| 7 | +use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt}; |
| 8 | + |
| 9 | +struct Client; |
| 10 | + |
| 11 | +#[async_trait] |
| 12 | +impl client::Handler for Client { |
| 13 | + type Error = anyhow::Error; |
| 14 | + |
| 15 | + async fn check_server_key( |
| 16 | + self, |
| 17 | + server_public_key: &key::PublicKey, |
| 18 | + ) -> Result<(Self, bool), Self::Error> { |
| 19 | + info!("check_server_key: {:?}", server_public_key); |
| 20 | + Ok((self, true)) |
| 21 | + } |
| 22 | + |
| 23 | + async fn data( |
| 24 | + self, |
| 25 | + channel: ChannelId, |
| 26 | + data: &[u8], |
| 27 | + session: client::Session, |
| 28 | + ) -> Result<(Self, client::Session), Self::Error> { |
| 29 | + info!("data on channel {:?}: {}", channel, data.len()); |
| 30 | + Ok((self, session)) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[tokio::main] |
| 35 | +async fn main() { |
| 36 | + env_logger::builder() |
| 37 | + .filter_level(LevelFilter::Debug) |
| 38 | + .init(); |
| 39 | + |
| 40 | + let config = russh::client::Config::default(); |
| 41 | + let sh = Client {}; |
| 42 | + let mut session = russh::client::connect(Arc::new(config), ("localhost", 22), sh) |
| 43 | + .await |
| 44 | + .unwrap(); |
| 45 | + if session.authenticate_password("root", "pass").await.unwrap() { |
| 46 | + let mut channel = session.channel_open_session().await.unwrap(); |
| 47 | + channel.request_subsystem(true, "sftp").await.unwrap(); |
| 48 | + let sftp = SftpSession::new(channel.into_stream()).await.unwrap(); |
| 49 | + info!("current path: {:?}", sftp.canonicalize(".").await.unwrap()); |
| 50 | + |
| 51 | + // create dir and symlink |
| 52 | + let path = "./some_kind_of_dir"; |
| 53 | + let symlink = "./symlink"; |
| 54 | + |
| 55 | + sftp.create_dir(path).await.unwrap(); |
| 56 | + sftp.symlink(path, symlink).await.unwrap(); |
| 57 | + |
| 58 | + info!("dir info: {:?}", sftp.metadata(path).await.unwrap()); |
| 59 | + info!( |
| 60 | + "symlink info: {:?}", |
| 61 | + sftp.symlink_metadata(path).await.unwrap() |
| 62 | + ); |
| 63 | + |
| 64 | + // scanning directory |
| 65 | + for entry in sftp.read_dir(".").await.unwrap() { |
| 66 | + info!("file in directory: {:?}", entry.file_name()); |
| 67 | + } |
| 68 | + |
| 69 | + sftp.remove_file(symlink).await.unwrap(); |
| 70 | + sftp.remove_dir(path).await.unwrap(); |
| 71 | + |
| 72 | + // interaction with i/o |
| 73 | + let filename = "test_new.txt"; |
| 74 | + let mut file = sftp.create(filename).await.unwrap(); |
| 75 | + info!("metadata by handle: {:?}", file.metadata().await.unwrap()); |
| 76 | + |
| 77 | + file.write_all(b"magic text").await.unwrap(); |
| 78 | + info!("flush: {:?}", file.flush().await); // or file.sync_all() |
| 79 | + info!( |
| 80 | + "current cursor position: {:?}", |
| 81 | + file.stream_position().await |
| 82 | + ); |
| 83 | + |
| 84 | + let mut str = String::new(); |
| 85 | + |
| 86 | + file.rewind().await.unwrap(); |
| 87 | + file.read_to_string(&mut str).await.unwrap(); |
| 88 | + file.rewind().await.unwrap(); |
| 89 | + |
| 90 | + info!( |
| 91 | + "our magical contents: {}, after rewind: {:?}", |
| 92 | + str, |
| 93 | + file.stream_position().await |
| 94 | + ); |
| 95 | + |
| 96 | + file.shutdown().await.unwrap(); |
| 97 | + sftp.remove_file(filename).await.unwrap(); |
| 98 | + |
| 99 | + // should fail because handle was closed |
| 100 | + error!("should fail: {:?}", file.read_u8().await); |
| 101 | + } |
| 102 | +} |
0 commit comments