Skip to content

Commit

Permalink
Merge pull request #60 from Miyoshi-Ryota/release-upload-file
Browse files Browse the repository at this point in the history
Release upload file
  • Loading branch information
Miyoshi-Ryota authored May 5, 2024
2 parents 49aacfd + 06fb848 commit 10cf660
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "async-ssh2-tokio"
version = "0.8.8"
version = "0.8.9"
edition = "2021"
license-file = "LICENSE"
readme = "README.md"
Expand All @@ -21,6 +21,6 @@ russh-keys = "0.40.1"
russh-sftp = "2.0.0-beta.4"
thiserror = "1.0"
async-trait = "0.1.61"
tokio = { version = "1.14.0", features = ["fs"] }
tokio = { version = "1", features = ["fs"] }

[dev-dependencies]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ for rust with the tokio runtime. Powered by the rust ssh implementation
```toml
[dependencies]
tokio = "1"
async-ssh2-tokio = "0.8.8"
async-ssh2-tokio = "0.8.9"
```

## Usage
```rust
use async_ssh2_tokio::client::{Client, AuthMethod, ServerCheckMethod};

#[tokio::main]
async fn main() -> Result<(), async_ssh2_tokio::Error> {
// if you want to use key auth, then use following:
Expand Down
19 changes: 19 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ impl Client {
.map_err(crate::Error::SshError)
}

/// Upload a file with sftp to the remote server.
///
/// `src_file_path` is the path to the file on the local machine.
/// `dest_file_path` is the path to the file on the remote machine.
/// Some sshd_config does not enable sftp by default, so make sure it is enabled.
/// A config line like a `Subsystem sftp internal-sftp` or
/// `Subsystem sftp /usr/lib/openssh/sftp-server` is needed in the sshd_config in remote machine.
pub async fn upload_file(
&self,
src_file_path: &str,
Expand Down Expand Up @@ -466,6 +473,7 @@ ASYNC_SSH2_TEST_CLIENT_PROT_PRIV
ASYNC_SSH2_TEST_CLIENT_PRIV
ASYNC_SSH2_TEST_CLIENT_PROT_PASS
ASYNC_SSH2_TEST_SERVER_PUB
ASYNC_SSH2_TEST_UPLOAD_FILE
",
)
}
Expand Down Expand Up @@ -791,4 +799,15 @@ ASYNC_SSH2_TEST_SERVER_PUB
assert_eq!(result1.stdout, "test clone\n");
assert_eq!(result2.stdout, "test clone2\n");
}

#[tokio::test]
async fn client_can_upload_file() {
let client = establish_test_host_connection().await;
let _ = client
.upload_file(&env("ASYNC_SSH2_TEST_UPLOAD_FILE"), "/tmp/uploaded")
.await
.unwrap();
let result = client.execute("cat /tmp/uploaded").await.unwrap();
assert_eq!(result.stdout, "this is a test file\n");
}
}
1 change: 1 addition & 0 deletions tests/async-ssh2-tokio/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ENV ASYNC_SSH2_TEST_SERVER_PUB=/root/server.ed25519.pub
ENV ASYNC_SSH2_TEST_HOST_PORT=22
ENV ASYNC_SSH2_TEST_HOST_NAME=ssh-server
ENV ASYNC_SSH2_TEST_KNOWN_HOSTS=/root/.ssh/known_hosts
ENV ASYNC_SSH2_TEST_UPLOAD_FILE=/async-ssh2-tokio/tests/async-ssh2-tokio/test-upload-file

COPY tests/async-ssh2-tokio/id_ed25519 /root/.ssh/id_ed25519
COPY tests/async-ssh2-tokio/id_ed25519.pub /root/.ssh/id_ed25519.pub
Expand Down
1 change: 1 addition & 0 deletions tests/async-ssh2-tokio/test-upload-file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a test file
3 changes: 3 additions & 0 deletions tests/sshd-test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ FROM linuxkit/sshd:62036c2a279715d05e8298b9269a0659964f2619-amd64

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN apk add openssh-sftp-server=9.1_p1-r5

# hadolint ignore=DL3006
RUN echo 'root:root' |chpasswd

RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN sed -ri 's/^#?PasswordAuthentication.*$/PasswordAuthentication yes/g' /etc/ssh/sshd_config
RUN sed -ri 's/^#Subsystem/Subsystem/g' /etc/ssh/sshd_config

COPY ssh_host_ed25519_key ssh_host_ed25519_key.pub /etc/ssh/
COPY authorized_keys /root/.ssh/authorized_keys
Expand Down

0 comments on commit 10cf660

Please sign in to comment.