Android Tutorial Running shell commands as SuperUser

Status
Not open for further replies.
if you want run any shell command as superuser(you must have rooted phone), try this:
B4X:
Dim Command, Runner As String
Dim StdOut, StdErr As StringBuilder
Dim Result As Int
Dim Ph As Phone
Runner = File.Combine(File.DirInternalCache, "runner")
Command = File.Combine(File.DirInternalCache, "command")
File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
File.WriteString(File.DirInternalCache, "command", "modprobe cifs" & CrLf & "modprobe aufs" & CrLf & "exit") 'Any commands via crlf, and exit at end 
Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
Msgbox(StdOut.tostring, "")
 

mc73

Well-Known Member
Licensed User
Longtime User
You can try to list the files in a restricted folder (ls).

Today I'm searching for a way to check root too. Erel, isn't it true that one can always restrict access to such folders, even in root mode? Thus, how can I be sure that the phone is unrooted if I get an error with ls?
From what I see googling, it is not an easy path to really say if the phone is rooted or not. Any ideas would be greatly appreciated.
 

madSac

Active Member
Licensed User
Longtime User
Probably data folder is always restricted without root permission.
Note that I am saying root permission.If user don't grant you root permission then you cannot say anything. You will have to treat it as unrooted.

Any user cannot block read permission to data folder. So ls command will work if root permission is provided to your app.
 

EvgenyB4A

Active Member
Licensed User
Longtime User
Hi
I want update APK silently (without user confirmation) for rooted device and found the following code:
protected void raise_notification() {
String update_file = preferences.getString(UPDATE_FILE, "");
boolean silent_update_failed = preferences.getBoolean(SILENT_FAILED, false);
if( update_file.length() > 0 && !silent_update_failed ) {
final String libs = "LD_LIBRARY_PATH=/vendor/lib:/system/lib ";
final String[] commands = {
libs + "pm install -r " + context.getFilesDir().getAbsolutePath() + "/" + update_file,
libs + "am start -n " + context.getPackageName() + "/" + get_main_activity()
};
execute_as_root(commands); // not supposed to return if successful
preferences.edit().putBoolean(SILENT_FAILED, true).commit(); // avoid silent update loop
}
super.raise_notification();
}


How to compose the strings of "pm install" and "am start" with B4A?
 

XPDT

New Member
if you want run any shell command as superuser(you must have rooted phone), try this:
B4X:
Dim Command, Runner As String
Dim StdOut, StdErr As StringBuilder
Dim Result As Int
Dim Ph As Phone
Runner = File.Combine(File.DirInternalCache, "runner")
Command = File.Combine(File.DirInternalCache, "command")
File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
File.WriteString(File.DirInternalCache, "command", "modprobe cifs" & CrLf & "modprobe aufs" & CrLf & "exit") 'Any commands via crlf, and exit at end
Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
Msgbox(StdOut.tostring, "")
I get this error:
Unknown Type: Phone
 

alexhi

Member
Licensed User
Longtime User
Hi all !
if I send command in ADB shell /system/bin/sunxi-pio -m PA8=0 this is work OK
If I write :
Command = File.Combine(File.DirInternalCache, "command")
File.WriteString(File.DirInternalCache, "runner", "< " & Command)
File.WriteString(File.DirInternalCache, "command", "/system/bin/sunxi-pio -m PA8=0" & CRLF & "exit") 'Any commands via crlf, and exit at end
Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
This is not work :( Why? (ORANGE PI H3Droid i have ROOT ) Snx.
 
Status
Not open for further replies.
Top