Guest User

Rust (ssh2(libssh2))

a guest
Sep 20th, 2024
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. use ssh2::Session;
  2. use std::io::Read;
  3. use std::net::TcpStream;
  4.  
  5. fn main() {
  6. let username = "user";
  7. let password = "pass";
  8. let server = "server:22";
  9.  
  10. let tcp = TcpStream::connect(server).expect("Failed to connect to server");
  11. let mut sess = Session::new().expect("Failed to create session");
  12. sess.set_tcp_stream(tcp);
  13. sess.handshake().expect("Failed to handshake");
  14.  
  15. sess.userauth_password(username, password).expect("Failed to authenticate");
  16. assert!(sess.authenticated());
  17.  
  18. let run_command = |cmd: &str| {
  19. let mut channel = sess.channel_session().expect("Failed to create channel");
  20. channel.exec(cmd).expect("Failed to execute command");
  21. let mut output = String::new();
  22. channel.read_to_string(&mut output).expect("Failed to read output");
  23. println!("Output: {}", output);
  24. channel.wait_close().expect("Failed to close channel");
  25. };
  26. run_command("date +\"%T.%N\"");
  27. run_command("date +\"%T.%N\"");
  28. }
Advertisement
Add Comment
Please, Sign In to add comment