Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use ssh2::Session;
- use std::io::Read;
- use std::net::TcpStream;
- fn main() {
- let username = "user";
- let password = "pass";
- let server = "server:22";
- let tcp = TcpStream::connect(server).expect("Failed to connect to server");
- let mut sess = Session::new().expect("Failed to create session");
- sess.set_tcp_stream(tcp);
- sess.handshake().expect("Failed to handshake");
- sess.userauth_password(username, password).expect("Failed to authenticate");
- assert!(sess.authenticated());
- let run_command = |cmd: &str| {
- let mut channel = sess.channel_session().expect("Failed to create channel");
- channel.exec(cmd).expect("Failed to execute command");
- let mut output = String::new();
- channel.read_to_string(&mut output).expect("Failed to read output");
- println!("Output: {}", output);
- channel.wait_close().expect("Failed to close channel");
- };
- run_command("date +\"%T.%N\"");
- run_command("date +\"%T.%N\"");
- }
Advertisement
Add Comment
Please, Sign In to add comment