Giter Site home page Giter Site logo

rxshell's Introduction

RxShell

Build Download Coverage Status

A library that helps your app interact with shells on Android.

Quickstart

Include the library in your modules build.gradle file:

implementation 'eu.darken.rxshell:<insert-latest-release>'

Now your project is ready to use the library, let's quickly talk about a few core concepts:

  1. You construct a shell using RxCmdShell.builder().
  2. The shell has to be opened before use (shell.open()), which will launch the process and give you a RxCmdShell.Session to work with.
  3. Build your commands with Cmd.builder("your command").
  4. Commands are run with session.submit(command), cmd.submit(session) or cmd.execute(session).
  5. Remember to close() the session to release resources.

Examples

Single-Shot execution

If you pass a shell builder to the command it will be used for a single shot execution.

A shell is created and opened, used and closed.

Cmd.execute(...) is shorthand for Cmd.submit(...).blockingGet(), so don't run it from a UI thread.

Cmd.Result result = Cmd.builder("echo hello").execute(RxCmdShell.builder());

Reusing a shell

If you want to issue multiple commands, you can reuse the shell which is faster and uses less resources.

RxCmdShell.Session session = RxCmdShell.builder().build().open().blockingGet();
// Blocking
Cmd.Result result1 = Cmd.builder("echo straw").execute(session);
// Async
Cmd.builder("echo berry").submit(session).subscribe(result -> Log.i("ExitCode: " + result.getExitCode()));
shell.close().blockingGet();

The default shell process is launched using sh, if you want to open a root shell (using su) tell the ShellBuilder!

Cmd.Result result = Cmd.builder("echo hello").execute(RxCmdShell.builder().root(true));

Checking root access

// General info
new RootContext.Builder(getContext()).build().subscribe(c -> {/* c.getRoot().getState() */});
// Just root state
Root root = new Root.Builder().build().blockingGet();
if(root.getState() == Root.State.ROOTED) /* yay */

Used by

  • SD Maid, which was also the motivation for this library.

Alternatives

While this is obviously :^) the best library, there are alternatives you could be interested in:

rxshell's People

Contributors

d4rken avatar joni-salminen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rxshell's Issues

Detect magisk hide

I tested Root Validator in nexus6 with magisk. Normally it works. Thanks! 👍
But if magisk hide enabled (with magisk manager hiding enabled),
RxShell rooting detection doesn't works.

Build issue: `SimException: invalid opcode ba`

Error during build after adding the library to the build.gradle file.

Error:com.android.builder.dexing.DexArchiveBuilderException: Error while dexing eu/darken/rxshell/process/RxProcess.class
Error:com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)

Y

Ggh

Command completion race condition

Seeing a rare racecondition likely related to Observable.zip in https://github.com/d4rken/RxShell/blob/master/core/src/main/java/eu/darken/rxshell/cmd/CmdProcessor.java#L111

1513428431737 V/RXS:RxCmdShell: open()
1513428431738 V/RXS:RxShell: open()
1513428431738 V/RXS:RxProcess: open()
1513428431740 V/RXS:RxProcess: processCreator:onNext(Process[pid=5851, hasExited=false])
1513428431740 D/RXS:RxProcess: open():doOnSuccess RxProcess.Session(process=Process[pid=5851, hasExited=false])
1513428431740 V/RXS:RxProcess:Session: input()
1513428431740 V/RXS:RxProcess:Session: output()
1513428431740 V/RXS:RxProcess:Session: error()
1513428431740 V/RXS:RxShell:Session: errorLines()
1513428431740 V/RXS:RxProcess:Session: destroy()
1513428431740 V/RXS:RxProcess:Session: waitFor()
1513428431740 V/RXS:RxShell:Session: waitFor()
1513428431740 V/RXS:RxShell: open():doOnSuccess RxShell.Session(processSession=RxProcess.Session(process=Process[pid=5851, hasExited=false]))
1513428431740 V/RXS:RxShell:Session: waitFor()
1513428431741 D/RXS:RxShell:Session: writeLine(line=V5B9D812A9C51=/data/user/0/eu.thedarken.sdm/files/toybox_sdm, flush=true)
1513428431741 V/RXS:CmdProcessor: attach(RxShell.Session(processSession=RxProcess.Session(process=Process[pid=5851, hasExited=false])))
1513428431741 V/RXS:RxShell:Session: waitFor()
1513428431741 V/RXS:RxShell:Session: waitFor()
1513428431741 V/RXS:RxShell:Session: cancel()
1513428431741 V/RXS:RxCmdShell: open():doOnSuccess eu.darken.rxshell.cmd.RxCmdShell$Session@9b97c1513428431741 V/RXS:RxCmdShell:Session: waitFor()
1513428431742 D/RXS:RxProcess:Session: Waiting for Process[pid=5851, hasExited=false] to exit.
1513428431742 D/RXS:CmdProcessor: Submitted: Cmd(timeout=0, commands=[$V5B9D812A9C51 test ! -e '/storage/emulated/0' || $V5B9D812A9C51 find -L '/storage/emulated/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user_de/0' || $V5B9D812A9C51 find -L '/data/user_de/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user/0' || $V5B9D812A9C51 find -L '/data/user/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N"])
1513428431742 D/RXS:CmdProcessor: Processing: Cmd(timeout=0, commands=[$V5B9D812A9C51 test ! -e '/storage/emulated/0' || $V5B9D812A9C51 find -L '/storage/emulated/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user_de/0' || $V5B9D812A9C51 find -L '/data/user_de/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user/0' || $V5B9D812A9C51 find -L '/data/user/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N"])
1513428431742 V/RXS:RxShell:Session: outputLines()
1513428431742 V/RXS:Harvester:Output: request(9223372036854775807)
1513428431742 V/RXS:RxShell:Session: errorLines()
1513428431742 V/RXS:Harvester:Error: request(9223372036854775807)
1513428431742 D/RXS:RxShell:Session: writeLine(line=$V5B9D812A9C51 test ! -e '/storage/emulated/0' || $V5B9D812A9C51 find -L '/storage/emulated/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", flush=false)
1513428431742 D/RXS:RxShell:Session: writeLine(line=$V5B9D812A9C51 test ! -e '/data/user_de/0' || $V5B9D812A9C51 find -L '/data/user_de/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", flush=false)
1513428431742 D/RXS:RxShell:Session: writeLine(line=$V5B9D812A9C51 test ! -e '/data/user/0' || $V5B9D812A9C51 find -L '/data/user/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", flush=false)
1513428431742 D/RXS:RxShell:Session: writeLine(line=echo fb6e4bdf-16ae-4f0d-b1b8-c3445f5d7ec1 $?, flush=false)
1513428431742 D/RXS:RxShell:Session: writeLine(line=echo fb6e4bdf-16ae-4f0d-b1b8-c3445f5d7ec1 >&2, flush=true)
1513428431775 V/RXS:Harvester:Error: Permission denied
1513428431775 D/RXS:Harvester:Error: Permission denied
1513428431776 V/RXS:RxShell: LineStream:error onComplete()
1513428431776 V/RXS:RxShell: LineStream:output onComplete()
1513428431776 V/RXS:Harvester:Error: onComplete()
1513428431776 V/RXS:Harvester:Output: onComplete()
1513428431776 V/RXS:Harvester:Error: onError(java.io.IOException: Upstream completed prematurely.)
1513428431776 V/RXS:Harvester:Output: onError(java.io.IOException: Upstream completed prematurely.)
1513428431776 V/RXS:CmdProcessor: errorLines():doOnEach: OnNextNotification[eu.darken.rxshell.cmd.Harvester$Crop@519a13a]
1513428431776 V/RXS:CmdProcessor: outputLine():doOnEach: OnNextNotification[eu.darken.rxshell.cmd.OutputHarvester$Crop@7e581eb]
1513428431776 V/RXS:RxProcess: processCreator:onComplete()
1513428431776 V/RXS:CmdProcessor: errorLines():doOnEach: OnCompleteNotification
1513428431776 V/RXS:Harvester:Error: cancel()
1513428431776 V/RXS:RxProcess: cancel()
1513428431777 D/RXS:RootKiller: kill(Process[pid=5851 ,hasExited=true, exitcode=1])
1513428431777 D/RXS:RootKiller: Process is no longer alive, skipping kill.
1513428431777 V/RXS:RxProcess: Process finished, clearing session
1513428431777 V/RXS:RxShell: LineStream:error onCancel()
1513428431777 D/RXS:RxProcess:Session: Exitcode: 1, Process: Process[pid=5851 ,hasExited=true, exitcode=1]
1513428431777 V/RXS:RxProcess:Session: waitFor():doOnSuccess 1513428431777 V/RXS:RxShell:Session: waitFor():doOnSuccess 1513428431777 V/RXS:CmdProcessor: Attached session ended!
1513428431777 V/RXS:CmdProcessor: Poison pill!
1513428431777 D/RXS:CmdProcessor: Post zip: OnNextNotification[QueueCmd(command=Cmd(timeout=0, commands=[$V5B9D812A9C51 test ! -e '/storage/emulated/0' || $V5B9D812A9C51 find -L '/storage/emulated/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user_de/0' || $V5B9D812A9C51 find -L '/data/user_de/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user/0' || $V5B9D812A9C51 find -L '/data/user/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N"]), exitCode=-3, output.size()=null, errors.size()=null)]
1513428431777 V/RXS:CmdProcessor: onNext(QueueCmd(command=Cmd(timeout=0, commands=[$V5B9D812A9C51 test ! -e '/storage/emulated/0' || $V5B9D812A9C51 find -L '/storage/emulated/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user_de/0' || $V5B9D812A9C51 find -L '/data/user_de/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user/0' || $V5B9D812A9C51 find -L '/data/user/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N"]), exitCode=-3, output.size()=null, errors.size()=null))
1513428431777 V/RXS:RxShell:Session: cancel()
1513428431777 I/RXS:CmdProcessor: Processed: Cmd.Result(cmd=Cmd(timeout=0, commands=[$V5B9D812A9C51 test ! -e '/storage/emulated/0' || $V5B9D812A9C51 find -L '/storage/emulated/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user_de/0' || $V5B9D812A9C51 find -L '/data/user_de/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N", $V5B9D812A9C51 test ! -e '/data/user/0' || $V5B9D812A9C51 find -L '/data/user/0' -mindepth 1 -print0 | $V5B9D812A9C51 xargs -0 -r -s 65536 $V5B9D812A9C51 stat -c "%a:%F:%d:%i:%u:%g:%s:%B:%b:%Y:%N"]), exitcode=-3, output.size()=null, errors.size()=null)
1513428431777 V/RXS:RxProcess:Session: destroy():doOnComplete
1513428431777 D/RXS:CmdProcessor: Post zip: OnCompleteNotification
1513428431777 V/RXS:RxShell:Session: cancel():doOnComplete
1513428431777 V/RXS:CmdProcessor: onComplete()
1513428431777 V/RXS:CmdProcessor: outputLine():doOnEach: OnCompleteNotification
1513428431777 V/RXS:Harvester:Output: cancel()
io.reactivex.exceptions.OnErrorNotImplementedException: Upstream completed prematurely.
	at io.reactivex.e.b.a$af.accept(Functions.java:1704)
	at io.reactivex.e.h.k.onError(LambdaSubscriber.java:79)
	at io.reactivex.i.b.onError(PublishProcessor.java:2325)
	at eu.darken.rxshell.cmd.Harvester$BaseSub.onError(Harvester.java:96)
	at eu.darken.rxshell.cmd.Harvester$BaseSub.onComplete(Harvester.java:102)
	at io.reactivex.e.j.k.a(HalfSerializer.java:91)
	at io.reactivex.e.h.s.onComplete(StrictSubscriber.java:109)
	at io.reactivex.e.e.b.co$a.onComplete(FlowableRefCount.java:82)
	at io.reactivex.e.e.b.cg$c.a(FlowablePublish.java:366)
	at io.reactivex.e.e.b.cg$c.c(FlowablePublish.java:429)
	at io.reactivex.e.e.b.cg$c.onComplete(FlowablePublish.java:252)
	at io.reactivex.e.e.b.dm$a.onComplete(FlowableSubscribeOn.java:108)
	at io.reactivex.e.e.b.aa$a.c(FlowableCreate.java:262)
	at io.reactivex.e.e.b.aa$a.n_(FlowableCreate.java:254)
	at eu.darken.rxshell.shell.RxShell.lambda$makeLineStream$6$RxShell(RxShell.java:190)
	at eu.darken.rxshell.shell.RxShell$$Lambda$6.subscribe(Unknown Source:4)
	at io.reactivex.e.e.b.aa.subscribeActual(FlowableCreate.java:72)
	at io.reactivex.i.subscribe(Flowable.java:13041)
	at io.reactivex.i.subscribe(Flowable.java:12987)
	at io.reactivex.e.e.b.dm$a.run(FlowableSubscribeOn.java:82)
	at io.reactivex.e.g.l.run(ScheduledRunnable.java:61)
	at io.reactivex.e.g.l.call(ScheduledRunnable.java:52)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
	at java.lang.Thread.run(Thread.java:764)
Caused by: java.io.IOException: Upstream completed prematurely.
	... 23 more

How to input on terminal waiting response

Hi, I'm trying your library, but testing with some commands like adb pair, after launching it, the terminal ask user to input a number:

C:\Android\sdk\platform-tools>adb pair localhost:12333
Enter pairing code:

How it is possible to wait this response and writing something to continue. Calling like the next, keeps the proccess running without exit.

private fun rxshellOpenClose(command: List<String>): Cmd.Result? {
        val session = RxCmdShell.builder().build().open().blockingGet()
        val resultBlocking = Cmd.builder(command).execute(session)      // Blocking
        session.close()
        return resultBlocking
    }

thank you @d4rken

Is it possible to read the command line output instantly?

When a time-consuming binary is executed, the output is always available after execution. And I need to respond immediately to each line of output.

Sample pseudocode:

int main(int argc, char **argv) {
    printf("sleep for 5 sec.\n");
    sleep(5);
    printf("exit.\n");
    return EXIT_SUCCESS;
}

I need to get a callback such as "onLineOutput"(or "onNext" in Rx) while "sleep for 5 sec." output, not all things after 5 seconds.

Please help me, even if it is to provide a way to implement it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.