Skip to content

Instantly share code, notes, and snippets.

@belsander
Last active December 17, 2016 15:52
Show Gist options
  • Save belsander/3ae688e11c47b0dff4b692f815971268 to your computer and use it in GitHub Desktop.
Save belsander/3ae688e11c47b0dff4b692f815971268 to your computer and use it in GitHub Desktop.
Script to connect to Android device attached via the network. Reconnects if connection was lost.
#!/bin/bash
#
# adb_with_reconnect.sh: Wrapper around Android Debug Bridge which auto reconnects when network connection has to reestablished (Useful in case of unstable Android device or poor network connection. Also makes use of nmap for an analysis of the Android device, so installation of nmap is recommended
#
# Copyright (C) 2016 Sander Bel
#
# This program is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program. If not,
# see <http://www.gnu.org/licenses/>
#
#
# AUTHOR: Sander Bel
# COPYRIGHT: Copyright (C) 2016 Sander Bel
# CREDITS: Sander Bel
# LICENSE: GPLv3
# VERSION: 1.0.0
# MAINTAINER: Sander Bel
# EMAIL: sander@proapps.be
# STATUS: Production
#
#
# set -n # Uncomment to check your syntax, without execution.
# # NOTE: Do not forget to put the comment back in or
# # the shell script will not execute!
#
# GLOBALS
IP_ADDR="192.168.0.100"
ADB_CMD="null"
USAGE_CMD="usage: $0 "
OPTION_TAB=$(printf ' %.0s' $(eval "echo {1.."$((${#USAGE_CMD}))"}"))
USAGE="$USAGE_CMD[-h] [-l] [-v]
$OPTION_TAB[-i IP_ADDRESS]
$OPTION_TAB[-a ADB_CMD]
Wrapper around Android Debug Bridge which auto reconnects when network connection has to reestablished (Useful in case of unstable Android device or poor network connection. Also makes use of nmap for an analysis of the Android device, so installation of nmap is recommended
optional arguments:
-h, --help show this help message and exit
-l, --license show license information and exit
-v, --version show version information and exit
-i IP_ADDRESS, --ip_address IP_ADDRESS
IP address of the Android device to connect to (default: $IP_ADDR)
-a ADB_CMD, --adb_cmd
Location of the adb executable (located in <SDK_FOLDER>/platform-tools/adb
$0 Copyright (C) 2016 Sander Bel
This program comes with ABSOLUTELY NO WARRANTY; for details type '$0 --license'.
This is free software, and you are welcome to redistribute it under certain conditions; type '$0 --license' for details."
LICENSE="For more information regarding the GPLv3 license, see <https://blog.b-e-l.be/gnu-gplv3-license>"
VERSION="$0 v1.0.0"
while :
do
case "$1" in
-h | --help)
echo "$USAGE"
exit 0
;;
-l | --license)
echo "$LICENSE"
exit 0
;;
-v | --version)
echo "$VERSION"
exit 0
;;
-i | --ip_address)
IP_ADDR=$2
shift 2
;;
-a | --adb_cmd)
ADB_CMD=$2
shift 2
;;
--) # End of all options
shift
break
;;
-*)
echo "$0: error: unrecognized arguments: $1 $2" >&2
exit 1
;;
*) # No more options
break
;;
esac
done
ADB_DEVICE=""
if [ $(command -v nmap 2>/dev/null) ];
then
NMAP_CMD="nmap -Pn $IP_ADDR -p 5553-5560 "
else
NMAP_CMD=""
fi
while true;
do
echo -n "$(date) - Checking connected devices"
ADB_DEVICE=$(./adb devices | grep $IP_ADDR | cut -f 1)
if [ -z "$ADB_DEVICE" ];
then
echo ""
$NMAP_CMD | sed -e '1,2d' | sed -e '$d' | sed -e '$d'
echo -n "$(date) - Connecting to $IP_ADDR; "
$ADB_CMD connect $IP_ADDR
else
echo ": $ADB_DEVICE"
fi
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment