Skip to content

Instantly share code, notes, and snippets.

@nottux
Last active January 16, 2021 19:12
Show Gist options
  • Save nottux/2632057329698845fe37752863b70040 to your computer and use it in GitHub Desktop.
Save nottux/2632057329698845fe37752863b70040 to your computer and use it in GitHub Desktop.
a script to auto connect my android phone
#!/bin/bash
a=android-aa8b17a6b252b32
b=/tmp/casper_ip.txt
c="192.168.43.1"
d=$(route -n | awk '{if($4=="UG")print $2}')
e=$(ip -o -f inet addr show | grep -Po "/[[:digit:]]{1,2}(?=.*scope\sglobal)")
if [ "$1" = "$z" ]
then :
else if [ -f "$b" ]
then adb disconnect $(cat "$b") && rm "$b"
exit
fi
exit
fi
if [ -f "$b" ]
then adb disconnect $(cat "$b")
fi
if [ "$d" = "$c" ]
then ping -q $c & adb connect $c && echo $c >"$b"
kill $(jobs -p 1)
else f=$(until nmap -sn $d$e|grep -Po "(?<=$a..).*(?=\))";do :;done)
ping -q $f & adb connect $f && echo $f >"$b"
kill $(jobs -p 1)
fi
## usage:
## without variables it will try to find and connect to your device.
## with any variable it will disconnect device if its connected and remove the temporary file
## explanation:
## a is the device name in nmap report
## b file will store ip address of our device
## c is the special case where you might connect to your device when the device itself is hotspot, it is the gateaway ip in that situation (you have to know that yourself). can be left empty
## d is the gateaway of what you're connected to
## e is the subnet mask
## f is the found ip address of your device
@nottux
Copy link
Author

nottux commented Jan 16, 2021

I just realized that there is a logical error in this script.
In theory we don't need c variable and problems that come with it. But that's for tomorrow

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