Skip to content

Instantly share code, notes, and snippets.

@tseglevskiy
Created October 29, 2016 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tseglevskiy/877d8b959dd05b860b120a18ddf806c2 to your computer and use it in GitHub Desktop.
Save tseglevskiy/877d8b959dd05b860b120a18ddf806c2 to your computer and use it in GitHub Desktop.
How to use 'su' command in Android
/*******************************************************************************
* Origin: https://github.com/alt236/Wifi-Key-Recovery---Android/blob/master/src/aws/apps/wifiKeyRecovery/util/ExecTerminal.java
*
* Copyright 2011 Alexandros Schillings
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
public class ExecTerminal {
final String TAG = this.getClass().getName();
public class ExecResult{
final String TAG = this.getClass().getName();
String stdOut = "";
String stdErr = "";
String stdIn = "";
public ExecResult(String stdIn, String stdOut, String stdErr){
this.stdOut = stdOut;
this.stdErr = stdErr;
this.stdIn = stdIn;
}
public ExecResult(){
clear();
}
public void clear(){
stdOut = "";
stdErr = "";
stdIn = "";
}
public String getStdOut() {
return stdOut;
}
public String getStdErr() {
return stdErr;
}
public String getStdIn() {
return stdIn;
}
};
public boolean checkSu(){
final ExecTerminal et = new ExecTerminal();
ExecResult res = et.execSu("su && echo 1");
if (res.getStdOut().trim().equals("1")){
Log.i(TAG, "^ got root!");
return true;
}
Log.w(TAG, "^ could not get root.");
return false;
}
public ExecResult execSu(String cmd) {
Log.d(TAG, "^ execSu(): '" + cmd + "'");
String stdOut = "";
String stdErr = "";
try {
final Process process = Runtime.getRuntime().exec("su");
final DataInputStream is = new DataInputStream(process.getInputStream());
final DataInputStream eis = new DataInputStream(process.getErrorStream());
final DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
os.close();
final BufferedReader readerOut = new BufferedReader(new InputStreamReader(is));
final BufferedReader readerErr = new BufferedReader(new InputStreamReader(eis));
process.waitFor();
try {
String line = "";
while ((line = readerOut.readLine()) != null) {
stdOut = stdOut + line + "\n";
}
readerOut.close();
} catch (IOException e) {
Log.e(TAG, "^ execSu() - IOException in sdtdOut Loop: " + e.getMessage());
}
try {
String line = "";
while ((line = readerErr.readLine()) != null) {
stdErr = stdErr + line + "\n";
}
readerErr.close();
} catch (IOException e) {
Log.e(TAG, "^ execSu() - IOException in sdtdOut Loop: " + e.getMessage());
}
} catch (IOException e) {
Log.e(TAG, "^ execSu() - IOException: " + e.getMessage());
} catch (InterruptedException e) {
Log.e(TAG, "^ execSu() - InterruptedException: " + e.getMessage());
}
ExecResult res = new ExecResult(cmd, stdOut, stdErr);
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment