0

I made a simple shell (actually windows batch) script that automates a task on android phone. It can be ran on any computer. I thought that if I convert the batch to linux sh script, I could issue the commands without adb shell before them.

So this (.exe added for clarity):

adb.exe shell sleep 1

becomes this:

sleep 1

Well it works with sleep in particular, but not with sendevent. I made a script to press and release focus button:

#!/bin/sh
# Simply send two key events to see if it works (spoiler: it doesn't)
sendevent /dev/input/event0 1 212 1
sendevent /dev/input/event0 0 0 0
sleep 1
sendevent /dev/input/event0 1 212 0
sendevent /dev/input/event0 0 0 0

But I cannot execute the sendevent commands:

image description

That's ridiculous. Obviously my phone is capable of executing the commands from external source, so why the hell not from itself? Am I going to need a computer to work with the shell?

What can I do to issue adb commands to my own phone? (I am not using rooted device. Android developers made it too much painful to achieve.)

Tomáš Zato
  • 390
  • 4
  • 11
  • 25
  • 1
    Whatever you use to execute that shell script does not have the appropriate permissions to open the event0 device file. This has nothing to do with whether or not you can execute a command. This is special to that single command and can be solved by requesting appropriate permissions (root permissions in that case). – GiantTree May 26 '16 at 23:00
  • @GiantTree Well it cannot be resolved the way you propose, because my device is not rooted. Does bold text display properly on your device? Solution I'm looking for is somehow connecting to the adb service on my device to send those commands there, since as you correctly say my used doesn't have access to the required resources. – Tomáš Zato May 26 '16 at 23:05
  • I saw that you don't have a rooted device. This does not mean that those permissions are not required on your device. Ideally a permissive kernel would suffice, because on my device, that exact file you posted runs perfectly without any errors. – GiantTree May 26 '16 at 23:09
  • I don't understand now. Are you saying the permission can be obtained without having rooted device? – Tomáš Zato May 27 '16 at 00:11
  • Sorry, I try to explain: There are 2 modes a kernel can be in: enforcing (locked down, high security) and permissive (somewhat open, medium security). Most/all devices ship with enforcing-only kernels. To get the needed permissions you need to either root your device and run the commands in a root context (after su) or by flashing a custom kernel compiled for permissive mode (most of which root your device or need root for management anyway). There might be another way around your issue, if so, please open a new question stating what you want to achieve. – GiantTree May 27 '16 at 00:14
  • @GiantTree Why would I open a new question? What I want is to issue all commands that work on ADB from computer directly on the device. I expected there's a way to start ADB client on the device and make it connect to itself. – Tomáš Zato May 27 '16 at 00:59
  • Most devices don't ship with an adb client, so you can't connect back to yourself. I get what you try to achieve. My idea was, that what you try to achieve with arbitrary commands, might be possible without using those input commands. Remember that adbd (the ADB daemon on the device) runs with system permissions but does not carry them on to child processes, that's why you can run commands from adb but not from within a shell script. – GiantTree May 27 '16 at 01:08
  • @TomášZato As GiantTree pointed out, even if you were able to launch adb on the phone and make it connect to itself, you will not be able to access that event0 device. This is because the file is owned by root and is readable and writable only by user root and group input. Any other user can do absolutely nothing, and there is no way to circumvent the issue, but root your device. – Grimoire May 27 '16 at 09:19
  • @DeathMaskSalesman I do not understand. What is the difference between connecting to adbd from PC and from the phone itself? It's still the same process, isn't it? Please can you explain it more or give me some links? – Tomáš Zato May 27 '16 at 09:36
  • @TomášZato It's basically the same thing, but it's utterly painful to force it and useless as well. Did you try to issue the sendevent command from your computer via adb? – Grimoire May 27 '16 at 09:39
  • @DeathMaskSalesman Yeah it works perfectly well from computer. But I don't want to need the computer to create the events. I still don't understand why is it not possible to connect to phones own adb daemon service. – Tomáš Zato May 27 '16 at 09:43
  • @TomášZato Still researching it, but it appears that you won't be able to start the client part of adb without root permissions, as it couldn't bind port 5038. – Grimoire May 27 '16 at 09:49
  • @TomášZato You definitely need root. The commands to set the port for adbd to listen are setprop service.adb.tcp.port 5555, followed by a stop adbd and start adbd. Only then you'll be able to adb connect localhost:5555 and finally get your device connected to itself. – Grimoire May 27 '16 at 09:57
  • @DeathMaskSalesman And could I first set the port from computer, making a backdoor that can be used later? Thansk for all this help :) – Tomáš Zato May 27 '16 at 10:03
  • @TomášZato I didn't test this procedure from a computer. You could implement it, and tell us the outcome. – Grimoire May 27 '16 at 10:04
  • Related: Run adb command inside the terminal emulator or programmatically without root. Note: as long as you've the adb client in the device and is connected in loopback mode, you can run *any* shell command that you were able to execute using a PC and adb. – Firelord May 27 '16 at 12:48

2 Answers2

1

Adb is a two-part system, the "ADB" command that runs on your computer, and a daemon that runs on the device to receive the commands you send to it.

To run it on the device you need a version of ADB that is compiled to run on the device. There are several of these on the play store (Search "Adb Shell") on Google Play, such as Adb Shell.

There is also an "ADBLib" by Cameron Guzman on GitHub that can be compiled into an Android App and allow run Adb Shell commands from there.

SteveS
  • 111
  • 3
0

on a rooted phone you can do this by download linux deploy termius and any adb over wifi app on the play store all you have to do is set up linux deploy on Debian with root as the user name and ssh access. connect to it with termius run sudo apt-get install adb when that finishes turn on the adb over wifi app go back to termius adn type adb devices it should come up with emulator-5554 device during this prosess it may ask you to athenticate the rsa key just say yes ass well as tick the box that says always then you can run your adb commands

zoho
  • 1