(Guide) Setting up and using adb commands with Termux

Endercraft

Moderator (& bug finder :D)
This thread is for those who need to run ADB commands on their devices locally and automatically, as well for those whose devices don't support ADB hack to do certains operations - for example enabling/disabling mobile data on Xiaomi.

Here we will be using Termux to run these commands.

Big thanks to @tanutanu (who made the original method) and @Dimlos (from which I learned a lot about how to use Termux and adb with Termux).

  1. Install Termux (https://f-droid.org/en/packages/com.termux) and Termux:Tasker (https://f-droid.org/en/packages/com.termux.tasker).
  2. Open Termux. Run the following commands :
    yes | apt update yes | apt upgrade yes | pkg install vim yes | pkg install nmap yes | pkg install android-tools mkdir -p ~/.termux/tasker cd ~/.termux/tasker
  3. Open developer options (google how to do it on your device if you don't know how).
  4. Enable USB debugging, install apps via USB and USB debugging (secure settings).
    From this point, the method differs a bit according to your Android version.
  5. If you are on Android 10 or below (for those with Android 11+ you can completely automate this process if you are connected to wifi, skip to here however this is less complicated).
  6. Connect your device to a computer via USB.
  7. Download ADB on your computer from these sources according to the model :
    Windows - https://dl.google.com/android/repositor ... indows.zip
    Linux - https://dl.google.com/android/repositor ... -linux.zip
    Mac - https://dl.google.com/android/repositor ... darwin.zip
  8. In the folder containing the file adb.exe, open a new command prompt (make sure it is opened in the directory where adb.exe is located else it won't work !).
  9. Type the command adb devices. If you are on Mac or Linux, add ./ before the command else it won't work.
  10. On your device, you should see a prompt asking you to confirm if you want to allow the connection. Accept this prompt.
  11. On your computer, type adb tcpip 5555 (again, add ./ before the command if on Mac or Linux). This needs to be done every time you reboot your device.
    [Marker for those who wish to automate everything on Android 11+] [if you are on Android 10 or below, skip to here]
  12. Open Wireless debugging in developer options. Enable it and enable split screen on your device to have both the settings app and Termux open.
  13. In wireless debugging, tap on Pair device with pairing code. You will see a 6 digit pairing code, and below it an IP address and port (you only need the port which is located after the colon, for example 192.168.0.101:39204)
  14. In Termux, type adb pair localhost:port. Replace "port" you the port you obtained from the pairing prompt. You should be prompted to enter the pairing code on Termux. Type this code and press enter (allow connect prompt if asked to).
    This is what your window should look like :
    Screenshot_2023-10-14-23-13-20-095_com.termux.jpg
  15. Run adb tcpip 5555 && adb kill-server to setup everything.
  16. Download the nmap.sh file from here : https://www.mediafire.com/file/03869ly4rq9h7gl/nmap.sh/file
  17. Once that's done, open Termux and run the command termux-setup-storage. Allow Termux to access all files when prompted.
  18. Run the command path=$(find /sdcard/ -name nmap.sh 2>/dev/null) && cp $path ~/.termux/tasker && chmod +x ~/.termux/tasker/nmap.sh. This will automatically copy the file to the desired location (instead of installing another app and doing it manually).
  19. In MacroDroid app info (google how to open on your device), enable the permission Run commands in Termux environment.
  20. Exclude Termux and Termux:Tasker from battery optimizations and allow them to autostart if necessary.
  21. Import the attached macro in MacroDroid. Do a test run by testing the empty trigger or macro actions (make sure you have performed ADB hack before else it will not work correctly) : if a popup message saying success (it's not instant, wait a few minutes at most) apeears then you have set up everything correctly. You should check in the Termux notification : it should contain "1 task".
    [Marker if you have done adb tcpip 5555 from your computer]
  22. If everything works correctly, you have the choice : you can either run commands from Termux using Termux:Tasker, in which case you can run every adb commands or you can use an app only able to run shell commands called ADB Shell [Tasker Plugin] : https://play.google.com/store/apps/details?id=com.ADBPlugin ; there is a good chance the Play Store will tell you the app is too old for your device, however that's a lie. You can install directly from https://m.apkpure.com/adb-shell-tasker-plugin/com.ADBPlugin
  23. If you are going with the ADB Shell tasker plugin option : using a plugin action in MacroDroid, you can select that app to run ADB shell commands. In the IP address field, enter localhost ; in the port field, enter 5555 ; in the ADB Shell Command field, enter your command (if it's adb shell pm list packages for example, you will have to remove adb shell from the command) ; in the ADB timeout in ms, you should use 50 however you are free to use pretty much any number you want. As you can see there is also an Emulate Ctrl+C toggle : if commands aren't working for you, try to enable this.
    Note that you can open this plugin app directly to test the commands and your setup easily.

  24. If you are going with Termux, it's a bit tricky but definitely possible even for less advanced users.
  25. Open Termux. Run this command which will setup everything for you : cd ~/.termux/tasker && echo '$1 $2 $3 $4 $5 $6 $7 $8 $9' > adb_commands.sh && chmod +x adb_commands.sh && adb kill-server && adb start-server && sleep 2 && adb devices
    As a result, you should see the device "emulator-5554" in the device list. If that's not the case it means something went wrong. The command also created a script called adb_commands.sh which you can call from MacroDroid as a plugin action. You can have a command with a maximum of 9 arguments. Here's an example :Screenshot_2023-10-14-23-08-59-973_com.termux.tasker-edit.jpg

Hopefully this thread is good enough for most to understand.
If you haven't done this before, it would be great to have some feedback on how well that works. It can be simplified further but at this point you could just make a macro that does this for you.
 

Attachments

  • adb_tcpip_5555.macro
    13.1 KB · Views: 110
Last edited:

sampleuserhere

Active member
If we have tudo installed in Termux, we could save the scripts inside Macrodroid variable and run them directly with it.

tudo installation guide can be found here. Credit goes to @Dimlos and @agnostic-apollo.


I attached a sample macro to pair termux with notification prompt and authorize wifi debug after boot then run adb connect & adb tcpip.

Feel free to edit the macro and publish it later. I'll edit my post later too if needed.
 

Attachments

  • adbPair_&_adbConnect.macro
    46.4 KB · Views: 58

Endercraft

Moderator (& bug finder :D)
If we have tudo installed in Termux, we could save the scripts inside Macrodroid variable and run them directly with it.

tudo installation guide can be found here. Credit goes to @Dimlos and @agnostic-apollo.


I attached a sample macro to pair termux with notification prompt and authorize wifi debug after boot then run adb connect & adb tcpip.

Feel free to edit the macro and publish it later. I'll edit my post later too if needed.
I planned to use Tudo at first but it didn't work for me so I used the basic script instead. If I can get it to work someday I'll edit the thread.
 

Dimlos

Well-known member
@sampleuserhere
I attempted the macro. The pairing process was effortless and highly expedient.
Although I was aware of the necessary operations, it was challenging to ascertain the success of the setup.
Novices may benefit from specifying the current process and its outcome.
This is subjective, so you can disregard it if deemed unnecessary.
 

Endercraft

Moderator (& bug finder :D)
I think I'll have time tomorrow afternoon to test the macro again because last time I didn't have Wi-Fi so it couldn't work.
I'll edit if the macro successfully runs the script.
 

Dimlos

Well-known member
There is no need to force yourself to use tudo since the setup process will take a long time.
@tanutanu published a macro that writes variables as text to /.termux/tasker.
This method is a little difficult to use because it requires resetting appropriate permissions, but I think there is room for consideration.
 

lyon

Member
This topic is interesting, maybe I have an easier solution (at least from Android 11 which has wireless debugging) which is to use shizuku through the terminal (in this case termux) for this A text editor would be needed, I recommend "material files" as it is simpler and available on Github. in shizuku itself it says that it is necessary, basically you click to choose the location of the shizuku folder, and you will just open the one named "rish" and edit the two lines that are like "PKG" and change them to "com.termux" and save.

Em seguida, abra o termux e execute "pwd" e ele mostrará a pasta em que está, e isso é necessário para facilitar a cópia do caminhoThen, open termux and run "pwd" and it will show the folder it is in, and this is necessary to make it easier to copy the path, then if necessary run "termux-setup-storage" and it will ask for permission to access the storage, accept and paste/type the path of the shizuku folder with the two files.
move both files to the path shown by typing "pwd" will appear pop-up shizuku allowed authorization, accept "all the time" and it will be working, just type "sh rish" and "enter" and you can now execute the macrodroid adb hack commands. This also works on Android 10, just start shizuku via adb on the computer and do the same process, unfortunately on the Galaxy A01 Core it is bad to use as it forces shizuku to stop in few minutes after it starts up, so I don't think it's relevant, but on other devices it should work for longer.

To help visually, I'll leave a link to a video on YouTube (but because he's already done it before, some authorizations are not shown and he only edited the file in one line, I recommend doing it in both texts "PKG" to avoid bugs)

 

Endercraft

Moderator (& bug finder :D)
You can do that too however it only lets you run shell commands and not the full adb which might already be good enough for some users.
 

sampleuserhere

Active member
Last week, someone in r/tasker posted a way to get the port using python with zeroconf.

https://www.reddit.com/r/tasker/comments/18m11gk
Code:
pkg install python
pip install zeroconf

Most of the time, it took about less than 1s to get the port. However it comes with the cost of having to install python & zeroconf, which both takes around 600-700mb worth of space.

Here's the python script.

Code:
from zeroconf import Zeroconf, ServiceBrowser
import threading

TYPE = "_adb-tls-connect._tcp.local."

class MyListener:
    def __init__(self, zeroconf, event):
        self.found_service = False
        self.zeroconf = zeroconf
        self.exit_event = event

    def remove_service(self, zeroconf, type, name):
        pass

    def update_service(self, zeroconf, type, name):
        pass

    def add_service(self, zeroconf, type, name):
        if not self.found_service and name.startswith("adb-"):
            info = zeroconf.get_service_info(type, name)
            port = info.port
            print(port)
            self.found_service = True
            self.zeroconf.close()
            self.exit_event.set() # Set the event to signal the main loop to exit

def main():
    zeroconf = Zeroconf()
    exit_event = threading.Event()
    listener = MyListener(zeroconf, exit_event)
    browser = ServiceBrowser(zeroconf, TYPE, listener)

    # Wait for the event to be set (service found)
    exit_event.wait()

if __name__ == '__main__':
    main()

We could execute the script like this with bash script. ( The script above has to be escaped first before manually set into a variable. )

Code:
script='the script'
port=`python -c "$script"`
echo "$port"
 

Dimlos

Well-known member
It is good to see new methods being devised.
If this method works without problems, it will be beneficial to the users since nmap is not available for Samsung devices.
 

Endercraft

Moderator (& bug finder :D)
If you really don't want external apps you can also use MacroDroid's HTTP Request but it's painfully slow and sometimes fails.
 

sampleuserhere

Active member
It is good to see new methods being devised.
If this method works without problems, it will be beneficial to the users since nmap is not available for Samsung devices.

Probably it works, afaik this zeroconf seems to intercept the information Android broadcasts over mDNS/DNS-DS, similar to NsdManager API.
 
Top