I am trying to execute traceroute command following way:
Runtime r = Runtime.getRuntime();
Process p = r.exec("traceroute google.com");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((inputLine = in.readLine()) != null) {
Log.d(tag,inputLine);
}
But it is giving me the following error:
java.io.IOException: Error running exec().
Commands: [traceroute, 202.164.36.66]
Working Directory: null
Environment: null
It works fine for ping or any other command of linux, please help.
There is no Traceroute installed on the device, so this will not work.
Related
I have a rooted Android 7 phone and I would like to dump unix input event files. Using adb I could do it using the following command:
adb shell getevent -t /dev/input/event7 > recorded_touch_events.txt
This will dump the event7 file into recorded_touch_events.txt. But this only works when the phone is connected by usb cable with the PC. Using Android I can dump files with the following code:
th = new Thread(new Runnable(){
private Process exec;
#Override
public void run() {
try {
exec = Runtime.getRuntime().exec(new String[]{"su","-c","getevent -t /dev/input/event7"});
InputStreamReader is = new InputStreamReader(
exec.getInputStream());
String s;
BufferedReader br = new BufferedReader(is);
while(((s = br.readLine()) != null) && run){
// write line to text file
}
is.close();
exec.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
In this way, I could store every read line in a text file.
Are there other approaches (probably faster ones) for directly dumping the event file?
getevent is used to print input events out in human readable form. For example during interactive debug session. You do not need to use getevent for just dumping or any other computer processing task. Just open and read the input file. The event record format is very simple.
Certainly No, you are doing it right.
I am copying a kernel executable through my app in my package folder in /data/data and trying to run it in android app using Instrumentation
I have tried ProcessBuilder and Runtime.getRuntime().exec as well both are showing Exit Code 1 and command does not run.
My command:
String command = "/data/data/<packageName>/e4crypt add_key -S 0x11112222333344445555666677778888 <<< "+key;
Process proc = Runtime.getRuntime().exec(command);
int i = proc.exitValue();
BufferedReader stdInputs = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
StringBuilder output = new StringBuilder();
while ((line = stdInputs.readLine()) != null) {
output.append(line);
}
How to format the long string? I have tried ProcessBuilder and Runtime.getRuntime().exec and UiDevice.getInstance(getInstrumentation()).executeShellCommand()
The first parameter is "add_key" Even when i run the executable with one parameter it shows Exitcode=1 and it does not run. Same command works fine from command window.
I suspect the command formatting is incorrect.
I ve got a problem, executing su with parameters (containing blank space?!). My Command.java looks like this:
public class Command
{
Process process;
public String executeCommand(String command)
{
StringBuffer output = new StringBuffer();
try {
process = Runtime.getRuntime().exec(command);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
}
In my MainActivity it is working for single commands.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView content = (TextView) findViewById(R.id.txt_content);
Command cmd = new Command();
String outp = cmd.executeCommand("su -c 'id'");
Log.d("OOOOOOUT", outp);
content.setText(outp);
}
The result shows, that it is working:
D/OOOOOOUT: uid=0(root) gid=0(root) context=u:r:init:s0
This is also working for the ls command. But when I try to parse an argument with paramters, it will not get executed.
E.g.
String outp = cmd.executeCommand("su -c 'ls /data/data/'");
Log.d("OOOOOOUT", outp);
content.setText(outp);
I also tryed the following:
String outp = cmd.executeCommand("su -c \'ls /data/data/\'");
String outp = cmd.executeCommand("su -c 'ls -l'");
And even more. When I execute this command within the shell directly I get the following output:
shell#hammerhead:/ $ su -c 'ls -l'
drwxr-xr-x root root 1970-06-20 18:01 acct
drwxrwx--- system cache 2016-06-21 22:06 cache
-rwxr-x--- root root 272364 1970-01-01 01:00 charger
dr-x------ root root 1970-06-20 18:01 config
I also tryed using the complete path:
shell#hammerhead:/ $ /system/xbin/su -c 'ls -l'
drwxr-xr-x root root 1970-06-20 18:01 acct
drwxrwx--- system cache 2016-06-21 22:06 cache
Also within the application. I guess its a parsing error. Sometimes I see people adding the "\n" at the end of a command? No clue why. I really appreciate any help within this topic. Thanks!
I think the way to pass arguments to the su command would be to use a string array:
Runtime.getRuntime().exec(new String[]{"su","-c", "ls /data/data/"});
I have been working on this small project in college about changing the default DNS of wifi network to a custom DNS like Google, OpenDNS, Metacert, etc.
I know I have to write a shellscript inside the app's code that would edit the hosts file in the filesystem.
The problem is I have no idea where to start from. I have researched on google for some time and I couldn't figure anything.
If anyone knows about it, please guide me. Please tell me the name of the file to be edited, its location, what commands are required and how to run those commands' combination as a shellscript on a click of a button on the UI of app.
EDIT : I'm stuck only at this. Any help will be greatly appreciated.
I'm not sure about which files you would have to edit but this should give you the tools you need to do that.
The first thing you need to do is root the phone if you haven't already. If it's not rooted, you'll run into an issue like: Working Directory : null environment when running Process.Builder on android
There are a lot of guides available for that online. Install SuperSU as well. In order to run shell commands or scripts you should look at the ProcessBuilder class in Android:
http://developer.android.com/reference/java/lang/ProcessBuilder.html
I've given some sample code below to help you along the way. You could execute this in an OnClick() for a button.
/**
* Runs the shell command.
*
* #param command an array of Strings. command[0] contains the name of the
* shell command. command[1]... contains parameters.
*
* #return the text outputted by the command to stderr or stdout
*/
String runCmd(String[] command, boolean readOutput,
boolean waitForExit) {
ProcessBuilder probuilder = new ProcessBuilder()
.command(command)
.redirectErrorStream(true);
String output = "";
Process process;
// Log.d("MyShellCommand", "Executing " + command[0]);
try {
process = probuilder.start();
} catch(IOException e) {
return e.getMessage();
}
if (readOutput) {
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
try {
while ((line = br.readLine()) != null) {
// Log.d("MyShellCommand", "Read a line: " + line);
output += line + "\n";
}
} catch(IOException e) {
output = e.getMessage();
}
}
I'm writing an android app, and i need to read 7 SYSTEM files at start up. What would be the most efficient way to do this?
This is my code right now and it's pretty slow
read(file1);
read(file2);
...
read(file7);
...
public static String read(String file) {
String fileContents = "";
try {
String[] args = {"/system/bin/cat", file };
ProcessBuilder cmd = new ProcessBuilder(args);
Process p = cmd.start();
InputStream stream = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
fileContents = reader.readLine();
reader.close();
stream.close();
}
catch (IOException e) {
e.printStackTrace();
}
return fileContents;
}
There has to be some way to cat each file without closing the stream which should significantly speed things up. I tried making a shell script with the 7 cat cmds and reading the output but everything is all mashed together and i can't split the results.
I tried splitting the cat cmds with echo cmds to form a deliminator in the output:
cat file1
echo !
cat file2
echo !
But the deliminators "!" don't show up in the output and i can't figure out why.
Any suggestions?
I guess you could try multiple threads. Probably 7 would do just fine, but you have to check if 7 is not too much for the slowest device you are targeting. You need to leave some CPU for UI thread.