Get default gateway address from shell command

edited June 2019 in Supported devices

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.



  • edited June 2019

    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,,] Routes: [fa62::/64 -> :: eth0, -> eth0, -> eth0,] DnsAddresses: [,,,,]

  • 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 2019

    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.