Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void run (String command) {
- String hostname = "130.237.177.213";
- String username = "pi";
- String password = "raspberry";
- try
- {
- StrictMode.ThreadPolicy policy = new
- StrictMode.ThreadPolicy.Builder()
- .permitAll().build();
- StrictMode.setThreadPolicy(policy);
- Connection conn = new Connection(hostname); //init connection
- conn.connect(); //start connection to the hostname
- boolean isAuthenticated =
- conn.authenticateWithPassword(username, password);
- if (isAuthenticated == false)
- throw new IOException("Authentication failed.");
- Session sess = conn.openSession();
- sess.execCommand(command);
- InputStream stdout = new StreamGobbler(sess.getStdout());
- BufferedReader br = new BufferedReader(new
- InputStreamReader(stdout)); //reads text
- while (true)
- { String line = br.readLine(); // read line
- if (line == null)
- break;
- System.out.println(line); }
- /* Show exit status, if available (otherwise "null") */
- System.out.println("ExitCode: " + sess.getExitStatus());
- sess.close(); // Close this session
- conn.close();
- }
- catch (IOException e)
- { e.printStackTrace(System.err);
- System.exit(2); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement