Helios210
Helios210

Reputation: 1

Cannot execute script in Termux that requires SU permissions

So I don't usually post, instead spending hours trawling forums looking for an answer - but I'm at a complete loss.

What I want to do:

Quite simple really, I want a script that will automatically change the MAC address on my OnePlus 5, I already have all the commands I need to do this and can easily accomplish it manually, getting a script to do it however? Oh boy...

What I have tried:

These are the commands that, when run in termux manually, give me the result I need:

ip link set dev wlan0 down

macchanger --random wlan0

ip link set dev wlan0 up

Now the reason I don't use ip link set dev wlan0 address xx:xx:xx:xx:xx:xx is because then I have to enter an address, whereas macchanger can generate this automatically. (I don't have the Unix-foo to generate a MAC address programmatically).

So I did the following, in chronological order:

1) Created a script in nano with these commands.

2) Made the script executable using chmod +x changeMAC.sh

This gives an "Access Denied" error message when executed.

3) Tried first going into SU, then executing the script, this gives a "macchanger" not found error as termux loses the environment when su is invoked. So I installed the tsu wrapper which allows you to call tsu and get su with the environment maintained, equivalent to su --preserve-environment.

However this is where I started banging my head off the desk.

If I do the following in the termux terminal:

tsu
./changeMAC.sh

The terminal just sits there, doing nothing. If I then type "exit" and hit return I get three "Access Denied" error messages.

I've tried to circumvent the error:

4) Using AutoInput to type into termux like a user but it refused to do so.

5) Executing the shell commands directly from Tasker > Run Shell but I suspect this isn't working because the macchanger package isn't installed in the scope of the tasker shell.

6) Using su --preserve-environment directly, but the same thing happens!

So to summarise my script looks like this:

\#!/data/data/com.termux/files/usr/bin/sh

sleep 1

ip link set dev wlan0 down

sleep 1

macchanger --random wlan0

sleep 1

ip link set dev wlan0 up

exit

but:

1) Won't work with standard privaleges

2) Doesn't appear to execute when tsu/su --preserve-environment is used, giving errors when the tsu instance is exited out of instead, almost asthough its waiting for the instance to end before executing...

3) Works fine when executed manually from a tsu instance.

I think I just do not know enough about the intricacies of Unix/Linux/Android to understand why this isn't working and therefore correct it.

Any help is greatly appreciated.

Best,

H.

Phone Details:

OnePlus 5, rooted with Magisk

ElementalX Kernel

xXx NoLimits Custom ROM

Edit 1:

I have tried 'sudo' from https://gitlab.com/st42/termux-sudo however this requires su, which then can't find macchanger and I'm back to square one.

Upvotes: 0

Views: 5944

Answers (1)

Antoine Draune
Antoine Draune

Reputation: 323

There is severals reason why it's not working:

  • When 'Access Denied', you just simulate an env for the context of your script, if you check /proc/PID_OF_YOUR_PROCESS/env you will see an empty env
  • If you use chroot solution like GNURootDebian, you will face chroot jail
  • Script shell with root permission on android system is wild, expect issue.
  • Prefer conditional chaining execution with '&&' or '||' over sleep chainning process 'command1;sleep(1);command2;sleep(1)'

The simpliest solution i can propose you is this:

 Create a simple Application android, and on your main activity add:

protected void      onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        String[] env = {"PATH=/su/bin:/sbin:/system/sbin:/system/bin:/su/xbin:/system/xbin:/system/xbin/"};
        DataOutputStream outputStream;
        outputStream = new DataOutputStream(Runtime.getRuntime().exec("su", env).getOutputStream());
        outputStream.writeBytes("ip link set dev wlan0 down && macchanger --random wlan0 && ip link set dev wlan0 up && exit \n");
        outputStream.flush();
        Toast.makeText(this, "Macchanger DONE", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "Macchanger Error", Toast.LENGTH_SHORT).show();
    }
}

It work on Nexus 5 Rooter, but you should definetly add a button to control the fonctionnality, and why not print the new mac wich you can find here:

  • cat /sys/class/net/NAME_INTERFACE/address

And if you want to automate it, same logic, but with Service and not Activity.

Upvotes: 1

Related Questions