Skip to content

Instantly share code, notes, and snippets.

@allsey87
Created April 23, 2017 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allsey87/2f741f2d9a5942b851791e9dcc545761 to your computer and use it in GitHub Desktop.
Save allsey87/2f741f2d9a5942b851791e9dcc545761 to your computer and use it in GitHub Desktop.
Bash script for controlling the DuoVero-based BeBots
#!/bin/bash
# required programs: gnu parallel, sshpass
OPENWRT_IP='192.168.1.1'
OPENWRT_PASS='amadeus0247'
OPENWRT_USER='root'
OPENWRT_GET_LEASES='cat /tmp/dhcp.leases'
BEBOT_MAC_START='00:19:88'
BEBOT_CONN_TEST_ATTEMPTS=10
printf "Searching for robots: "
# An array for the bebot IP addresses
declare -a BEBOT_IP_ADDRESSES
# An array denoting which addresses have responded
declare -a BEBOT_IP_RESPONSIVE_ADDRESSES
# loop over the dhcp entries on the router adding the bebots to list
while read -r LEASE_TIME MAC_ADDRESS IP_ADDRESS HOST_NAME HOST_UID
do
if test "${MAC_ADDRESS#*$BEBOT_MAC_START}" != "$MAC_ADDRESS"
then
BEBOT_IP_ADDRESSES+=($IP_ADDRESS)
fi
done <<< "$(sshpass -p ${OPENWRT_PASS} ssh ${OPENWRT_USER}@${OPENWRT_IP} ${OPENWRT_GET_LEASES})"
# print out IP addresses of detected robots
for INDEX in "${!BEBOT_IP_ADDRESSES[@]}"
do
printf "${BEBOT_IP_ADDRESSES[INDEX]} "
done
printf "\n"
# check if the bebots are responding
printf "Connecting to robots: "
BEBOT_RESPONSE_LOG=$(mktemp)
for ATTEMPT in $(seq 1 $BEBOT_CONN_TEST_ATTEMPTS)
do
printf '%s\n' "${BEBOT_IP_ADDRESSES[@]}" | parallel --resume-failed --joblog $BEBOT_RESPONSE_LOG "ping -c1 -W2 {} &> /dev/null"
printf "."
done
printf "\n"
while read -r P_SEQ P_HOST P_START_TIME P_RUNTIME P_SEND P_RECEIVE P_EXIT_VAL P_SIGNAL P_CMD_1 P_CMD_2 P_CMD_3 P_CMD_4 P_CMD_5 P_CMD_6
do
if [ $P_EXIT_VAL -eq 0 ]
then
BEBOT_IP_RESPONSIVE_ADDRESSES+=($P_CMD_4)
fi
done <<< "$(sed 1,1d $BEBOT_RESPONSE_LOG)" # removes first line and cats into read
rm $BEBOT_RESPONSE_LOG
# start tmux ssh sessions to all robots
for INDEX in "${!BEBOT_IP_RESPONSIVE_ADDRESSES[@]}"
do
printf "Creating pane for: ${BEBOT_IP_RESPONSIVE_ADDRESSES[INDEX]}\n"
if [ $INDEX -eq 0 ]
then
tmux new-session -d -s bebots "ssh root@${BEBOT_IP_RESPONSIVE_ADDRESSES[INDEX]}"
tmux rename-window 'bebots'
else
tmux split-window -h "ssh root@${BEBOT_IP_RESPONSIVE_ADDRESSES[INDEX]}"
fi
done
tmux select-window -t bebots:0
tmux select-pane -t 0
tmux select-layout -t bebots tiled
# add key bindings to session
tmux unbind-key C-s
tmux unbind-key C-r
tmux unbind-key C-p
tmux unbind-key C-h
tmux unbind-key C-d
tmux unbind-key C-x
tmux bind-key -n C-s setw synchronize-panes
tmux bind-key -n C-r send-keys "blocktracker"\\\; send-keys Enter\\\;
tmux bind-key -n C-p send-keys "pmstatus |& grep batt"\\\; send-keys Enter\\\;
tmux bind-key -n C-h send-keys "shutdown -h 0 && exit"\\\; send-keys Enter\\\;
tmux bind-key -n C-d send-keys "wget -qO /usr/bin/blocktracker http://192.168.1.2:8000/build/blocktracker"\\\; send-keys Enter\\\;
tmux bind-key -n C-x send-keys "exit"\\\; send-keys Enter\\\;
# attach to session
tmux attach-session -t bebots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment