Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1.  
  2.  
  3. ----------MainActivity.java-----------
  4.  
  5. package com.example.demyon.opengatefinal;
  6.  
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13.  
  14. import static junit.framework.Assert.assertEquals;
  15. import static junit.framework.Assert.fail;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.  
  19. static final String command ="./gate.sh";
  20. static final String userName= "pi";
  21. static final String password= "1234";
  22. static final String connectionIP= "192.168.1.4";
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27.  
  28.  
  29.  
  30. Button openBtn = (Button) findViewById(R.id.openBtn);
  31.  
  32.  
  33.  
  34. openBtn.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. SshConn s = new SshConn();
  38. s.execute("");
  39.  
  40. }
  41. });
  42.  
  43. }
  44.  
  45.  
  46. private class SshConn extends AsyncTask<String, String, String> {
  47.  
  48.  
  49. @Override
  50. protected void onPreExecute(){
  51.  
  52. }
  53.  
  54. @Override
  55. protected String doInBackground(String... params) {
  56. Log.v("Tag","Erro numero 1");
  57. Log.v("Tag",userName+" "+password+" "+connectionIP);
  58. SSHManager instance = new SSHManager(userName, password, connectionIP, "");
  59. Log.v("Tag","Erro numero 2");
  60. String errorMessage = instance.connect();
  61. Log.v("Tag","Erro numero 3");
  62. if(errorMessage != null)
  63. {
  64. Log.v("Tag","Erro dentro do IF");
  65. System.out.println(errorMessage);
  66. Log.v("Tag","Erro dentro do IF 2");
  67. fail();
  68. Log.v("Tag","Erro dentro do IF3");
  69. }
  70. Log.v("Tag","Erro numero 4");
  71. String expResult = "";
  72. // call sendCommand for each command and the output
  73. //(without prompts) is returned
  74. String result = instance.sendCommand(command);
  75. // close only after all commands are sent
  76. instance.close();
  77. assertEquals(expResult, result);
  78. Log.v("Tag","Erro Final");
  79.  
  80. return null;
  81. }
  82.  
  83. @Override
  84. protected void onPostExecute(String msg){
  85.  
  86. }
  87. }
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. -------SSHManager.java----------
  98.  
  99. package com.example.demyon.opengatefinal;
  100.  
  101. import com.jcraft.jsch.Channel;
  102. import com.jcraft.jsch.ChannelExec;
  103. import com.jcraft.jsch.JSch;
  104. import com.jcraft.jsch.JSchException;
  105. import com.jcraft.jsch.Session;
  106.  
  107. import java.io.IOException;
  108. import java.io.InputStream;
  109. import java.util.logging.Level;
  110. import java.util.logging.Logger;
  111.  
  112. /**
  113. * Created by Demyon on 10/15/2017.
  114. */
  115.  
  116. public class SSHManager {
  117. private static final Logger LOGGER =
  118. Logger.getLogger(SSHManager.class.getName());
  119. private JSch jschSSHChannel;
  120. private String strUserName;
  121. private String strConnectionIP;
  122. private int intConnectionPort;
  123. private String strPassword;
  124. private Session sesConnection;;
  125. private int intTimeOut;
  126.  
  127. private void doCommonConstructorActions(String userName,
  128. String password, String connectionIP, String knownHostsFileName)
  129. {
  130. jschSSHChannel = new JSch();
  131.  
  132. try
  133. {
  134. jschSSHChannel.setKnownHosts(knownHostsFileName);
  135. }
  136. catch(JSchException jschX)
  137. {
  138. logError(jschX.getMessage());
  139. }
  140.  
  141. strUserName = userName;
  142. strPassword = password;
  143. strConnectionIP = connectionIP;
  144. }
  145.  
  146. public SSHManager(String userName, String password,
  147. String connectionIP, String knownHostsFileName)
  148. {
  149. doCommonConstructorActions(userName, password,
  150. connectionIP, knownHostsFileName);
  151. intConnectionPort = 22;
  152. intTimeOut = 60000;
  153. }
  154.  
  155. public SSHManager(String userName, String password, String connectionIP,
  156. String knownHostsFileName, int connectionPort)
  157. {
  158. doCommonConstructorActions(userName, password, connectionIP,
  159. knownHostsFileName);
  160. intConnectionPort = connectionPort;
  161. intTimeOut = 60000;
  162. }
  163.  
  164. public SSHManager(String userName, String password, String connectionIP,
  165. String knownHostsFileName, int connectionPort, int timeOutMilliseconds)
  166. {
  167. doCommonConstructorActions(userName, password, connectionIP,
  168. knownHostsFileName);
  169. intConnectionPort = connectionPort;
  170. intTimeOut = timeOutMilliseconds;
  171. }
  172.  
  173. public String connect()
  174. {
  175. String errorMessage = null;
  176.  
  177. try
  178. {
  179. sesConnection = jschSSHChannel.getSession(strUserName,
  180. strConnectionIP, intConnectionPort);
  181. sesConnection.setPassword(strPassword);
  182. // UNCOMMENT THIS FOR TESTING PURPOSES, BUT DO NOT USE IN PRODUCTION
  183. // sesConnection.setConfig("StrictHostKeyChecking", "no");
  184. sesConnection.connect(intTimeOut);
  185. }
  186. catch(JSchException jschX)
  187. {
  188. errorMessage = jschX.getMessage();
  189. }
  190.  
  191. return errorMessage;
  192. }
  193.  
  194. private String logError(String errorMessage)
  195. {
  196. if(errorMessage != null)
  197. {
  198. LOGGER.log(Level.SEVERE, "{0}:{1} - {2}",
  199. new Object[]{strConnectionIP, intConnectionPort, errorMessage});
  200. }
  201.  
  202. return errorMessage;
  203. }
  204.  
  205. private String logWarning(String warnMessage)
  206. {
  207. if(warnMessage != null)
  208. {
  209. LOGGER.log(Level.WARNING, "{0}:{1} - {2}",
  210. new Object[]{strConnectionIP, intConnectionPort, warnMessage});
  211. }
  212.  
  213. return warnMessage;
  214. }
  215.  
  216. public String sendCommand(String command)
  217. {
  218. StringBuilder outputBuffer = new StringBuilder();
  219.  
  220. try
  221. {
  222. Channel channel = sesConnection.openChannel("exec");
  223. ((ChannelExec)channel).setCommand(command);
  224. InputStream commandOutput = channel.getInputStream();
  225. channel.connect();
  226. int readByte = commandOutput.read();
  227.  
  228. while(readByte != 0xffffffff)
  229. {
  230. outputBuffer.append((char)readByte);
  231. readByte = commandOutput.read();
  232. }
  233.  
  234. channel.disconnect();
  235. }
  236. catch(IOException ioX)
  237. {
  238. logWarning(ioX.getMessage());
  239. return null;
  240. }
  241. catch(JSchException jschX)
  242. {
  243. logWarning(jschX.getMessage());
  244. return null;
  245. }
  246.  
  247. return outputBuffer.toString();
  248. }
  249.  
  250. public void close()
  251. {
  252. sesConnection.disconnect();
  253. }
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement