Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Create funcion shell interactive #118

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

mgpx
Copy link

@mgpx mgpx commented Apr 26, 2018

With this feature you have the possibility to create an interactive shell, where only one shell is created and run several commands in the same instance

I implemented this functionality to send commands dynamically and solve the problem of a very long command, I will show below. This question in stackoverflow

su -c 'sqlite3 /data/data/com.example/databases/messages.db "select id, data from messages WHERE messages.data='EXIT';" '

returns me the error no such column 'EXIT'

while both commands down worked

adb shell
adb su
sqlite3 /data/data/com.example/databases/messages.db "select id, data from messages WHERE messages.data='EXIT';"

and

adb shell
su -c 'sqlite3 /data/data/com.example/databases/messages.db "select id, data from messages;"

to solve this problem, or to execute commands within another executable, my problem can be solved with the following implementation

SharpAdbClient.InteractiveShell.Shell shell = new SharpAdbClient.InteractiveShell.Shell(AdbClient.Instance as AdbClient);
shell.ResponseAdb += Shell_ResponseAdb;
await shell.ExecuteRemoteCommandAsync($"su -c 'sqlite3 \"{file}\"'", deviceData, CancellationToken.None, int.MaxValue);


private static bool Shell_ResponseAdb(string adbResponse, SharpAdbClient.InteractiveShell.ShellResponseEventArgs args)
{ 
	 //last message showing in adb
	if (adbResponse.Contains("Enter SQL statements terminated with a"))
	{
		args.NextCommand = "select distinct id, data from messages WHERE messages.data LIKE 'EXIT' ;";
	}
	else if (adbResponse.Contains("select distinct id, data from messages WHERE messages.data LIKE 'EXIT' ;"))
	{
		args.NextCommand = ".exit";
	}
	else if (adbResponse.Contains(".exit"))
	{
		args.CloseShell = true;
	}
	else if (adbResponse.Contains("|EXIT"))
	{
		//rows sql
	}
	return true;
}

@@ -402,7 +402,7 @@ public Stream GetShellStream()
/// <remarks>
/// This uses the default time out value.
/// </remarks>
protected bool Write(byte[] data)
internal bool Write(byte[] data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you're actually using this method, right? So can we keep it protected?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, you're right, I called this function, but I switched to

 (shellStream.Inner as System.Net.Sockets.NetworkStream).Write(bytes, 0, bytes.Length);

I'll correct it, thank you.

@qmfrederik
Copy link
Contributor

Thanks, I would need to think a bit about the API surface of this and whether it's something we want to host in the core library before merging it.

Just to be sure - I believe you can create the InteractiveShell class in a project that references SharpAdbClient, right?
That is, all methods you need from SharpAdbClient are public?

@mgpx
Copy link
Author

mgpx commented Apr 27, 2018

I will correct, and add a description in the methods

Just to be sure - I believe you can create the InteractiveShell class in a project that references SharpAdbClient, right?

Yes it is possible, I started to change this project because my initial idea was to send an array of commands and it ran inside, but I had some problems in relation to this, so the solution I found was this .

Tks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants