Environment Variables
Over the past few years, my Android hotspot was always at the IP address
192.168.43.1
and apparently it was the same for everyone. It never changed, so I wrote my scripts accordingly. Now, however, I see a new IP address every few days, which sucks. It meant a rewrite of al existing code that connects my computer to the phone. In the process, I learned a few things or got to practice my skills.
It’s possible that they did this so that whoever gains access to a computer that was set up to access an Android phone, won’t be able to access the phone without more information, but I am not sure.
So, first a function that gets the IP address from within the phone
get_ip_address(){
ip a | grep 192.168. | awk '{print $2}' | sed 's/\/.*//'
}
On the computer, a function that tries to connect to the phone using the IP address if it exists, if not, prompts you to input it
connect_to_phone() {
echo "connecting to $IP"
ssh -p 8022 $IP
if [ $? -ne 0 ]; then
echo "Connection failed. Get the IP address from your phone."
read -p "Answer 'yes' if you want to set it up > "
if [ "$REPLY" == "yes" ]; then
read -p "what's the IP address? > "
export IP=$REPLY
fi
fi
}
The new thing to learn for me is that emacs can use these environment variables with the command (getenv)
. Then I can then construct command strings using (concat)
, and execute them.