Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 2, 2018 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/a3eb509034ae96bead2d9a48e39cab52 to your computer and use it in GitHub Desktop.
Save rust-play/a3eb509034ae96bead2d9a48e39cab52 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
extern crate ssh2;
extern crate byteorder;
use std::net::TcpStream;
use std::path::Path;
use ssh2::Session;
use std::io::prelude::*;
use std::fs;
use std::fs::File;
use std::io::BufReader;
use byteorder::{ReadBytesExt, NativeEndian};
fn main() {
println!("Hello, world!");
connect_to_server();
}
fn connect_to_server() {
// Almost all APIs require a `Session` to be available
let sess1 = Session::new().unwrap();
let mut agent = sess1.agent().unwrap();
// Connect the agent and request a list of identities
agent.connect().unwrap();
agent.list_identities().unwrap();
for identity in agent.identities() {
let identity = identity.unwrap(); // assume no I/O errors
println! ("{}", identity.comment());
let pubkey = identity.blob();
}
let tcp = TcpStream::connect("host:22").unwrap();
let mut sess = Session::new().unwrap();
sess.handshake(&tcp).unwrap();
sess.userauth_agent("root").unwrap();
let file_name = "a.war";
let file_size = fs::metadata(file_name).expect("could not get file meta data").len() as usize;
let file = File::open(file_name).expect("failed to open file");
let mut buf_reader = BufReader::new(file);
let mut buffer: Vec<u8> = Vec::with_capacity(file_size);
println! ("file_size {}", file_size as u64,);
buf_reader.read_to_end(&mut buffer).expect("failed to read");
let mut remote_file = sess.scp_send(Path::new(file_name),
0o644, file_size as u64, None).unwrap();
remote_file.write( buffer.as_mut_slice()).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment