Sample code for 30+ languages & platforms
Rust

SCP Upload File

See more SCP Examples

Demonstrates how to upload a file using the SCP protocol (Secure Copy Protocol over SSH).

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let ssh = chilkat::Ssh::new();

// Connect to an SSH server:

// Hostname may be an IP address or hostname:
let hostname = "www.some-ssh-server.com".to_string();
let port = 22;

if ssh.connect(&hostname, port).is_err() {
    println!("{}", ssh.last_error_text());
    return;
}

// Wait a max of 5 seconds when reading responses..
ssh.set_idle_timeout_ms(5000);

// Authenticate using login/password:
if ssh.authenticate_pw("myLogin", "myPassword").is_err() {
    println!("{}", ssh.last_error_text());
    return;
}

// Once the SSH object is connected and authenticated, we use it
// as the underlying transport in our SCP object.
let scp = chilkat::Scp::new();

if scp.use_ssh(&ssh).is_err() {
    println!("{}", scp.last_error_text());
    return;
}

let remote_path = "test.txt".to_string();
let local_path = "/home/bob/test.txt".to_string();
if scp.upload_file(&local_path, &remote_path).is_err() {
    println!("{}", scp.last_error_text());
    return;
}

println!("SCP upload file success.");

// Disconnect
ssh.disconnect();