Adding database information insertion features in the phone app
[iot2.git] / others / lede-gui / src / main / java / com / example / lede2 / SSH.java
1 /**\r
2  * Created by Tak and Bowon on 17. 7. 21.\r
3  * SSH class can be used to make ssh connections and send command lines\r
4  */\r
5 \r
6 package com.example.lede2;\r
7 \r
8 import android.content.Context;\r
9 import android.util.Log;\r
10 import android.os.AsyncTask;\r
11 \r
12 import com.jcraft.jsch.JSch;\r
13 import com.jcraft.jsch.JSchException;\r
14 import com.jcraft.jsch.Session;\r
15 import com.jcraft.jsch.ChannelExec;\r
16 import com.jcraft.jsch.Channel;\r
17 \r
18 import java.io.IOException;\r
19 import java.io.InputStream;\r
20 import java.lang.String;\r
21 import java.util.ArrayList;\r
22 import java.util.Arrays;\r
23 import java.util.List;\r
24 \r
25 //import android.provider.Settings;\r
26 //import android.support.v7.app.AppCompatActivity;\r
27 //import android.os.Bundle;\r
28 //import java.io.ByteArrayOutputStream;\r
29 //import java.util.Properties;\r
30 //import java.io.PrintStream;\r
31 \r
32 // AsyncTask input : command line\r
33 // AysncTask output : output from a command\r
34 public class SSH extends AsyncTask<String, Void, List<String>> {\r
35 \r
36         // variables used for connection\r
37         private Session session;\r
38         private Channel channel;\r
39         private ChannelExec ce;\r
40         // in this project, we supposed we use fixed host, username, password\r
41         private String host;\r
42         private String username;\r
43         private String password;\r
44 \r
45         // host, username, password initialize\r
46         @Override\r
47         protected void onPreExecute() {\r
48                 super.onPreExecute();\r
49 \r
50                 host = ConfigActivity.ROUTERIP;\r
51                 username = ConfigActivity.ROUTERUSER;\r
52                 password = ConfigActivity.RPWD;\r
53         }\r
54 \r
55         /* \r
56         The functions below are mainly from :\r
57         https://stackoverflow.com/questions/25789245/how-to-get-jsch-shell-command-output-in-string\r
58         */\r
59 \r
60         // open the connection using username, password, and hostname\r
61         public boolean open() throws JSchException {\r
62 \r
63                 JSch jSch = new JSch();\r
64 \r
65                 session = jSch.getSession(username, host, 22);\r
66                 java.util.Properties config = new java.util.Properties();\r
67                 config.put("StrictHostKeyChecking", "no");  // not recommended\r
68                 session.setPassword(password);\r
69                 session.setConfig(config);\r
70 \r
71 \r
72                 Log.d("SSH CONNECT OPEN", "Connecting SSH to " + host + " - Please wait for few seconds... ");\r
73                 session.connect();\r
74                 if (session.isConnected()) {\r
75                         Log.d("SSH CONNECT", "router connected!");\r
76                         return true;\r
77                 } else {\r
78                         Log.d("SSH NOT CONNECT", "router NOT connected!");\r
79                         return false;\r
80                 }\r
81         }\r
82 \r
83         // send a command\r
84         public void runCommand(String command) throws JSchException, IOException {\r
85 \r
86                 if (!session.isConnected())\r
87                         throw new RuntimeException("Not connected to an open session.  Call open() first!");\r
88 \r
89                 channel = session.openChannel("exec");\r
90                 ce = (ChannelExec) channel;\r
91                 ce.setCommand(command);\r
92                 ce.connect();\r
93                 Log.d("SSH RUN COMMAND", command);\r
94         }\r
95 \r
96         // get output from a command\r
97         private List<String> getChannelOutput(Channel channel) throws IOException {\r
98 \r
99                 byte[] buffer = new byte[1024];\r
100                 List<String> output_lines = new ArrayList<String>();\r
101                 try {\r
102                         InputStream in = channel.getInputStream();\r
103                         String line = new String();\r
104                         while (true) {\r
105                                 while (in.available() > 0) {\r
106                                         int i = in.read(buffer, 0, 1024);\r
107                                         if (i < 0) {\r
108                                                 break;\r
109                                         }\r
110                                         line = new String(buffer, 0, i);\r
111                                         // add the read line to the return value list.\r
112                                         output_lines = new ArrayList(Arrays.asList(line.split("\\n")));\r
113                                 }\r
114 \r
115                                 if(line.contains("logout")) {\r
116                                         break;\r
117                                 }\r
118                                 if (channel.isClosed()) {\r
119                                         break;\r
120                                 }\r
121                                 try {\r
122                                         Thread.sleep(1000);\r
123                                 } catch (Exception ee){}\r
124                         }\r
125                 } catch(Exception e) {\r
126                         Log.d("SSH READOUTPUT ERROR", "Error while reading channel output: "+ e);\r
127                 }\r
128 \r
129                 return output_lines;\r
130         }\r
131 \r
132 \r
133         /*\r
134         usage :\r
135         0. params == "-ch <password>" : change default password into <password>\r
136         1. params == "-co <password>" : add device to the database and hostapd file\r
137         2. params == "-dn <password>" : delete devices by their names\r
138         3. params == "-ln <password>" : list devices' names\r
139         */\r
140         @Override\r
141         protected List<String> doInBackground(String... params) {\r
142 \r
143                 List<String> result_lines = new ArrayList<String>();\r
144                 String cmd;\r
145 \r
146                 if(params[0].substring(0,3).equals("-ch")) { // ./change_default_pw.sh -ch <password>\r
147                         cmd = MainActivity.DEF_CHANGE_DEFAULT_SCRIPT + " " + params[0];\r
148                 } else if(params[0].substring(0,3).equals("-co")) { // ./connect_device.sh -co <password> <device-name>\r
149                         cmd = MainActivity.DEF_CONNECT_DEVICE_SCRIPT + " " + params[0];\r
150                 } else if(params[0].substring(0,3).equals("-dn")) { // ./register_device.sh -dn <device-name>\r
151                         cmd = MainActivity.DEF_REGISTER_DEVICE_SCRIPT + " " + params[0];\r
152                 } else if(params[0].substring(0,3).equals("-ln")) { // ./register_device.sh -ln <device-name>\r
153                         // below block is a little different from others cause it needs to get output from the router\r
154                         try {\r
155                                 // try open the connection\r
156                                 if (!open()) {\r
157                                         Log.d("SSH CONNECTION CLOSE", "open failed.");\r
158                                         return null;\r
159                                 }\r
160                                 cmd = MainActivity.DEF_REGISTER_DEVICE_SCRIPT + " " + params[0];\r
161                                 runCommand(cmd);\r
162                                 ce.setCommand(cmd);\r
163                                 ce.connect();\r
164                                 result_lines = getChannelOutput(ce);\r
165                         } catch (Exception e) {\r
166                         }\r
167                         channel.disconnect();\r
168 \r
169                         // only this block return meaningful value, which should be the names of devices.\r
170                         return result_lines;\r
171                 } else {\r
172                         Log.d("SSH PARAM ERROR", "Wrong parameter used.");\r
173                         return null;\r
174                 }\r
175 \r
176                 // now the command is set, so send it.\r
177                 try {\r
178                         // try open the connection\r
179                         if (!open()) {\r
180                                 Log.d("SSH CONNECTION CLOSE", "open failed.");\r
181                                 return null;\r
182                         }\r
183                         runCommand(cmd);\r
184                         ce.setCommand(cmd);\r
185                         ce.connect();\r
186                 } catch (Exception e) {\r
187                 } // done\r
188 \r
189                 channel.disconnect();\r
190                 return null;\r
191         }\r
192 \r
193         /*\r
194         @Override\r
195         protected  onPostExecute(Void param) {\r
196                 Log.d("POST", "in post execute");\r
197         }\r
198         */\r
199 }\r