Skip to content

Instantly share code, notes, and snippets.

@rostegg
Last active July 31, 2020 19:20
Show Gist options
  • Select an option

  • Save rostegg/96d73edddd4a8e9e320df630b27f6bfa to your computer and use it in GitHub Desktop.

Select an option

Save rostegg/96d73edddd4a8e9e320df630b27f6bfa to your computer and use it in GitHub Desktop.
Discover all Android devices throug network with ADB TCP debug mode enabled
#!/bin/bash
# Copy to /usr/bin and don't forget to make the script executable
# Usage: adb-auto-connect <adb_port> ("5555" if empty)
IP_TEMPLATE=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}' | grep -Eo '([0-9]*\.){3}' | grep -v '127.0.0')
NETWORK_IP_WITH_MASK="${IP_TEMPLATE}0/24"
printf "\e[32mStarting scan \e[0m${NETWORK_IP_WITH_MASK}\n"
AVAILABLE_IPS=($(nmap -sn ${NETWORK_IP_WITH_MASK} | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"))
printf '\e[32mAvailable devices in network: \e[0m\n'
printf ' %s\n' "${AVAILABLE_IPS[@]}"
ADB_PORT=$1
if [ -z "$ADB_PORT" ]
then
ADB_PORT="5555"
fi
GOOD_RESPONSE="connected to"
BAD_RESPONSE="unable"
for IP in "${AVAILABLE_IPS[@]}"
do
printf "Trying connect to \e[32m${IP}\e[0m...\n"
RESPONSE=$(adb connect ${IP}:${ADB_PORT})
if [[ $RESPONSE == *${BAD_RESPONSE}* ]]; then
printf "\e[31mCan't connect to \e[0m${IP}:${ADB_PORT}\e[31m, maybe it's not even adb device...\e[0m\n"
fi
if [[ $RESPONSE == *${GOOD_RESPONSE}* ]]; then
printf "\e[32mSuccessfully connected to \e[0m${IP}:${ADB_PORT}\n"
fi
done
adb devices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment