Get default gateway address from shell command

edited June 3 in General

Is it possible to get the default gateway address using a shell command? I tried "ifconfig" and "ip route show", but those commands don't provide that information.

Tagged:

Comments

  • edited June 3

    If you don't mind parsing it from a larger chunk, you could do dumpsys ethernet:

    NetworkInfo: [type: Ethernet[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: a8:21:ec:3a:3d:51, failover: false, available: true, roaming: false, metered: false]
    LinkProperties: {InterfaceName: eth0 LinkAddresses: [fa62::ba14:a3ff:3a32:3a23/64,192.168.1.14/24,] Routes: [fa62::/64 -> :: eth0,192.168.1.0/24 -> 0.0.0.0 eth0,0.0.0.0/0 -> 192.168.1.1 eth0,] DnsAddresses: [8.8.8.8,8.8.4.4,82.61.22.14,91.120.132.7,]

  • Parsing the response is perfectly fine. Thanks a lot!

  • If execute "dumpsys ethernet | grep 'LinkProperties:'" command in the shell myself I get the desired output. However, if I try to execute it in a process I launch from my app I get this:

    'Permission Denial: can't dump EthernetService from pid=2836, uid=10057'

    If I try "su -c "dumpsys ethernet | grep 'LinkProperties:'"" I get an empty response.

    Is there another command I could use which doesn't require elevated permissions?

  • edited June 17

    Its interesting that "su" doesn't get the permission to do so. Maybe you can add <uses-permission android:name="android.permission.DUMP" /> to your application?

Sign In or Register to comment.