I tried looking several posts and questions, but they all give solution for getting the Subnet Mask and none has provided method for setting the subnet mask via programming. Any solutions?
Asked
Active
Viewed 1,361 times
0
-
Hope [This Link](http://stackoverflow.com/questions/28283915/android-detect-local-ip-and-subnet-mask-for-wifi-both-while-tethering-and-conn)http://stackoverflow.com/questions/28283915/android-detect-local-ip-and-subnet-mask-for-wifi-both-while-tethering-and-conn will help you – chotemotelog Dec 14 '15 at 08:57
-
Its for getting only :D – Mike Walker Dec 14 '15 at 09:14
-
Try http://stackoverflow.com/questions/4106502/set-android-ip-dns-gateway-setting-programatically (not sure if that still works or not) – zapl Dec 14 '15 at 09:27
1 Answers
0
I assume you want to set the network interfaces subnet mask for your device. This is not possible for security reasons. Android does not allow apps to change the network configuration to prevent them from malicious behaviour.
When root access is available, one might invoke shell commands from the app. To set the IP and subnet mask of a specified interface use:
ip addr add <ip>/<mask-bits> dev <interface>
example: ip addr add 192.168.3.162/24 dev eth1
to set eth1 IP to 192.168.3.162 with 255.255.255.0 as the subnet mask.

Jonas Köritz
- 2,606
- 21
- 33
-
-
You will need root access on your phone to invoke the shell commands neccessary. For Example `ip addr add 192.168.3.162/24 dev eth1` to set the IP and subnet mask for the eth1 interface. – Jonas Köritz Dec 15 '15 at 09:06
-
-
-
Here is what I am getting http://jpst.it/DZ7S by running such command. don't know what to do now :| And you didn't provide to add custom subnet mask. like 255.255.248.0 in my case – Mike Walker Dec 15 '15 at 11:47