Guest User

Untitled

a guest
May 7th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. public static void sendCommandWithSudo(String user, String pass, String ip, String command) {
  2.  
  3. try {
  4. JSch jsch = new JSch();
  5. Session session = jsch.getSession(user, ip, 22);
  6. Properties config = new Properties();
  7. config.put("StrictHostKeyChecking", "no");
  8. config.put("PreferredAuthentications", "password");
  9. session.setConfig(config);
  10. session.setPassword(pass);
  11.  
  12. System.out.println("IP Address: " + ip);
  13. System.out.println("Username: " + user);
  14. System.out.println("Password: " + pass);
  15. System.out.println("Command issued: " + command);
  16.  
  17. UserInfo ui = new MyUserInfo();
  18. session.setUserInfo(ui);
  19.  
  20. session.connect();
  21.  
  22. Channel channel = session.openChannel("shell");
  23. channel.setInputStream(System.in);
  24. channel.setOutputStream(System.out);
  25. PrintStream shellStream = new PrintStream(channel.getOutputStream());
  26.  
  27. channel.connect(3*1000);
  28.  
  29. List<String> commands = new ArrayList<String>();
  30. commands.add("sudo su - sudouser");
  31. commands.add(command);
  32.  
  33.  
  34. for(String cmd: commands) {
  35. shellStream.println(cmd);
  36. shellStream.flush();
  37. }
  38.  
  39. Thread.sleep(5000);
  40.  
  41. channel.disconnect();
  42. session.disconnect();
  43.  
  44. shellStream.close();
  45.  
  46. } catch (Exception e) {
  47. System.out.println("Exception caught: " + e.getMessage());
  48. }
  49.  
  50. }
Add Comment
Please, Sign In to add comment