The final version for lede-gui (the phone app for device registration)
[iot2.git] / others / lede-gui / src / main / java / com / example / lede2 / SSH_MySQL.java
1 package com.example.lede2;\r
2 \r
3 /**\r
4  * Created by rtrimana on 9/25/17.\r
5  */\r
6 \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
11 \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
17 \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
23 \r
24 \r
25 \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
32 \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
36 \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
46 \r
47     //use this to see the output of the command used\r
48     private List<String> resultLines = new ArrayList<String>();\r
49 \r
50     // host, username, password initialize\r
51     @Override\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
57 \r
58     }\r
59 \r
60         /*\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
63         */\r
64 \r
65     // open the connection using username, password, and hostname\r
66     public boolean open() throws JSchException {\r
67 \r
68         JSch jSch = new JSch();\r
69 \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
75 \r
76 \r
77         Log.d("SSH CONNECT OPEN", "Connecting SSH to " + host + " - Please wait for few seconds... ");\r
78         session.connect();\r
79         if (session.isConnected()) {\r
80             Log.d("SSH CONNECT", "router connected!");\r
81             return true;\r
82         } else {\r
83             Log.d("SSH NOT CONNECT", "router NOT connected!");\r
84             return false;\r
85         }\r
86     }\r
87 \r
88     // send a command\r
89     public void runCommand(String command) throws JSchException, IOException {\r
90 \r
91         if (!session.isConnected())\r
92             throw new RuntimeException("Not connected to an open session.  Call open() first!");\r
93 \r
94         System.out.println("command: " + command);\r
95         channel = session.openChannel("exec");\r
96         ce = (ChannelExec) channel;\r
97         ce.setCommand(command);\r
98         ce.connect();\r
99         Log.d("SSH RUN COMMAND", command);\r
100     }\r
101 \r
102     // get output from a command\r
103     private List<String> getChannelOutput(Channel channel) throws IOException {\r
104 \r
105         byte[] buffer = new byte[8192];\r
106         List<String> output_lines = new ArrayList<String>();\r
107         try {\r
108             InputStream in = channel.getInputStream();\r
109             String line = new String();\r
110             while (true) {\r
111                 while (in.available() > 0) {\r
112                     int i = in.read(buffer, 0, 8192);\r
113                     if (i < 0) {\r
114                         break;\r
115                     }\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
119                 }\r
120 \r
121                 if(line.contains("logout")) {\r
122                     break;\r
123                 }\r
124                 if (channel.isClosed()) {\r
125                     break;\r
126                 }\r
127                 try {\r
128                     Thread.sleep(1000);\r
129                 } catch (Exception e){}\r
130             }\r
131         } catch(Exception e) {\r
132             Log.d("SSH READOUTPUT ERROR", "Error while reading channel output: "+ e);\r
133         }\r
134 \r
135         return output_lines;\r
136     }\r
137 \r
138 \r
139     /*\r
140     usage : execute commands through SSH for database MySQL\r
141     */\r
142     @Override\r
143     protected List<String> doInBackground(String... params) {\r
144 \r
145         String cmd;\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
151 \r
152         // now the command is set, so send it.\r
153         try {\r
154             // try open the connection\r
155             if (!open()) {\r
156                 Log.d("SSH CONNECTION CLOSE", "open failed.");\r
157                 return null;\r
158             }\r
159             runCommand(cmd);\r
160             ce.setCommand(cmd);\r
161             ce.connect();\r
162             resultLines = getChannelOutput(ce);\r
163         } catch (Exception e) {\r
164         } // done\r
165 \r
166         channel.disconnect();\r
167         return null;\r
168     }\r
169 \r
170     public List<String> getResultLines() {\r
171         return resultLines;\r
172     }\r
173         /*\r
174         @Override\r
175         protected  onPostExecute(Void param) {\r
176                 Log.d("POST", "in post execute");\r
177         }\r
178         */\r
179 }\r