2019-10-02 19:03:50 +02:00
#!/bin/bash
# Default variables
GIT_FOLDER = "/tmp/pwnagotchi"
GIT_URL = "https://github.com/evilsocket/pwnagotchi/"
VERSION = "master"
SUPPORTED_RESTART_MODES = ( 'auto' 'manual' )
MODE = "auto"
BACKUPCONFIG = 0
RESTORECONFIG = 0
# Functions
function usage( ) {
cat <<EOF
usage: $0 [ OPTIONS]
2019-10-02 14:05:22 -05:00
Note: This should be run from the pwnagotchi itself!
2019-10-02 19:03:50 +02:00
Options:
-v # Version to update to, can be a branch or commit. (default: master)
-u # Url to clone from. (default: https://github.com/evilsocket/pwnagotchi)
-m # Mode to restart to. (Supported: ${SUPPORTED_RESTART_MODES[*]}; default: auto)
2019-10-02 14:05:22 -05:00
-b # Backup the current pwnagotchi config, then overwrite with defaults.
-r # Restore the current pwnagotchi config after upgrade. (-b will be enabled.)
2019-10-02 19:03:50 +02:00
-h # Shows this help.
EOF
exit 0
}
function test_root( ) {
if ! [ $( id -u) = 0 ] ; then
echo "[!] This script must be run as root."
exit 1
fi
}
function test_github( ) {
wget -q --spider $GIT_URL
if [ $? -ne 0 ] ; then
2019-10-02 14:05:22 -05:00
echo "[!] Cannot reach github. This script requires internet access and DNS resolution, ensure connection sharing is working and valid DNS server in /etc/resolv.conf."
2019-10-02 19:03:50 +02:00
exit 2
fi
}
2019-10-02 14:05:22 -05:00
while getopts "v:u:m:brh" o; do
2019-10-02 19:03:50 +02:00
case " ${ o } " in
v)
VERSION = " ${ OPTARG } "
; ;
u)
GIT_URL = " ${ OPTARG } "
; ;
b)
BACKUPCONFIG = 1
; ;
r)
2019-10-02 14:05:22 -05:00
BACKUPCONFIG = 1
2019-10-02 19:03:50 +02:00
RESTORECONFIG = 1
2019-10-02 14:05:22 -05:00
; ;
2019-10-02 19:03:50 +02:00
h)
usage
; ;
2019-10-02 14:05:22 -05:00
m)
if [ [ " ${ SUPPORTED_RESTART_MODES [*] } " = ~ ${ OPTARG } ] ] ; then
MODE = " ${ OPTARG } "
else
usage
fi
; ;
2019-10-02 19:03:50 +02:00
*)
usage
; ;
esac
done
shift $(( OPTIND-1))
2019-10-02 14:05:22 -05:00
echo "[+] Checking prerequisites."
test_root
test_github
2019-10-02 19:03:50 +02:00
# clean up old files, clone master, set checkout to commit if needed.
echo " [+] Cloning to $GIT_FOLDER ... "
2019-10-02 14:05:22 -05:00
#rm $GIT_FOLDER -rf
2019-10-02 19:03:50 +02:00
git clone $GIT_URL $GIT_FOLDER -q
cd $GIT_FOLDER
if [ $VERSION != "master" ] ; then
git checkout $VERSION -q
fi
echo "[+] Updating..."
if [ $BACKUPCONFIG -eq 1 ] ; then
echo "[+] Creating backup of config.yml"
mv /root/pwnagotchi/config.yml /root/config.yml.bak -f
2019-10-02 14:05:22 -05:00
echo "[+] Creating backup of host files"
mv /etc/hosts /etc/hosts.bak -f
mv /etc/hostname /etc/hostname.bak -f
mv /etc/motd /etc/motd.bak -f
2019-10-02 19:03:50 +02:00
fi
2019-10-02 14:05:22 -05:00
echo " [+] Installing $( git log -1 --format= "%h" ) "
2019-10-02 19:03:50 +02:00
rm /root/pwnagotchi -rf # ensures old files are removed
rsync -aPq $GIT_FOLDER /sdcard/boot/* /boot/
rsync -aPq $GIT_FOLDER /sdcard/rootfs/* /
cd /tmp
rm $GIT_FOLDER -rf
2019-10-02 14:05:22 -05:00
2019-10-02 19:03:50 +02:00
if [ $RESTORECONFIG -eq 1 ] ; then
echo "[+] Restoring backup of config.yml"
mv /root/config.yml.bak /root/pwnagotchi/config.yml -f
2019-10-02 14:05:22 -05:00
echo "[+] Restoring backup of host files"
mv /etc/hosts.bak /etc/hosts -f
mv /etc/hostname.bak /etc/hostname -f
mv /etc/motd.bak /etc/motd -f
2019-10-02 19:03:50 +02:00
fi
echo " [+] Restarting pwnagotchi in $MODE mode. $( screen -X -S pwnagotchi quit) "
if [ $MODE = = "auto" ] ; then
sudo -H -u root /usr/bin/screen -dmS pwnagotchi -c /root/pwnagotchi/data/screenrc.auto
elif [ $MODE = = "manual" ] ; then
sudo -H -u root /usr/bin/screen -dmS pwnagotchi -c /root/pwnagotchi/data/screenrc.manual
fi
2019-10-02 14:05:22 -05:00
echo "[+] Finished"