I am trying to make USSD call to send AT command. When I execute code, I get following error:
"Error running exec(). Command: [AT+CUSD=1,"*222#",15] Working Directory: null Environment: null"
My code is:
String args="AT+CUSD=1,\"*222#\",15";
try {
java.lang.Process process=Runtime.getRuntime().exec(args);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
Log.e("zoraf",bufferedReader.toString());
} catch (IOException e) {
e.printStackTrace();
}
What can be done to solve this issue ?
It seams that you lack some basic understanding of what AT commands are and how they operate. AT commands are not something that you pass to a shell or the operating system like Runtime.getRuntime().exec(args) implies. AT commands are text command that you send to a modem over a serial interface (e.g. USB, Bluetooth, RS-232 or a virtual one).
The absolutely best place for you to start is to read all of chapter 5 of the V.250 specification. It is an extremely important document that will guaranteed teach you much more about AT commands than you currently know.
Related
I am relatively new to android and I just came across adb shell commands which provide a lot of useful things. My issue is that when I execute the command "top -bn1" using adb shell from terminal it gives me a bunch of processes with users - root, system and other users. But when I run the same command from an application using Runtime.getRuntime().exec("top -bn1") the ouput is totally different and only processes for a particular user are displayed.
Here is the adb output -
And here is the code in the app and its output -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
process = Runtime.getRuntime().exec("top -bn1");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String msg = bufferedReader.lines().collect(Collectors.joining("\n"));
Log.d("Data", msg);
} catch (IOException e) {
e.printStackTrace();
}
}
Can somebody please explain -
Why is there a difference in the outputs as such?
How can I get the entire output by running the command from application?
What all requirements are there for the same?
adb shell is member of a lot of groups so it has a lot of privileges. It is not root, but it is as close as it gets. You can see the list of groups that the shell user is added by running id command from the terminal or check out this answer. On the other hand, your application runs in a sandbox and have very limited privileges. That's why you see a limited info when you run a command from the app code.
If you want to have unlimited access to the system from within your application, you need to request root access. You can do that by running the su command in a rooted phone:
Runtime.getRuntime().exec("su");
I have been trying to run a few Linux commands on my android phone with
Process process = Runtime.getRuntime().exec(COMMAND);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
And noticed that I can only run specific commands and get the proper output even if the device is rooted with SuperSU (I have also tested it with a device without SuperSU on it).
For example, if I run ls and try to put it on a screen (through a TextView) as follows:
public void onBtnClick(View view) {
try {
EditText commandLine = findViewById(R.id.commandText);
Process process = Runtime.getRuntime().exec(commandLine.getText().toString());
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
((TextView) findViewById(R.id.mainTextView)).setText(((TextView) findViewById(R.id.mainTextView)).getText() + "\n" + in.readLine());
commandLine.setText("");
} catch (IOException e) {
e.printStackTrace();
}
}
the output is acct which makes sense.
But on the other hand, if lets say I run pwd it gives me the following error:
W/System.err: java.io.IOException: Error running exec(). Command:
[pwd] Working Directory: null Environment: null
I did some research online and stumbled upon Termux that can have complete control over the phone through a terminal which is exactly what I'm looking to make (for my own learning and testing purposes).
And although it's just an emulator it can do exactly what I want but the only problem is that it requires I input the commands through the terminal.
What I'm here for is to sort of replicate what Termux does for myself so that I could run Linux commands properly from the Java code of the application, not requiring the user to actually input commands.
Can anyone help me with where to start and the basics of running those commands properly on my device?
Termux states that it doesn't work as a traditional Linux bash does since it sets its own virtual-ish environment when first setting up in its data directory in /data/data/com.termux/files/usr see here and here
Also, according to the official android docs, the exec(command) method,
Executes the specified string command in a separate process.
This is a convenience method. An invocation of the form exec(command)
behaves in exactly the same way as the invocation exec(command, null,
null).
So if it's a separate process, when executing a command, it will run it inside that process' directory (Each process in Linux gets its directory and is assigned an PID which os uses). So what the ls command gives you is simply whatever's inside that process' directory. You should be somewhere like /proc/31415/ and there's only a acct file (cgroup in regular Linux).
What you should be doing is running the command inside a directory by declaring it when invoking getRuntime().exec() see the link above to find the right one you'll need. I'd suggest using the override which handles all the parameters.
You'll need something like this:
String[] cmd = {"mkdir", "testDir"};
File env = new File(getFilesDir().getAbsolutePath());
Runtime.getRuntime().exec(cmd, null, env);
Also, it doesn't hurt to take a look at Termux's installer code (exec(). It'll give you a good overlook to setting up your environment as well as working with basic commands.
Also, I think you've done it already but double check to make sure that you're requesting WRITE_EXTERNAL_STORAGE permission for your application.
I change the permission of /system/xbin/tcpdump to 777 and write the code in my Android APP like below.
But I still get the error message "stderr=tcpdump: any: You don't have permission to capture on that device" and "stderr=(socket: Operation not permitted)".
Is there anyone know what's the problem here?
Thanks.
Process process
String[] cmd = {"/system/xbin/tcpdump", "-i any", "-w /sdcard/tcpdump.pkt"};
TextUtils.join(" ", cmd);
process = Runtime.getRuntime().exec(cmd);
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = errorReader.readLine()) != null) {
Log.d(TAG, "stderr="+line);
}
Android does not support raw sockets, which would include the ability to collect raw frames using a sniffer, by any user space process on non-rooted devices.
If you check the main support site for TCPDump on Android, you'll find that one of the very first requirements is a rooted Android device.
I have some test built, and my testing department can't figure out how to use the terminal. Uiautomator test are .jar files so must be ran via terminal. So for convenience, I want to make an app for them with the tests in a list to choose from to execute. Is this possible? My research leads me to believe that the devices will need to be rooted. If that is the case I will not be able to do it. So is there a workaround to this? This is what I have tried:
Runtime rt = Runtime.getRuntime();
try {
Process process = rt.exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("uiautomator runtest test.jar -c ui.test.getData\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
This code will produce this :
java.io.IOException: Error running exec(). Command: [su] Working Directory: null Environment: null
Please help me with a way to get this to work. Thanks!
Do you have suggestions of something I could do to make it easy for my testing team to run a test of mine?
Create a desktop app that runs the test.
Or, just put a nice batch file/shell script in front of it. Perhaps one that launches the test and displays a photo of a cute kitten in the tester's Web browser.
(testers love cute kittens)
Or, set up a continuous integration server, so testers are not manually running the tests at all -- the tests are run automatically, and the testers are simply examining the results. I presume that somebody has a recipe for Hudson/Jenkins/whatever that can run an uiautomator test. And there may be a separate recipe for integrating photos of cute kittens into the test result reports, though it is possible that you'll be stuck writing your own for that.
I am making an android application which can get ping response time from android application.
I have already done it with windows environment but when i take it onto android device, device can't send any response. How can i solve this problem.?
Thanks in advance.
here is my code
String ip="www.google.com"; String pingResult=" ";
String pingCmd="ping"+ip;
Runtime r=Runtime.getRuntime();
Process p=r.exec(pingCmd);
BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
String inputLine;
Toast.makeText(getApplicationContext(), "Going loop", 1).show();
while((inputLine=in.readLine())!=null)
{
pingResult=inputLine;
}
Toast.makeText(getApplicationContext(),pingResult, 1).show();
in.close();
Your will work fine if you change the line:
Process p=r.exec(pingCmd);
to:
Process p=r.exec(new String[] {"ping", "-c 4", "www.google.pt"});
The reason is:
-Android exec command expects a String[] instead of String.
-The -c parameter is required to limit the number of pings to 4 (to replicate the windows behaviour). Otherwise it will ping forever.
Finally, this only works on a real device. To have it working in the emulator, you would need to configure adb to redirect ping replays to the emulator.
Good luck.