Android: Android kill tcpdump process - android

First sorry for my bad English.
I have developed an app in Android Studio. It call tcpdump (Process process = Runtime.getRuntime().exec("su -c tcpdump -s 0 -v -w /sdcard/capture.pcap");). Initially it works correctly. However, tcpdump process is killed abruptly. Would anyone tell me why?
Thank you

It might tell you why on standard error:
final Process p = Runtime.getRuntime().exec(tcpdumpCommand);
final BufferedReader reader = new BufferedReader(
new InputStreamReader(p.getErrorStream()));
(new Thread() {
public void run() {
while ((line = reader.readLine()) != null) {
Log.d("tcpdump", "stderr: " + line);
}
Log.d("tcpdump", "end of stderr");
}
}).start();
...and then look in logcat. Or try the same with getOutputStream() (for stdout) instead of getErrorStream().
Also if tcpdump was successfully capturing traffic and then abruptly stopped, one possible reason is you've filled the SD card.

Related

Starting adb logs on internal storage without going into the shell

I am trying to start internal logs within my android devices without having to go directly into the adb shell. I have to script this process so I can run it from a program.
I am aware that you can take the following steps to start internal logs on an android device:
Open a cmd prompt
enter 'adb shell'
enter 'logcat -v time -f /sdcard/LogFile.txt&'
The above will start a logcat process within the actual device. I can now unplug my phone from the computer and move around then come back and collect the logs once my test is complete. It's crucial that I am able to start this process and be able to unplug my device with the logs still running.
To my knowledge running 'adb shell' in front of any command would run as if it were in the shell. Therefore by this logic I tried running:
Method 1:
'adb shell logcat -v time -f /sdcard/LogFile.txt&'
This command did properly start the log on the device which is great. However, once I unplug from my computer the logcat process stops.
Method 2:
'adb shell "logcat -v time -f /sdcard/LogFile.txt&" '
This didn't seem to do anything at all on the phone and I don't know why.
Method 3
I have tried the scripting method as well where I run a Batch file that contains only:
'adb shell < Commands.txt'
Where commands has the single line:
'logcat -v time -f /sdcard/LogFile.txt&'
This doesn't appear to do anything. It appears to send the command once the window comes up but doesn't actually perform the action.
Any help on this topic would be greatly appreciated. Thanks.
you can use this code in application class. so when app will launch this code will start executing. and it will create new log file according to current time.
Thread t = new Thread(new Runnable() {
public void run() {
while (collectLog == true) {
try {
Thread.sleep(1000 * 60 * 6);
StringBuilder log = null;
Date now = new Date();
String fileName = formatter.format(now);
File file = new File(dir, fileName + "_logcat2.txt");
try {
Process process = Runtime.getRuntime().exec("logcat -d");// d will dump logs
//Process process = Runtime.getRuntime().exec("logcat -c"); c will clear logs
// process = Runtime.getRuntime().exec("logcat -f " + file);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
} catch (IOException e) {
}
try {
//to write logcat in text file
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut)
// Write the string to the file
osw.append(log.toString());
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();

Android Wear - code to read LogCat output programmatically and continuously is not working

Hi everyone I recently wrote some code to read LogCat output continuously within a background Thread started by a background Service in my app. I do not think there are errors in my code but it's not working.
In my Thread, I have:
#Override
public void run() {
Process process = null;
String cmd = "logcat -v time | grep 'AudioFocus requestAudioFocus()'";
try {
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("logcat -c" + "\n");
os.writeBytes(cmd + "\n");
os.flush();
} catch(Exception e) {
}
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
try {
while ( (line = reader.readLine()) != null ) {
Log.e(StaticVal.TAG, line);
// code for write line to my own log file
}
} catch(Exception e) {
}
}
I also added permission to manifest:
<uses-permission android:name="android.permission.READ_LOGS" />
However, when I plug my device with PC and test it with Android Studio, Log.e(StaticVal.TAG, line); did not print out the results and nothing wrote into my own log file.
Any help is appreciated!
(Directly run logcat -v time | grep 'AudioFocus requestAudioFocus()' on my device through adb terminal get the results as expected)

getRuntime Android show process

I'm doing a simple app that show the currently process in android, like a shell.
My app execute ls, cd, makedir and other commands, but top or htop command doesnt. (htop doesn't recognize, and top, the app freeze). I need root to this? I've downloaded the terminal app in unrooted android and top command works.
my app has 2 class. a principal and a shell
principal class
public void onClick(View arg0) {
// TODO Auto-generated method stub
ShellExecuter exe = new ShellExecuter();
command = input.getText().toString();
String outp = exe.Executer(command);
out.setText(outp);
Log.d("Output", outp);
}
shell class
public String Executer(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
Why some commands the app works and top for example doesn't??
If you have your Device connected, go to the shell to see what commands you have available using the following command in your computer's command line:
adb shell
At first glance you will be able to tell that "top" is a job that "displays and update sorted information about processes" and it blocks the shell commands line, hence, in your application is locking the thread that executed that command.
Hope it Helps!
Regards!
thanks. adb shell with top command show the process
but
p = Runtime.getRuntime().exec("top");
doesn't maybe works??
I don't understante cause my app freeze
or a top -n 1 not fixed?

Runtime.exec() isn't working

I'm trying to get the radio logs from Android device. I've found this snippet to read radio logs. However, the control never enters the while loop.
try {
String line = "";
Process process = Runtime.getRuntime().exec("adb logcat -b radio");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
Log.i("entered loop", log.toString());
log.append(line + "\n");
}
Log.i("app", log.toString());
} catch (IOException e) {}
The code works fine if we replace the command as adb logcat -d as parameter to exec().
I think the control is stuck in the line = bufferedReader.readLine() statement in condition.
EDIT:
Basically, I want to read the notification that is displayed on the screen. For example: The notification that we see after the call or sending SMS displaying remaining balance.
The notification is actually printed in the Android Logs and thus I am reading Logs and extracting the message from it. Is there any other way to store all notifications displayed on screen.
Please help me in this regard.
If you're running this code on a device, try replacing
Process process = Runtime.getRuntime().exec("adb logcat -b radio");
with
Process process = Runtime.getRuntime().exec("/system/bin/sh" + "-c" + "adb logcat -b radio");
otherwise, try replacing it with (on linux)
Process process = Runtime.getRuntime().exec("/bin/sh" + "-c" + "adb logcat -b radio");
or (on windows)
Process process = Runtime.getRuntime().exec("C:\\Windows\\System32\\cmd.exe" + "-c" + "adb logcat -b radio");

How to prevent executing Linux commands in java in separate process?

I am developing application to filter logcat generated by android.I executed linux comand '/system/bin/logcat -b main -f /sdcard/logcat.txt'using getruntme.exec. to execute the command but it is running in separate process though i uninstall my application the process keeps on running .Is there any way i can prevent running in another process.
By getruntme.exec, do you mean you're using something like this? (borrowed from here):
import java.io.*;
public class TestExec {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("cmd /C dir");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Then you could kill it with p.destroy()
No. exec spawns a new process, that is what it is supposed to do - and what you want to do if you start an external command. But it returns a Process object that you can use to manipulate the running process.

Categories

Resources