Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. package com.servermonitor.servermonitor;
  2.  
  3. /**
  4.  * Created by ryan on 4/13/2017.
  5.  */
  6.  
  7. // TODO: promptYesNo()
  8. // TODO: possibly keyboardInteractive
  9.  
  10. import android.os.AsyncTask;
  11.  
  12. import com.jcraft.jsch.*;
  13.  
  14. import java.io.InputStream;
  15.  
  16. public class ssh
  17. {
  18.         public static String g_password = "";
  19.         public static class ConnectTask extends AsyncTask<String, Void, String>
  20.         {
  21.                 protected String doInBackground(String... params)
  22.                 {
  23.                         String host = params[0];
  24.                         String user = params[1];
  25.                         String password = params[2];
  26.                         int port = Integer.parseInt(params[3]);
  27.                         String command = params[4];
  28.                         g_password = password;
  29.                         String output = "";
  30.                         JSch jsch = new JSch();
  31.                         try {
  32.                                 Session session = jsch.getSession(user, host, port);
  33.                                 UserInfo ui = new MyUserInfo();
  34.                                 session.setUserInfo(ui); // currently hanging here
  35.                                 session.connect();
  36.                                 Channel channel = session.openChannel("exec");
  37.                                 ((ChannelExec) channel).setCommand(command);
  38.                                 channel.setInputStream(null);
  39.                                 ((ChannelExec) channel).setErrStream(System.err);
  40.                                 InputStream in = channel.getInputStream();
  41.                                 channel.connect();
  42.                                 byte[] tmp = new byte[1024];
  43.                                 while(true) {
  44.                                         while(in.available() > 0) {
  45.                                                 int i = in.read(tmp, 0, 1024);
  46.                                                 if(i < 0) break;
  47.                                                 output = output + new String(tmp, 0, i);
  48.                                         }
  49.                                         if(channel.isClosed()) {
  50.                                                 if(in.available() > 0) continue;
  51.                                                 break;
  52.                                         }
  53.                                         try {
  54.                                                 Thread.sleep(5);
  55.                                         } catch(Exception e) {
  56.                                                 System.out.println("Sleep Exception: " + e);
  57.                                                 e.printStackTrace();
  58.                                         }
  59.                                 }
  60.                                 channel.disconnect();
  61.                                 session.disconnect();
  62.                         } catch(Exception e) {
  63.                                 System.err.println("Exception: " + e);
  64.                                 e.printStackTrace();
  65.                         }
  66.                         MainActivity.output = output;
  67.                         MainActivity.connectComplete = true;
  68.                         return output;
  69.                 }
  70.         }
  71.  
  72.         static class MyUserInfo implements UserInfo, UIKeyboardInteractive
  73.         {
  74.                 public String getPassword()
  75.                 {
  76.                         return passwd;
  77.                 }
  78.  
  79.                 public boolean promptYesNo(String str)
  80.                 {
  81.                         // TODO
  82.                         return true;
  83.                 }
  84.  
  85.                 String passwd;
  86.  
  87.                 public String getPassphrase()
  88.                 {
  89.                         return null;
  90.                 }
  91.  
  92.                 public boolean promptPassphrase(String message)
  93.                 {
  94.                         return true;
  95.                 }
  96.  
  97.                 public boolean promptPassword(String message)
  98.                 {
  99.                         passwd = g_password;
  100.                         return true;
  101.                 }
  102.  
  103.                 public void showMessage(String message)
  104.                 {
  105.                         System.out.println("MSG: " + message);
  106.                 }
  107.  
  108.                 public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo)
  109.                 {
  110.                         return null;
  111.                 }
  112.         }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement