Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.servermonitor.servermonitor;
- /**
- * Created by ryan on 4/13/2017.
- */
- // TODO: promptYesNo()
- // TODO: possibly keyboardInteractive
- import android.os.AsyncTask;
- import com.jcraft.jsch.*;
- import java.io.InputStream;
- public class ssh
- {
- public static String g_password = "";
- public static class ConnectTask extends AsyncTask<String, Void, String>
- {
- protected String doInBackground(String... params)
- {
- String host = params[0];
- String user = params[1];
- String password = params[2];
- int port = Integer.parseInt(params[3]);
- String command = params[4];
- g_password = password;
- String output = "";
- JSch jsch = new JSch();
- try {
- Session session = jsch.getSession(user, host, port);
- UserInfo ui = new MyUserInfo();
- session.setUserInfo(ui); // currently hanging here
- session.connect();
- Channel channel = session.openChannel("exec");
- ((ChannelExec) channel).setCommand(command);
- channel.setInputStream(null);
- ((ChannelExec) channel).setErrStream(System.err);
- InputStream in = channel.getInputStream();
- channel.connect();
- byte[] tmp = new byte[1024];
- while(true) {
- while(in.available() > 0) {
- int i = in.read(tmp, 0, 1024);
- if(i < 0) break;
- output = output + new String(tmp, 0, i);
- }
- if(channel.isClosed()) {
- if(in.available() > 0) continue;
- break;
- }
- try {
- Thread.sleep(5);
- } catch(Exception e) {
- System.out.println("Sleep Exception: " + e);
- e.printStackTrace();
- }
- }
- channel.disconnect();
- session.disconnect();
- } catch(Exception e) {
- System.err.println("Exception: " + e);
- e.printStackTrace();
- }
- MainActivity.output = output;
- MainActivity.connectComplete = true;
- return output;
- }
- }
- static class MyUserInfo implements UserInfo, UIKeyboardInteractive
- {
- public String getPassword()
- {
- return passwd;
- }
- public boolean promptYesNo(String str)
- {
- // TODO
- return true;
- }
- String passwd;
- public String getPassphrase()
- {
- return null;
- }
- public boolean promptPassphrase(String message)
- {
- return true;
- }
- public boolean promptPassword(String message)
- {
- passwd = g_password;
- return true;
- }
- public void showMessage(String message)
- {
- System.out.println("MSG: " + message);
- }
- public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo)
- {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement