updated arguments to getopts. Added -m for restart mode. Moved prerequisites checks.

This commit is contained in:
Justin-p 2019-10-02 10:40:16 +02:00
parent cd5be9c4cd
commit bc2bca79b5

View File

@ -1,82 +1,111 @@
#!/bin/bash #!/bin/bash
# Default variables # Default variables
git_folder="/tmp/pwnagotchi" GIT_FOLDER="/tmp/pwnagotchi"
git_url="https://github.com/evilsocket/pwnagotchi/" GIT_URL="https://github.com/evilsocket/pwnagotchi/"
version="master" VERSION="master"
backupconfig=0 SUPPORTED_RESTART_MODES=( 'auto' 'manual' )
restoreconfig=0 MODE="auto"
BACKUPCONFIG=0
RESTORECONFIG=0
# Functions # Functions
function usage() { function usage() {
cat <<EOF cat <<EOF
usage: $0 [OPTIONS] usage: $0 [OPTIONS]
Options: Options:
-v, --version # Version to update to, can be a branch or commit. (default: master) -v # Version to update to, can be a branch or commit. (default: master)
-u, --url # Url to clone from. (default: https://github.com/evilsocket/pwnagotchi) -u # Url to clone from. (default: https://github.com/evilsocket/pwnagotchi)
-bc, --backupconfig # Backup the current pwnagotchi config. -m # Mode to restart to. (Supported: ${SUPPORTED_RESTART_MODES[*]}; default: auto)
-rc, --restoreconfig # Restore the current pwnagotchi config. -bc will be enabled. -b # Backup the current pwnagotchi config.
-h, --help # Shows this help. -r # Restore the current pwnagotchi config. (-b will be enabled.)
-h # Shows this help.
EOF EOF
exit 0 exit 0
} }
function test_root() { function test_root() {
if ! [ $(id -u) = 0 ]; then if ! [ $(id -u) = 0 ]; then
echo "[!] This script must be run as root." echo "[!] This script must be run as root."
exit 1 exit 1
fi fi
} }
function test_github() { function test_github() {
wget -q --spider $git_url wget -q --spider $GIT_URL
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "[!] Cannot reach github. This script requires internet access, ensure connection sharing is working." echo "[!] Cannot reach github. This script requires internet access, ensure connection sharing is working."
exit 2 exit 2
fi fi
} }
# Commandline arguments
while [[ "$#" -gt 0 ]]; do case $1 in
-v|--version) version="$2"; shift;;
-u|--url) git_url="$2"; shift;;
-bc|--backupconfig) backupconfig=1; shift;;
-rc|--restoreconfig) backupconfig=1 restoreconfig=1; shift;;
-h|--help) usage;;
*) echo "Unknown parameter passed: $1"; exit 3;;
esac; shift; done
echo "[+] Checking prerequisites." echo "[+] Checking prerequisites."
test_root test_root
test_github test_github
while getopts ":v:u:m:b:r:h" o; do
case "${o}" in
v)
VERSION="${OPTARG}"
;;
u)
GIT_URL="${OPTARG}"
;;
m)
if [[ "${SUPPORTED_RESTART_MODES[*]}" =~ ${OPTARG} ]]; then
MODE="${OPTARG}"
else
usage
fi
;;
b)
BACKUPCONFIG=1
;;
r)
BACKUPCONFIG=1
RESTORECONFIG=1
;;
h)
usage
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# clean up old files, clone master, set checkout to commit if needed. # clean up old files, clone master, set checkout to commit if needed.
echo "[+] Cloning to $git_folder..." echo "[+] Cloning to $GIT_FOLDER..."
rm $git_folder -rf rm $GIT_FOLDER -rf
git clone $git_url $git_folder -q git clone $GIT_URL $GIT_FOLDER -q
cd $git_folder cd $GIT_FOLDER
if [ $version != "master" ]; then if [ $VERSION != "master" ]; then
git checkout $version -q git checkout $VERSION -q
fi fi
echo "[+] Installing $(git log -1 --format="%h")" echo "[+] Installing $(git log -1 --format="%h")"
echo "[+] Updating..." echo "[+] Updating..."
if [ $backupconfig -eq 1 ]; then if [ $BACKUPCONFIG -eq 1 ]; then
echo "[+] Creating backup of config.yml" echo "[+] Creating backup of config.yml"
mv /root/pwnagotchi/config.yml /root/config.yml.bak -f mv /root/pwnagotchi/config.yml /root/config.yml.bak -f
fi fi
rm /root/pwnagotchi -rf # ensures old files are removed rm /root/pwnagotchi -rf # ensures old files are removed
rsync -aPq $git_folder/sdcard/boot/* /boot/ rsync -aPq $GIT_FOLDER/sdcard/boot/* /boot/
rsync -aPq $git_folder/sdcard/rootfs/* / rsync -aPq $GIT_FOLDER/sdcard/rootfs/* /
cd /tmp cd /tmp
rm $git_folder -rf rm $GIT_FOLDER -rf
if [ $restoreconfig -eq 1 ]; then if [ $RESTORECONFIG -eq 1 ]; then
echo "[+] Restoring backup of config.yml" echo "[+] Restoring backup of config.yml"
mv /root/config.yml.bak /root/pwnagotchi/config.yml -f mv /root/config.yml.bak /root/pwnagotchi/config.yml -f
fi fi
echo "[+] Restarting pwnagotchi in auto mode. $( screen -X -S pwnagotchi quit)" echo "[+] Restarting pwnagotchi in $MODE mode. $( screen -X -S pwnagotchi quit)"
sudo -H -u root /usr/bin/screen -dmS pwnagotchi -c /root/pwnagotchi/data/screenrc.auto 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
echo "[+] Finished" echo "[+] Finished"