#!/bin/bash #set -x # Parse command line options using getopt options=$(getopt -o f:l:o:s:ah --long first:,last:,only:,system:,all,help -- "$@") eval set -- "$options" LIST="" # get node definitions source ./node_list.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 system="deb" # default login="root" host_ip="" all=false # 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]>] [--system <[tc/deb]>] [--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 " --system (-s) Define the operating system (deb)" echo " --all (-a) Set all nodes" echo " --help (-h) Displays help" } # 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 ;; -s|--system) system="$2" shift 2 ;; -a|--all) start_node=0 stop_node=84 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) if [[ $system != "tc" && $system != "deb" ]];then echo "The defined operating system is unknown (tc/deb)" echo "" display_help exit 1 fi if [[ $system = "tc" ]]; then login="tc" fi # 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 "Shutting down nodes $start_node to $stop_node with running OS $system" echo "" # 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 "Shutting down apu$current with the IP $host_ip..." ssh -o StrictHostKeyChecking=no "$login"@"apu$current" "sudo shutdown 0" & 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