I'm trying to ping a site to detect wether I have internet or not in my android app. On many devices this does work well, but today, with a new devices, it allways returns that I have no internet.
This is the code:
static public boolean isInternetAvailable(String url){
Runtime runtime = Runtime.getRuntime();
try {
String command = "/system/bin/ping -c 1 " + url;
Process ipProcess = runtime.exec(command);
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
return false;
}
Even though the server is online, and I can ping it from various other devices, on the new device it allways returns false. Could it be possible that the ping command is blocked for some reason?
The new device is a Samsung GT-19305 with Android version 4.3.
You could try this:
Process process = new ProcessBuilder()
.command("/system/bin/ping", "android.com")
.redirectErrorStream(true)
.start();
try {
InputStream in = process.getInputStream();
OutputStream out = process.getOutputStream();
readStream(in);
finally {
process.destroy();
}
}
Related
I have a custom device android 4.3. Problem occurs with some commands, one of an example:
su -c 'pm enable com.android.systemui'
When I run this command over adb it works. However when I run the code programatically using this library it just does not work, no error is shown as well.
Interesting observations:
Shell.SU.available() : false
Shell.SU.isSELinuxEnforcing() : false
Ok so device is rooted. Any reason why you are trying to do that command using that library?
What I am trying to say is why can't you just run the shell command yourself?
runRootCommand method:
static boolean runRootCommand(String command) {
boolean status = true;
DataOutputStream os = null;
try {
Process process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (IOException | InterruptedException e) {
Log.e(TAG, e.toString());
status = false;
} finally {
try {
if (os != null)
os.close();
} catch (IOException e) {
Log.e(TAG, e.toString());
status = false;
}
}
return status;
}
And then call that method like this:
boolean success = runRootCommand("pm enable com.android.systemui");
if(success) {
// command was successful
} else {
// command was NOT successful
}
This will run the command as "su" (superuser).
Hope this helps.
I have an Activity which every second write a counter to the logcat:
Runnable rLog = new Runnable() {
#Override
public void run() {
i++;
Log.d("bbb", "i= " + i);
handler.postDelayed(this, 1000);
}
};
In addition - I have a service which read from "logcat -s bbb" and log it:
Runnable rGetLog = new Runnable() {
#Override
public void run() {
Runtime rt = Runtime.getRuntime();
Process process = null;
try {
process = rt.exec("logcat -s bbb");
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
try {
while ((line = bufferedReader.readLine()) != null)
{
Log.d("aaa", "get line = " + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
This code works well. The problem starts when I change the tag "bbb" to a real tag such as "AndroidRuntime" or another tag... I got an empty response from logcat
(if I run at the same time "logcat -s AndroidRuntime" from adb I got lots of lines...)
Who knows what the problem is? what can be different?
Thanks!
From Android Jelly Bean, applications cannot read log entries from other applications, unless your device is rooted and you read the logs as superuser.
try using sudo to get permissions:
process = rt.exec("su && logcat -s YOUR_TAG");
On a rooted android device, I tried to run a cat command that read kernel log, as follow:
Process p = Runtime.getRuntime().exec("su");
p = Runtime.getRuntime().exec("/system/bin/cat /proc/kmsg");
The su command was successfully executed but not the cat.
I tried to read the output of the command using getInputStream() but nothing was there, as follow:
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((read = err.read(buffer)) > 0)
{ //read error to buffer
catOutput.append(buffer, 0, read);
}
in.close();
I used the same code with ls command instead of displaying the kernel log, it worked just fine and show me the result.
I wonder if what error I am getting and wantted to see the error message on the shell when executing the cat command. Tried the p.getErrorStream() but it doesn't give me any result.
Could any one help me with this ? Thanks.
Here's a comprehensive example on how to do this - note that I got the idea from this answer:
public void catKmsg() {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
StringBuilder sbstdOut = new StringBuilder();
StringBuilder sbstdErr = new StringBuilder();
String command="/system/bin/cat /proc/kmsg";
try { // Run Script
proc = runtime.exec("su");
osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(command);
osw.flush();
osw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
if (proc != null) {
proc.waitFor();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
sbstdOut.append(ReadBufferedReader(new InputStreamReader
(proc.getInputStream())));
sbstdErr.append(ReadBufferedReader(new InputStreamReader
(proc.getErrorStream())));
if (proc.exitValue() != 0) {
}
}
I finally found the solution for the problem by using RootTools library.
Recently released (few months after my question was asked), RootTools provides a easy-to-use tool set that helps running commands that required root privilege. I created a wrapper to check if root access is available before executing shell command:
void testRootToolsCommand(String command){
if (RootTools.isRootAvailable())
toastMessage("Root is available !!!");
else {
toastMessage("NO ROOT !!! ");
return;
}
int timeOut = 1000;
try {
List<String> output = RootTools.sendShell(command,timeOut);
toastMessage("OUTPUT of the command \n" + output.toString());
} catch (RootToolsException re) {
toastMessage("Funny thing happened with RootTools!!! ");
} catch (TimeoutException te)
{
toastMessage("Timeout exception - Increase timeout !!! !!! ");
}
catch (Exception e) {
toastMessage(e.getMessage().toString());
}
}
An example of a function call is:
testRootToolsCommand("cat /proc/kmsg > /sdcard/jun11_4h51.txt");
Note: The Tool also support running multiple commands at once.
For some reason I cannot reboot Android devices using Runtime.getRuntime().exec("/system/bin/reboot");. I have tried the following code on 3 devices now without luck. One was built from rowboat-android source. The other two are the Motorola Droid Pro (Rooted, stock) and the HTC Ledgent (Rooted, Cynogen Mod). All devices are running Android 2.2 Froyo.
Does anyone know why? su works as well as the Super User application is visible. I should note various other shell commands do work, like netcfg (chmod' to 777) and ls.
public static boolean executeCommand(SHELL_CMD iShellCmd){
String cmd = null;
String line = null;
String fullResponse = null;
Process lPs = null;
Log.d(Global.TAG,"--> !!!!! Running shell command");
if (iShellCmd == SHELL_CMD.restart_device){
cmd = "reboot";
}
Log.d(Global.TAG,"--> About to exec: " + cmd);
try {
lPs = Runtime.getRuntime().exec("su");
lPs = Runtime.getRuntime().exec("/system/bin/reboot");
} catch (Exception e) {
e.printStackTrace();
}
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(lPs.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(lPs.getInputStream()));
try {
while ((line = in.readLine()) != null) {
Log.d(Global.TAG,"--> Command result line: " + line);
if (fullResponse == null){
fullResponse = line;
}else{
fullResponse = fullResponse + "\n" + line;
}
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d(Global.TAG,"--> Full response was: " + fullResponse);
return true;
}
Depending on how you've obtained root permission on your device, you can do any of the following:
Runtime.getRuntime().exec(new String[]{"/system/xbin/su","-c","reboot"});
or
Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot"});
or
Runtime.getRuntime().exec(new String[]{"su","-c","reboot"});
Probably better to test all three scenarios in your application.
Finally after weeks of searching:
Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
Try running "su /system/bin/reboot" instead of su and the command on different lines. That might help :)
If you want a button to be pressed try:
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
try {
Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot"});
} catch (IOException e) {
}
}
});
By the way for this code to work the button has to be called button1.
instead of {"/system/bin/su","-c","reboot"} i changed the "/system/bin/su" part to just "su" and then it worked for me.
Like this Runtime.getRuntime().exec(new String[]{"su","-c","reboot"});
I'm trying to create a ServerSocket on a port below 1024.
Obviously, this is restricted to root access only.
But I'm struggling on getting the right permissions for my app.
I'm using this code, for example, to test if I have root access (or trigger the dialog)
But it still doesn't let me ServerSocket work.
AFAIK, the process that is created with the SU command has the root access, not my app. How do I get root access into my own process?
public stat
ic boolean getRoot() {
try {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
return true;
} else {
return false;
}
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
Afaik you cannot open a port below 1024 under *nix systems if you're not root...