-package com.example.lede2;
-
-/**
- * Created by rtrimana on 9/25/17.
- */
-
-import android.os.AsyncTask;
-import android.util.Log;
-
-import com.jcraft.jsch.Channel;
-import com.jcraft.jsch.ChannelExec;
-import com.jcraft.jsch.JSch;
-import com.jcraft.jsch.JSchException;
-import com.jcraft.jsch.Session;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import android.content.Context;
-import android.util.Log;
-import android.os.AsyncTask;
-
-import com.jcraft.jsch.JSch;
-import com.jcraft.jsch.JSchException;
-import com.jcraft.jsch.Session;
-import com.jcraft.jsch.ChannelExec;
-import com.jcraft.jsch.Channel;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.String;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-// AsyncTask input : command line
-// AysncTask output : output from a command
-public class SSH_MySQL extends AsyncTask<String, Void, List<String>> {
-
- // variables used for connection
- private Session session;
- private Channel channel;
- private ChannelExec ce;
- // in this project, we supposed we use fixed host, username, password
- private String host;
- private String username;
- private String password;
-
- // host, username, password initialize
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
-
- host = ConfigActivity.MYSQLHOSTIP;
- username = ConfigActivity.MYSQLHOSTUSER;
- password = ConfigActivity.MYSQLHOSTPASSWORD;
- }
-
- /*
- The functions below are mainly from :
- https://stackoverflow.com/questions/25789245/how-to-get-jsch-shell-command-output-in-string
- */
-
- // open the connection using username, password, and hostname
- public boolean open() throws JSchException {
-
- JSch jSch = new JSch();
-
- session = jSch.getSession(username, host, 22);
- java.util.Properties config = new java.util.Properties();
- config.put("StrictHostKeyChecking", "no"); // not recommended
- session.setPassword(password);
- session.setConfig(config);
-
-
- Log.d("SSH CONNECT OPEN", "Connecting SSH to " + host + " - Please wait for few seconds... ");
- session.connect();
- if (session.isConnected()) {
- Log.d("SSH CONNECT", "router connected!");
- return true;
- } else {
- Log.d("SSH NOT CONNECT", "router NOT connected!");
- return false;
- }
- }
-
- // send a command
- public void runCommand(String command) throws JSchException, IOException {
-
- if (!session.isConnected())
- throw new RuntimeException("Not connected to an open session. Call open() first!");
-
- channel = session.openChannel("exec");
- ce = (ChannelExec) channel;
- ce.setCommand(command);
- ce.connect();
- Log.d("SSH RUN COMMAND", command);
- }
-
- // get output from a command
- private List<String> getChannelOutput(Channel channel) throws IOException {
-
- byte[] buffer = new byte[1024];
- List<String> output_lines = new ArrayList<String>();
- try {
- InputStream in = channel.getInputStream();
- String line = new String();
- while (true) {
- while (in.available() > 0) {
- int i = in.read(buffer, 0, 1024);
- if (i < 0) {
- break;
- }
- line = new String(buffer, 0, i);
- // add the read line to the return value list.
- output_lines = new ArrayList(Arrays.asList(line.split("\\n")));
- }
-
- if(line.contains("logout")) {
- break;
- }
- if (channel.isClosed()) {
- break;
- }
- try {
- Thread.sleep(1000);
- } catch (Exception ee){}
- }
- } catch(Exception e) {
- Log.d("SSH READOUTPUT ERROR", "Error while reading channel output: "+ e);
- }
-
- return output_lines;
- }
-
-
- /*
- usage : execute commands through SSH for database MySQL
- */
- @Override
- protected List<String> doInBackground(String... params) {
-
- String cmd;
- // Get into the path and create the config file
- cmd = "cd " + MainActivity.DEF_ADD_DEVICE_TO_MYSQL + ";";
- cmd = cmd + params[0];
-
- // now the command is set, so send it.
- try {
- // try open the connection
- if (!open()) {
- Log.d("SSH CONNECTION CLOSE", "open failed.");
- return null;
- }
- runCommand(cmd);
- ce.setCommand(cmd);
- ce.connect();
- } catch (Exception e) {
- } // done
-
- channel.disconnect();
- return null;
- }
-
- /*
- @Override
- protected onPostExecute(Void param) {
- Log.d("POST", "in post execute");
- }
- */
-}
+package com.example.lede2;\r
+\r
+/**\r
+ * Created by rtrimana on 9/25/17.\r
+ */\r
+\r
+import android.app.ProgressDialog;\r
+import android.os.AsyncTask;\r
+import android.os.SystemClock;\r
+import android.util.Log;\r
+\r
+import com.jcraft.jsch.Channel;\r
+import com.jcraft.jsch.ChannelExec;\r
+import com.jcraft.jsch.JSch;\r
+import com.jcraft.jsch.JSchException;\r
+import com.jcraft.jsch.Session;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.lang.String;\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+// AsyncTask input : command line\r
+// AysncTask output : output from a command\r
+public class SSH_MySQL extends AsyncTask<String, Void, List<String>> {\r
+\r
+ // variables used for connection\r
+ private Session session;\r
+ private Channel channel;\r
+ private ChannelExec ce;\r
+ // in this project, we supposed we use fixed host, username, password\r
+ private String host;\r
+ private String username;\r
+ private String password;\r
+ ProgressDialog dialog;\r
+\r
+ //use this to see the output of the command used\r
+ private List<String> resultLines = new ArrayList<String>();\r
+\r
+ // host, username, password initialize\r
+ @Override\r
+ protected void onPreExecute() {\r
+ super.onPreExecute();\r
+ host = ConfigActivity.MYSQLHOSTIP;\r
+ username = ConfigActivity.MYSQLHOSTUSER;\r
+ password = ConfigActivity.MYSQLHOSTPASSWORD;\r
+\r
+ }\r
+\r
+ /*\r
+ The functions below are mainly from :\r
+ https://stackoverflow.com/questions/25789245/how-to-get-jsch-shell-command-output-in-string\r
+ */\r
+\r
+ // open the connection using username, password, and hostname\r
+ public boolean open() throws JSchException {\r
+\r
+ JSch jSch = new JSch();\r
+\r
+ session = jSch.getSession(username, host, 22);\r
+ java.util.Properties config = new java.util.Properties();\r
+ config.put("StrictHostKeyChecking", "no"); // not recommended\r
+ session.setPassword(password);\r
+ session.setConfig(config);\r
+\r
+\r
+ Log.d("SSH CONNECT OPEN", "Connecting SSH to " + host + " - Please wait for few seconds... ");\r
+ session.connect();\r
+ if (session.isConnected()) {\r
+ Log.d("SSH CONNECT", "router connected!");\r
+ return true;\r
+ } else {\r
+ Log.d("SSH NOT CONNECT", "router NOT connected!");\r
+ return false;\r
+ }\r
+ }\r
+\r
+ // send a command\r
+ public void runCommand(String command) throws JSchException, IOException {\r
+\r
+ if (!session.isConnected())\r
+ throw new RuntimeException("Not connected to an open session. Call open() first!");\r
+\r
+ System.out.println("command: " + command);\r
+ channel = session.openChannel("exec");\r
+ ce = (ChannelExec) channel;\r
+ ce.setCommand(command);\r
+ ce.connect();\r
+ Log.d("SSH RUN COMMAND", command);\r
+ }\r
+\r
+ // get output from a command\r
+ private List<String> getChannelOutput(Channel channel) throws IOException {\r
+\r
+ byte[] buffer = new byte[8192];\r
+ List<String> output_lines = new ArrayList<String>();\r
+ try {\r
+ InputStream in = channel.getInputStream();\r
+ String line = new String();\r
+ while (true) {\r
+ while (in.available() > 0) {\r
+ int i = in.read(buffer, 0, 8192);\r
+ if (i < 0) {\r
+ break;\r
+ }\r
+ line = new String(buffer, 0, i);\r
+ // add the read line to the return value list.\r
+ output_lines = new ArrayList(Arrays.asList(line.split("\\n")));\r
+ }\r
+\r
+ if(line.contains("logout")) {\r
+ break;\r
+ }\r
+ if (channel.isClosed()) {\r
+ break;\r
+ }\r
+ try {\r
+ Thread.sleep(1000);\r
+ } catch (Exception e){}\r
+ }\r
+ } catch(Exception e) {\r
+ Log.d("SSH READOUTPUT ERROR", "Error while reading channel output: "+ e);\r
+ }\r
+\r
+ return output_lines;\r
+ }\r
+\r
+\r
+ /*\r
+ usage : execute commands through SSH for database MySQL\r
+ */\r
+ @Override\r
+ protected List<String> doInBackground(String... params) {\r
+\r
+ String cmd;\r
+ // Get into the path and create the config file\r
+ //starts at iot2/bin/iotinstaller\r
+ cmd = "cd " + MainActivity.DEF_ADD_DEVICE_TO_MYSQL + ";";\r
+ cmd = cmd + params[0];\r
+ //Log.d("yoyo", cmd);\r
+\r
+ // now the command is set, so send it.\r
+ try {\r
+ // try open the connection\r
+ if (!open()) {\r
+ Log.d("SSH CONNECTION CLOSE", "open failed.");\r
+ return null;\r
+ }\r
+ runCommand(cmd);\r
+ ce.setCommand(cmd);\r
+ ce.connect();\r
+ resultLines = getChannelOutput(ce);\r
+ } catch (Exception e) {\r
+ } // done\r
+\r
+ channel.disconnect();\r
+ return null;\r
+ }\r
+\r
+ public List<String> getResultLines() {\r
+ return resultLines;\r
+ }\r
+ /*\r
+ @Override\r
+ protected onPostExecute(Void param) {\r
+ Log.d("POST", "in post execute");\r
+ }\r
+ */\r
+}\r