Android Tutorial Running shell commands as SuperUser

Discussion in 'Tutorials & Examples' started by m0narX, Dec 14, 2010.

Thread Status:
Not open for further replies.
  1. m0narX

    m0narX Member Licensed User

    if you want run any shell command as superuser(you must have rooted phone), try this:
    Code:
    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, "")
     
    hani bassam, XPDT, camolas and 4 others like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Very nice. You are creating a SH script and then run it. Thank you for sharing.
     
  3. anaylor01

    anaylor01 Well-Known Member Licensed User

    I put this script in a button click event and I get the error Object should first be initialized (StringBuilder). on this line.
    Code:
    Result = Ph.Shell("sh"Array As String(Runner), StdOut, StdErr)
     
    Last edited: Jun 6, 2011
  4. Erel

    Erel Administrator Staff Member Licensed User

    Did you declare Ph? It should be a Phone object.
     
  5. anaylor01

    anaylor01 Well-Known Member Licensed User

    I put the code exactly as it is in the button click event. The code has Dim Ph as phone. Is there something else I need to do ?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Sorry, the problem is actually with StdErr and StdOut. You should call Initialize on each of them.
     
  7. anaylor01

    anaylor01 Well-Known Member Licensed User

    Sorry but I am new to this and am still figuring things out. So if you could show me the code I would appreciate it. Thanks.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Add this code before calling Ph.Shell:
    Code:
    StdOut.Initialize
    StdErr.Initialize
     
  9. anaylor01

    anaylor01 Well-Known Member Licensed User

    Thank you. This is awesome.
     
  10. anaylor01

    anaylor01 Well-Known Member Licensed User

    Maybe you can help me with this. I am trying to remove the SMS limit. I found some information on the net but am having trouble getting it to work. How to Increase the SMS Limit on AndroidIt keeps saying "No Such Table". Here is what I have so far.
    Code:
    SQL1.Initialize(File.DirInternal, "settings.db"True)
    lblsendingto.Text =  SQL1.ExecQuerySingleResult(
    "SELECT count(name) FROM secure")'system name secure name gservices Name value
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question in the questions sub forum.
     
  12. fpdianzen

    fpdianzen Member Licensed User

    hi!

    hi thank you for this info, yet im still confused.
    i wanted to enter this, shell commands.
    what format should i use? :sign0104:


    Code:
    #su
    #setprop ctl.stop adbd
     
  13. Erel

    Erel Administrator Staff Member Licensed User

  14. madSac

    madSac Active Member Licensed User

    One ques.
    How can i check if a device has SuperUser or not ?
    or say device is rooted or not ?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    You can run a script that will fail on unrooted devices and check the error string.
     
  16. madSac

    madSac Active Member Licensed User

    Any basic command which will have no effect ?
    I don't know anything about shell commands but I came to know its potential in this forum only.Even thought I rooted my phone much before but never tried to run shell commands.
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    You can try to list the files in a restricted folder (ls).
     
  18. madSac

    madSac Active Member Licensed User

    whenever i use this code
    Code:
    Dim p As Phone
    Dim sb,se As StringBuilder
    sb.Initialize
    se.Initialize
    p.Shell(
    "ls",Null,sb,se)
    Log(sb)
    Log(se)
    i get
    Code:
    opendir failed, Permission denied
    in logs.
    i have setting to prompt for access in SU but i get No prompt for superuser permission and this error in log.Am i mistaken somewhere ?
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    You should follow the example in the first post.
     
  20. madSac

    madSac Active Member Licensed User

    Ok.
    I tried it, it worked.
    Thank You Erel and m0narX.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice