Guest User

Untitled

a guest
Sep 24th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
  2. /**
  3. * This program will demonstrate how to exec 'sudo' on the remote.
  4. *
  5. */
  6. import com.jcraft.jsch.*;
  7. import java.awt.*;
  8. import javax.swing.*;
  9. import java.io.*;
  10.  
  11. public class Sudo{
  12. public static void main(String[] arg){
  13. try{
  14. JSch jsch=new JSch();
  15.  
  16. String host=null;
  17. if(arg.length>0){
  18. host=arg[0];
  19. }
  20. else{
  21. host=JOptionPane.showInputDialog("Enter username@hostname",
  22. System.getProperty("user.name")+
  23. "@localhost");
  24. }
  25. String user=host.substring(0, host.indexOf('@'));
  26. host=host.substring(host.indexOf('@')+1);
  27.  
  28. Session session=jsch.getSession(user, host, 22);
  29.  
  30. UserInfo ui=new MyUserInfo();
  31. session.setUserInfo(ui);
  32. session.connect();
  33.  
  34. String command=JOptionPane.showInputDialog("Enter command, execed with sudo",
  35. "printenv SUDO_USER");
  36.  
  37. String sudo_pass=null;
  38. {
  39. JTextField passwordField=(JTextField)new JPasswordField(8);
  40. Object[] ob={passwordField};
  41. int result=
  42. JOptionPane.showConfirmDialog(null,
  43. ob,
  44. "Enter password for sudo",
  45. JOptionPane.OK_CANCEL_OPTION);
  46. if(result!=JOptionPane.OK_OPTION){
  47. System.exit(-1);
  48. }
  49. sudo_pass=passwordField.getText();
  50. }
  51.  
  52. Channel channel=session.openChannel("exec");
  53.  
  54. // man sudo
  55. // -S The -S (stdin) option causes sudo to read the password from the
  56. // standard input instead of the terminal device.
  57. // -p The -p (prompt) option allows you to override the default
  58. // password prompt and use a custom one.
  59. ((ChannelExec)channel).setCommand("sudo -S -p '' "+command);
  60.  
  61.  
  62. InputStream in=channel.getInputStream();
  63. OutputStream out=channel.getOutputStream();
  64. ((ChannelExec)channel).setErrStream(System.err);
  65.  
  66. channel.connect();
  67.  
  68. out.write((sudo_pass+"\n").getBytes());
  69. out.flush();
  70.  
  71. byte[] tmp=new byte[1024];
  72. while(true){
  73. while(in.available()>0){
  74. int i=in.read(tmp, 0, 1024);
  75. if(i<0)break;
  76. System.out.print(new String(tmp, 0, i));
  77. }
  78. if(channel.isClosed()){
  79. System.out.println("exit-status: "+channel.getExitStatus());
  80. break;
  81. }
  82. try{Thread.sleep(1000);}catch(Exception ee){}
  83. }
  84. channel.disconnect();
  85. session.disconnect();
  86. }
  87. catch(Exception e){
  88. System.out.println(e);
  89. }
  90. }
  91.  
  92. public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
  93. public String getPassword(){ return passwd; }
  94. public boolean promptYesNo(String str){
  95. Object[] options={ "yes", "no" };
  96. int foo=JOptionPane.showOptionDialog(null,
  97. str,
  98. "Warning",
  99. JOptionPane.DEFAULT_OPTION,
  100. JOptionPane.WARNING_MESSAGE,
  101. null, options, options[0]);
  102. return foo==0;
  103. }
  104.  
  105. String passwd;
  106. JTextField passwordField=(JTextField)new JPasswordField(20);
  107.  
  108. public String getPassphrase(){ return null; }
  109. public boolean promptPassphrase(String message){ return true; }
  110. public boolean promptPassword(String message){
  111. Object[] ob={passwordField};
  112. int result=
  113. JOptionPane.showConfirmDialog(null, ob, message,
  114. JOptionPane.OK_CANCEL_OPTION);
  115. if(result==JOptionPane.OK_OPTION){
  116. passwd=passwordField.getText();
  117. return true;
  118. }
  119. else{
  120. return false;
  121. }
  122. }
  123. public void showMessage(String message){
  124. JOptionPane.showMessageDialog(null, message);
  125. }
  126. final GridBagConstraints gbc =
  127. new GridBagConstraints(0,0,1,1,1,1,
  128. GridBagConstraints.NORTHWEST,
  129. GridBagConstraints.NONE,
  130. new Insets(0,0,0,0),0,0);
  131. private Container panel;
  132. public String[] promptKeyboardInteractive(String destination,
  133. String name,
  134. String instruction,
  135. String[] prompt,
  136. boolean[] echo){
  137. panel = new JPanel();
  138. panel.setLayout(new GridBagLayout());
  139.  
  140. gbc.weightx = 1.0;
  141. gbc.gridwidth = GridBagConstraints.REMAINDER;
  142. gbc.gridx = 0;
  143. panel.add(new JLabel(instruction), gbc);
  144. gbc.gridy++;
  145.  
  146. gbc.gridwidth = GridBagConstraints.RELATIVE;
  147.  
  148. JTextField[] texts=new JTextField[prompt.length];
  149. for(int i=0; i<prompt.length; i++){
  150. gbc.fill = GridBagConstraints.NONE;
  151. gbc.gridx = 0;
  152. gbc.weightx = 1;
  153. panel.add(new JLabel(prompt[i]),gbc);
  154.  
  155. gbc.gridx = 1;
  156. gbc.fill = GridBagConstraints.HORIZONTAL;
  157. gbc.weighty = 1;
  158. if(echo[i]){
  159. texts[i]=new JTextField(20);
  160. }
  161. else{
  162. texts[i]=new JPasswordField(20);
  163. }
  164. panel.add(texts[i], gbc);
  165. gbc.gridy++;
  166. }
  167.  
  168. if(JOptionPane.showConfirmDialog(null, panel,
  169. destination+": "+name,
  170. JOptionPane.OK_CANCEL_OPTION,
  171. JOptionPane.QUESTION_MESSAGE)
  172. ==JOptionPane.OK_OPTION){
  173. String[] response=new String[prompt.length];
  174. for(int i=0; i<prompt.length; i++){
  175. response[i]=texts[i].getText();
  176. }
  177. return response;
  178. }
  179. else{
  180. return null; // cancel
  181. }
  182. }
  183. }
  184. }
Add Comment
Please, Sign In to add comment