Skip to content

Instantly share code, notes, and snippets.

@nosix nosix/pidep
Last active Dec 16, 2016

Embed
What would you like to do?
bash command to deploy and run python on remote Raspberry Pi.
#!/bin/bash
CMD=`basename $0`
usage() {
echo "Usage: $CMD [-r script] [-d dst] [-s src]" 1>&2
}
COMMAND=
DST=
SRC=
# Process options
while getopts r:d:s: OPT
do
case $OPT in
"r" ) SCRIPT=$OPTARG ;;
"d" ) DST=$OPTARG ;;
"s" ) SRC=$OPTARG ;;
* ) usage; exit 1 ;;
esac
done
# Drop options
shift `expr $OPTIND - 1`
# Check arguments
if [ -z "$SCRIPT" ] && [ -z "$SRC" ] ; then
usage; exit 1
fi
: ${PI_USER:='pi'}
: ${PI_HOST:='raspberrypi.local'}
: ${PI_HOME:='~/pyhome'}
: ${PI_ENV:='~/pyenv/python3'}
if [[ "$DST" =~ ^/ ]] || [ -z "$DST" ] ; then
dst="$DST"
else
dst="/$DST"
fi
SCP_RESULT=0
if [ -n "$SRC" ]; then
scp -r $SRC $PI_USER@$PI_HOST:$PI_HOME$dst
SCP_RESULT=$?
fi
if [ $SCP_RESULT -eq 0 ] && [ -n "$SCRIPT" ] ; then
ssh -t -t $PI_USER@$PI_HOST \
"source $PI_ENV/bin/activate; cd $PI_HOME; python $SCRIPT"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.