1 package com.example.lede2;
\r
4 * Created by rtrimana on 9/25/17.
\r
7 import android.app.ProgressDialog;
\r
8 import android.os.AsyncTask;
\r
9 import android.os.SystemClock;
\r
10 import android.util.Log;
\r
12 import com.jcraft.jsch.Channel;
\r
13 import com.jcraft.jsch.ChannelExec;
\r
14 import com.jcraft.jsch.JSch;
\r
15 import com.jcraft.jsch.JSchException;
\r
16 import com.jcraft.jsch.Session;
\r
18 import java.io.IOException;
\r
19 import java.io.InputStream;
\r
20 import java.util.ArrayList;
\r
21 import java.util.Arrays;
\r
22 import java.util.List;
\r
26 import java.io.IOException;
\r
27 import java.io.InputStream;
\r
28 import java.lang.String;
\r
29 import java.util.ArrayList;
\r
30 import java.util.Arrays;
\r
31 import java.util.List;
\r
33 // AsyncTask input : command line
\r
34 // AysncTask output : output from a command
\r
35 public class SSH_MySQL extends AsyncTask<String, Void, List<String>> {
\r
37 // variables used for connection
\r
38 private Session session;
\r
39 private Channel channel;
\r
40 private ChannelExec ce;
\r
41 // in this project, we supposed we use fixed host, username, password
\r
42 private String host;
\r
43 private String username;
\r
44 private String password;
\r
45 ProgressDialog dialog;
\r
47 //use this to see the output of the command used
\r
48 private List<String> resultLines = new ArrayList<String>();
\r
50 // host, username, password initialize
\r
52 protected void onPreExecute() {
\r
53 super.onPreExecute();
\r
54 host = ConfigActivity.MYSQLHOSTIP;
\r
55 username = ConfigActivity.MYSQLHOSTUSER;
\r
56 password = ConfigActivity.MYSQLHOSTPASSWORD;
\r
61 The functions below are mainly from :
\r
62 https://stackoverflow.com/questions/25789245/how-to-get-jsch-shell-command-output-in-string
\r
65 // open the connection using username, password, and hostname
\r
66 public boolean open() throws JSchException {
\r
68 JSch jSch = new JSch();
\r
70 session = jSch.getSession(username, host, 22);
\r
71 java.util.Properties config = new java.util.Properties();
\r
72 config.put("StrictHostKeyChecking", "no"); // not recommended
\r
73 session.setPassword(password);
\r
74 session.setConfig(config);
\r
77 Log.d("SSH CONNECT OPEN", "Connecting SSH to " + host + " - Please wait for few seconds... ");
\r
79 if (session.isConnected()) {
\r
80 Log.d("SSH CONNECT", "router connected!");
\r
83 Log.d("SSH NOT CONNECT", "router NOT connected!");
\r
89 public void runCommand(String command) throws JSchException, IOException {
\r
91 if (!session.isConnected())
\r
92 throw new RuntimeException("Not connected to an open session. Call open() first!");
\r
94 System.out.println("command: " + command);
\r
95 channel = session.openChannel("exec");
\r
96 ce = (ChannelExec) channel;
\r
97 ce.setCommand(command);
\r
99 Log.d("SSH RUN COMMAND", command);
\r
102 // get output from a command
\r
103 private List<String> getChannelOutput(Channel channel) throws IOException {
\r
105 byte[] buffer = new byte[8192];
\r
106 List<String> output_lines = new ArrayList<String>();
\r
108 InputStream in = channel.getInputStream();
\r
109 String line = new String();
\r
111 while (in.available() > 0) {
\r
112 int i = in.read(buffer, 0, 8192);
\r
116 line = new String(buffer, 0, i);
\r
117 // add the read line to the return value list.
\r
118 output_lines = new ArrayList(Arrays.asList(line.split("\\n")));
\r
121 if(line.contains("logout")) {
\r
124 if (channel.isClosed()) {
\r
128 Thread.sleep(1000);
\r
129 } catch (Exception e){}
\r
131 } catch(Exception e) {
\r
132 Log.d("SSH READOUTPUT ERROR", "Error while reading channel output: "+ e);
\r
135 return output_lines;
\r
140 usage : execute commands through SSH for database MySQL
\r
143 protected List<String> doInBackground(String... params) {
\r
146 // Get into the path and create the config file
\r
147 //starts at iot2/bin/iotinstaller
\r
148 cmd = "cd " + MainActivity.DEF_ADD_DEVICE_TO_MYSQL + ";";
\r
149 cmd = cmd + params[0];
\r
150 //Log.d("yoyo", cmd);
\r
152 // now the command is set, so send it.
\r
154 // try open the connection
\r
156 Log.d("SSH CONNECTION CLOSE", "open failed.");
\r
160 ce.setCommand(cmd);
\r
162 resultLines = getChannelOutput(ce);
\r
163 } catch (Exception e) {
\r
166 channel.disconnect();
\r
170 public List<String> getResultLines() {
\r
171 return resultLines;
\r
175 protected onPostExecute(Void param) {
\r
176 Log.d("POST", "in post execute");
\r