Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void sendCommandWithSudo(String user, String pass, String ip, String command) {
- try {
- JSch jsch = new JSch();
- Session session = jsch.getSession(user, ip, 22);
- Properties config = new Properties();
- config.put("StrictHostKeyChecking", "no");
- config.put("PreferredAuthentications", "password");
- session.setConfig(config);
- session.setPassword(pass);
- System.out.println("IP Address: " + ip);
- System.out.println("Username: " + user);
- System.out.println("Password: " + pass);
- System.out.println("Command issued: " + command);
- UserInfo ui = new MyUserInfo();
- session.setUserInfo(ui);
- session.connect();
- Channel channel = session.openChannel("shell");
- channel.setInputStream(System.in);
- channel.setOutputStream(System.out);
- PrintStream shellStream = new PrintStream(channel.getOutputStream());
- channel.connect(3*1000);
- List<String> commands = new ArrayList<String>();
- commands.add("sudo su - sudouser");
- commands.add(command);
- for(String cmd: commands) {
- shellStream.println(cmd);
- shellStream.flush();
- }
- Thread.sleep(5000);
- channel.disconnect();
- session.disconnect();
- shellStream.close();
- } catch (Exception e) {
- System.out.println("Exception caught: " + e.getMessage());
- }
- }
Add Comment
Please, Sign In to add comment