Advertisement
Guest User

kerberos jsch help

a guest
Feb 21st, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. package lk.sachith.kerberos;
  2.  
  3. import com.jcraft.jsch.*;
  4.  
  5.  
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.util.Properties;
  9.  
  10. /**
  11.  * Created by swithana on 2/20/14.
  12.  */
  13. public class JSCHKerberosConnector {
  14.     public static void main() {
  15.  
  16.         String host = "**********";
  17.         String user = "swithana";
  18.         String paraphrase = "******";
  19.         String password = "*********";
  20.         String  command = "ls -ltr";
  21.         String privateKey = "/Users/swithana/id_rsa";
  22.  
  23.         JSch jsch = new JSch();
  24.         jsch.setLogger(new MyLogger());
  25.  
  26.         System.setProperty("java.security.krb5.conf", "/Users/swithana/git/KerberosConnector/src/main/resources/krb5.conf");
  27.         System.setProperty("java.security.auth.login.config", "/Users/swithana/git/KerberosConnector/src/main/resources/login.conf");
  28.         System.setProperty("javax.security.auth.useSubjectCredsOnly", "true");
  29.  
  30.  
  31.         try {
  32.            // jsch.addIdentity(privateKey,paraphrase);
  33.  
  34.             Session session = jsch.getSession(user, host, 22);
  35.             Properties config = new java.util.Properties();
  36.             config.put("StrictHostKeyChecking", "no");
  37.             config.put("PreferredAuthentications",
  38.                     "gssapi-with-mic");
  39.  
  40.             session.setConfig(config);
  41.             session.setPassword(password);
  42.             session.connect(20000);
  43.  
  44.             Channel channel = session.openChannel("exec");
  45.             ((ChannelExec) channel).setCommand( command);
  46.             channel.setInputStream(null);
  47.             ((ChannelExec) channel).setErrStream(System.err);
  48.  
  49.             InputStream in = channel.getInputStream();
  50.             channel.connect();
  51.             byte[] tmp = new byte[1024];
  52.             while (true) {
  53.                 while (in.available() > 0) {
  54.                     int i = in.read(tmp, 0, 1024);
  55.                     if (i < 0) break;
  56.                     System.out.print(new String(tmp, 0, i));
  57.                 }
  58.                 if (channel.isClosed()) {
  59.                     System.out.println("exit-status: " + channel.getExitStatus());
  60.                     break;
  61.                 }
  62.                 try {
  63.                     Thread.sleep(1000);
  64.                 } catch (Exception ee) {
  65.                 }
  66.             }
  67.             channel.disconnect();
  68.             session.disconnect();
  69.             System.out.println("DONE");
  70.  
  71.         } catch (JSchException e) {
  72.             e.printStackTrace();
  73.         } catch (IOException e) {
  74.             e.printStackTrace();
  75.         }
  76.     }
  77.     public static class MyLogger implements com.jcraft.jsch.Logger {
  78.         static java.util.Hashtable name=new java.util.Hashtable();
  79.         static{
  80.             name.put(new Integer(DEBUG), "DEBUG: ");
  81.             name.put(new Integer(INFO), "INFO: ");
  82.             name.put(new Integer(WARN), "WARN: ");
  83.             name.put(new Integer(ERROR), "ERROR: ");
  84.             name.put(new Integer(FATAL), "FATAL: ");
  85.         }
  86.         public boolean isEnabled(int level){
  87.             return true;
  88.         }
  89.         public void log(int level, String message){
  90.             System.err.print(name.get(new Integer(level)));
  91.             System.err.println(message);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement