1

I am pretty sure it's possible as I successfully got same from termux Android app where I installed nmap and ran following commands:

  • Run ifconfig first and get the device IP (xxx.xxx.xxx.x)
  • Run nmap -sn xxx.xxx.xxx.x/24

Note: the device was not rooted and it even works in airplane mode.

Testing device: Android version 11, Samsung Galaxy m30s

I tried official Android docs for usb overview (usb manager, usbdevicce, etc.) and even running command line inside my app but wasn't successful (however I was able to do ssh once I know the ip address through command line).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
singhV
  • 157
  • 9
  • Do you want the ip of the android device? – Jorge Alejandro Puñales May 16 '21 at 18:23
  • Nope, i want the ip of the device connected with my android device through usb connection – singhV May 19 '21 at 09:22
  • Do you have to get the IP over USB? What are the circumstances? There is probably an alternative way to get the IP for the device you want. In order to get the IP over USB, not only does the USB interface have to support it, but the device you are connected to has to support it, which seems unlikely. – TallChuck May 20 '21 at 05:16
  • Yes, they both support. I tested it connecting my device to a normal system and was able to retrieve the ip through nmap command even when both my Linux system and the device were in airplane mode. Also i was able to achieve device ip through my android device (even in airplane mode) through termux app which gives us cli where i ran same nmap command. Only difference is i am not able to do it through my app. If termux is able to achieve it without rooting my android device then there must definetly be a solution. – singhV May 20 '21 at 20:36

2 Answers2

-1

You can get the IP address of the device.

Process p = Runtime.getRuntime().exec("/system/bin/ip address");

This will give you lots of information including loopback,ip4,ip6, broadcast, etc

You can use additional switches to further modify it according to your requirement.

to get all the available switches

open terminal/command prompt Connect your Android Device

Type - ADB devices You should be able to see your device.

Then type ADB shell and press enter

then type /system/bin/ip --help

this will show all the switches for this IP utility.

  • 1
    He wants the IP of the connected USB device, not the android device – spartygw May 20 '21 at 13:47
  • Yes @spartygw is right, I don't need IP of my android device. I want the IP of device which is connected to my android device through usb connection. – singhV May 20 '21 at 20:32
  • @singhV - Which device you are connecting through usb? – Developer_vaibhav May 21 '21 at 06:19
  • I simply connected my laptop with the android device. You may be thinking why to go for usb connection but actually i want it to work even in airplane mode. – singhV May 21 '21 at 12:37
-1

You can sent a request to a httpbin server, and obtain the answer from the resonse.

e.g.

Request URL

https://httpbin.org/ip

Response body

{
    "origin": "104.132.33.92"
}
Fung
  • 961
  • 1
  • 8
  • 17
  • this will give the ip of android device not the device connected with it. Please read the question again. – singhV May 21 '21 at 12:36