Skip to content

Instantly share code, notes, and snippets.

@stormzhang
Created August 28, 2014 00:52
Show Gist options
  • Save stormzhang/6fa157ceb7980a25fbf0 to your computer and use it in GitHub Desktop.
Save stormzhang/6fa157ceb7980a25fbf0 to your computer and use it in GitHub Desktop.
shell script for adb wifi
#!/bin/bash
#Modify this with your IP range
MY_IP_RANGE="192\.168\.1"
#You usually wouldn't have to modify this
PORT_BASE=5555
#List the devices on the screen for your viewing pleasure
adb devices
echo
#Find USB devices only (no emulators, genymotion or connected devices
declare -a deviceArray=(`adb devices -l | grep -v emulator | grep -v vbox | grep -v "${MY_IP_RANGE}" | grep " device " | awk '{print $1}'`)
echo "found ${#deviceArray[@]} device(s)"
echo
for index in ${!deviceArray[*]}
do
echo "finding IP address for device ${deviceArray[index]}"
IP_ADDRESS=$(adb -s ${deviceArray[index]} shell ifconfig wlan0 | awk '{print $3}')
echo "IP address found : $IP_ADDRESS "
echo "Connecting..."
adb -s ${deviceArray[index]} tcpip $(($PORT_BASE + $index))
adb -s ${deviceArray[index]} connect "$IP_ADDRESS:$(($PORT_BASE + $index))"
echo
echo
done
adb devices -l
#exit
@jamescfli
Copy link

This does not work for Moto G either.

The last two lines of the output show unauthorized:

...
List of devices attached 
192.168.6.110:5555     unauthorized

@jxllh123456
Copy link

finding IP address for device 47Q6R16328002931
IP address found : 172.18.214.94
Connecting...

@jxllh123456
Copy link

right ?

@meicuihui
Copy link

good

@akidee
Copy link

akidee commented Aug 18, 2017

I updated your script to consider netcfg, ifconfig and currently connected ports: https://gist.github.com/akidee/04ade213c92f087b06920698190f71b5

@jpwiedekopf
Copy link

Having reached this gist from Google, I adapted it for my needs.
In particular, the command in line 22 to find the IP failed for me on Android 10 due to a permission error. I suspect something changed that made ifconfig require elevated rights. With this solution from StackOverflow, I was able to get the script running by changing that line to this:

IP_ADDRESS=$(adb -s ${deviceArray[index]} shell "ip addr show wlan0 | grep -e wlan0$ | cut -d\" \" -f 6 | cut -d/ -f 1")

Also, I'd like to thank you for introducing me to the declare/array syntax, which I had never used before!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment