Advertisement
gregmark

OpenStack or Bust, Part 9: tink-stack-tenant.sh

Mar 15th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.66 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #####################################################################
  4. ###
  5. ### tink-stack-tenant.sh
  6. ### --------------------
  7. ###
  8. ### Script to run through a list of IPs and tinker.
  9. ### DESTRUCTIVE SCRIPT.... proceed cautiously
  10. ###
  11. ### The only argument required is an ip address, hostname, subnet, or
  12. ### any nmap target specification (e.g. 10.21.161.1-25, 10.21.161.1,3,5)
  13. ###
  14. ### Use option "-b" if you are logging into an Ubuntu system
  15. ###
  16. #####################################################################
  17.  
  18. # Make sure your root user has a keystone creds file sourced in
  19. # /root/.profile or this will fail.
  20.  
  21. ######################################################################
  22. ### CUSTOMIZE: write_script
  23. ### -----------------------
  24. ###    The script that is run on the remote host; it should provide
  25. ###    simple output that the process_output function can easily use.
  26. ######################################################################
  27. function write_script () {
  28.     cat <<EOF > $1
  29. #!/bin/bash
  30.  
  31. # See https://bugs.launchpad.net/python-novaclient/+bug/1020238
  32. nova='nova --no-cache'
  33.  
  34. echo
  35. echo "\$ keystone tenant-get \$(stack_id -t project_one)"
  36. keystone tenant-get \$(stack_id -t project_one)
  37. echo
  38.  
  39. echo "\$ keystone user-get \$(stack_id -u user_one)"
  40. keystone user-get \$(stack_id -u user_one)
  41. echo
  42.  
  43. echo "\$ quantum net-show \$(stack_id -n net_proj_one)"
  44. quantum net-show \$(stack_id -n net_proj_one)
  45. echo
  46.  
  47. echo "\$ quantum subnet-show \$(stack_id -b 50.50.1.0/24)"
  48. quantum subnet-show \$(stack_id -b 50.50.1.0/24)
  49. echo
  50.  
  51. echo "\$ quantum router-show \$(stack_id -o router_proj_one)"
  52. quantum router-show \$(stack_id -o router_proj_one)
  53. echo
  54.  
  55. echo "\$ quantum port-list -- --device_id \$(stack_id -o router_proj_one)"
  56. quantum port-list -- --device_id \$(stack_id -o router_proj_one)
  57. echo
  58.  
  59. echo "\$ quantum floatingip-list"
  60. quantum floatingip-list
  61. echo
  62.  
  63. echo "\$ \$nova list"
  64. \$nova list
  65. echo
  66.  
  67. rm -f \$0   # self-destruct
  68. EOF
  69. }
  70.  
  71.  
  72. ######################################################################
  73. ### CUSTOMIZE: process_output
  74. ### -------------------------
  75. ###    If the script produces output, here's where you can tinker with
  76. ###    the presentation
  77. ######################################################################
  78. function process_output () {
  79.     local ip=$1; local hn=$2; local out="$3"
  80.  
  81.     len=$( echo | awk '{print length( a b )}' a=$ip b=$hn )
  82.  
  83.     for (( x=$((len + 1)) ; x > 0 ; x-- )) ; do echo -n "-"; done ; echo
  84.     echo $ip $hn
  85.     for (( x=$((len + 1)) ; x > 0 ; x-- )) ; do echo -n "-"; done ; echo
  86.  
  87.     if [[ -n "$out" ]] ; then
  88.         echo "$out"
  89.     else
  90.         echo "script-FAILED"
  91.     fi
  92.  
  93.     echo ; echo
  94. }
  95.  
  96.  
  97. ######################################################################
  98. ######################################################################
  99. #---------------CHANGE BELOW AT YOUR OWN RISK-------------------------
  100. ######################################################################
  101. ######################################################################
  102.  
  103.  
  104. me=$(basename $0)
  105. USAGE=$( cat <<-EOF
  106.  
  107.     USAGE: $me [-b] [-u <username>] [-s]
  108.  
  109.         -b  login to Ubuntu system as user ubuntu; -s is implied
  110.         -u  login as user <username>
  111.         -s  run remote script with sudo
  112.     EOF
  113. )
  114.  
  115.  
  116. ######################################################################
  117. ### VARIABLES
  118. ######################################################################
  119. user=root; sudo=
  120.  
  121. tscr="/tmp/tink-$$.sh"  # payload script, copied to host targets
  122. tscr_x="/tmp/tink-$$_x.sh"  # script name on taregt hosts
  123. ssh_x="ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o PasswordAuthentication=no"
  124. scp_x="scp -q -o StrictHostKeyChecking=no -o BatchMode=yes -o PasswordAuthentication=no"
  125.  
  126. while getopts "hbu:s" opt ; do
  127.     case $opt in
  128.         h) echo "$USAGE"; echo; exit 1;;
  129.         b) user='ubuntu'; sudo='sudo -i';;
  130.         u) user=$OPTARG ;;
  131.         s) sudo='sudo -i' ;;
  132.     esac
  133. done
  134. shift $((OPTIND - 1))
  135.  
  136. me=$(basename $0)
  137. nmap_range="$@"
  138.  
  139. declare -a IPS  # Array of IP targets
  140.  
  141.  
  142. ######################################################################
  143. ### SANITY CHECK
  144. ######################################################################
  145.  
  146. function USAGE () {
  147.     echo
  148.     echo "USAGE: $me <ip|hostname|subnet|nmap-target>"
  149.     echo
  150.     echo "Edit script to customize write_script() and process_output() functions"
  151.     echo
  152.     exit 1
  153. }
  154.  
  155. if [[ -z "$nmap_range" ]] ; then
  156.     echo "[ERROR]: argument missing"
  157.     USAGE;
  158. elif [[ ! $nmap_range =~ "^[0-9]+\.[0-9]+\.[0-9,-]+\.[0-9,-]+" ]] ; then
  159.     echo "[ERROR]: invalid argument \"$nmap_range\""
  160.     USAGE;
  161. fi
  162.  
  163.  
  164. ######################################################################
  165. ### HELPER FUNCTIONS
  166. ######################################################################
  167.  
  168. function run_nmap () {
  169.     local targ="$@"
  170.     nmap -sP -oG - ${targ} |
  171.         awk '/^Host:/ {print $2}'
  172. }
  173.  
  174. function check_ssh () {
  175.     local host=$1
  176.     echo | nc -w 3 $host 22 > /dev/null 2>&1
  177.     return $?
  178. }
  179.  
  180.  
  181. ######################################################################
  182. ### MAIN
  183. ######################################################################
  184.  
  185. IPS=( $( run_nmap $nmap_range ) )
  186.  
  187. echo
  188. echo "${#IPS[@]} pingable IPs discovered"
  189. echo
  190.  
  191. write_script $tscr
  192.  
  193. for IP in ${IPS[@]} ; do
  194.  
  195.     search_dom=".$(awk '$1 == "search"{print $2}' /etc/resolv.conf)."
  196.     HN=$(dig +short -x $IP)
  197.     HN=${HN%$search_dom}
  198.  
  199.     if check_ssh $IP; then
  200.  
  201.         if $scp_x $tscr ${user}@${IP}:${tscr_x} >/dev/null 2>&1 ; then
  202.             ssh_out="$( $ssh_x $user@$IP $sudo bash $tscr_x 2>/dev/null )"
  203.             process_output $IP $HN "$ssh_out"
  204.         else
  205.             echo "$HN ($IP): scp-FAILED"
  206.         fi
  207.  
  208.     else
  209.  
  210.         echo "$HN ($IP): ssh-FAILED"
  211.  
  212.     fi
  213.  
  214. done
  215.  
  216. ### Axe the temporary tink script
  217. rm -f $tscr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement