#!/bin/bash #set -x # Parse command line options using getopt options=$(getopt -o f:l:o:ah --long first:,last:,only:,all,help -- "$@") eval set -- "$options" LIST="" # get node definitions source ./node_list.sh # get node addresses source ./deploy/mesh/node_addresses.sh #all default values are according to the testbed first_mendatory=true last_mendatory=true start_node=0 # default stop_node=24 # default current=0 login="root" host_ip="" all=false OUTPUT_FILE="plink_info.txt" # Function to resolve th ip of a given host function resolveip(){ local host="$1" if [ -z "$host" ];then host_ip="resolving failed" else local ip=$( getent hosts "$host" | awk '{print $1}' ) if [ -z "$ip" ];then ip=$( dig +short "$host" ) if [ -z "$ip" ];then # echo "unable to resolve '$host'" >&2 host_ip="resolving failed" else # echo "$ip" host_ip=$ip fi else # echo "$ip" host_ip=$ip fi fi } # Function to display usage information function display_usage { echo "Usage: $0 [--first <[0..84]>] [--last <[0..84]>] [--all ] [--only <[0..84]>] [--help]" } # Function to display help information function display_help { display_usage echo echo "Options:" echo " --first (-f) Set the start node (0)" echo " --last (-l) Set the stop node (24)" echo " --only (-o) Only one node" echo " --all (-a) Set all nodes" echo " --help (-h) Displays help" } # Function to find the key for a given MAC address from a given associative array find_key_for_mac() { local mac=$1 declare -n mac_array=$2 # Use nameref for associative array for key in "${!mac_array[@]}"; do if [[ "${mac_array[$key]}" == "$mac" ]]; then echo "$key" return fi done echo "Unknown" } # Function to check and display iw dev output function check_links() { local REMOTE_HOST="$1" local REMOTE_USER="root" local PHY_LIST="phy${2}MAC" local DEV="mesh$2" echo "Checking links on $DEV of $REMOTE_HOST" ESTAB_LINKS=() BLOCKED_LINKS=() ESTAB_COUNT=0 BLOCKED_COUNT=0 while IFS= read -r line; do if [[ $line =~ Station ]]; then MAC=$(echo $line | awk '{print $2}') KEY=$(find_key_for_mac "$MAC" "$PHY_LIST") # echo "$KEY" elif [[ $line =~ mesh\ plink ]]; then STATUS=$(echo $line | awk '{print $3}') elif [[ $line =~ signal\ avg ]]; then SIGNAL_AVG=$(echo $line | awk '{print $3}') if [ "$STATUS" == "ESTAB" ]; then ESTAB_LINKS+=("MAC: $MAC, Name: $KEY, Status: $STATUS, Signal Avg: $SIGNAL_AVG dBm") ESTAB_COUNT=$((ESTAB_COUNT + 1)) elif [ "$STATUS" == "BLOCKED" ]; then BLOCKED_LINKS+=("MAC: $MAC, Name: $KEY, Status: $STATUS, Signal Avg: $SIGNAL_AVG dBm") BLOCKED_COUNT=$((BLOCKED_COUNT + 1)) fi fi done < <(ssh ${REMOTE_USER}@${REMOTE_HOST} "iw dev $DEV station dump") echo "Established Links (Count: $ESTAB_COUNT):" for LINK in "${ESTAB_LINKS[@]}"; do echo "$LINK" done echo "----------------------------------------" if [ "$all" == true ]; then echo "Blocked Links (Count: $BLOCKED_COUNT):" for LINK in "${BLOCKED_LINKS[@]}"; do echo "$LINK" done echo "----------------------------------------" fi } # argument parsing while true; do case "$1" in -f|--first) start_node="$2" shift 2 ;; -l|--last) stop_node="$2" shift 2 ;; -o|--only) start_node="$2" stop_node="$2" shift 2 ;; -a|--all) all=true shift ;; -h|--help) display_help exit 0 ;; --) shift break ;; *) display_usage exit 1 ;; esac done # arranging node list LIST=$(seq $start_node 1 $stop_node) # check ranges if [[ $start_node -gt 84 || $start_node -lt 0 || $stop_node -gt 84 || $stop_node -lt 0 ]];then echo "Nodes out of range" echo "" display_help exit 1 fi # echo "Rebooting nodes $start_node to $stop_node with running OS $system" echo "Checking Peer Links on nodes $start_node to $stop_node:" >> $OUTPUT_FILE echo "" > $OUTPUT_FILE # interate list for i in $LIST ; do found=false # search for node in defined node list (array) for j in "${NODES[@]}"; do for k in $j; do #echo $j if [[ $i -eq $k ]]; then found=true fi done done if [[ $i -lt 10 ]];then current=0$i else current=$i fi if [ "$found" = true ]; then # delete node from known-host list to avoid problems when switching between deb and tc ssh-keygen -f "/home/apu/.ssh/known_hosts" -R "apu$current" > /dev/null 2>&1 # get node ip resolveip "apu$current" # echo "Refreshing init script on apu$current with the IP $host_ip..." >> $OUTPUT_FILE # ssh -o StrictHostKeyChecking=no "$login"@"apu$current" "sudo bash /opt/start_script.sh" & >> $OUTPUT_FILE # ssh -o StrictHostKeyChecking=no "$login"@"apu$current" "sudo bash /opt/start_script.sh &" >> $OUTPUT_FILE check_links "apu$current" "0" >> $OUTPUT_FILE check_links "apu$current" "1" >> $OUTPUT_FILE else # only inform the user about missing node defines when defining ranges by him-/herself if [ "$all" = false ]; then echo "Node apu$current not defined" fi fi done #set +x