Advertisement
gregmark

OpenStack or Bust, Part 6: tink-stack.sh script

Mar 10th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.75 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #####################################################################
  4. ###
  5. ### tink-stack.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.  
  19.  
  20. ######################################################################
  21. ### CUSTOMIZE: write_script
  22. ### -----------------------
  23. ###    The script that is run on the remote host; it should provide
  24. ###    simple output that the process_output function can easily use.
  25. ######################################################################
  26. function write_script () {
  27.     cat <<EOF > $1
  28. #!/bin/bash
  29.  
  30. ntp_host=\$(awk '\$1 == "server" {print \$2}' /etc/ntp.conf | head -1)
  31. service ntp stop >/dev/null 2>&1
  32. echo "$ ntpdate-debian \$ntp_host"
  33. ntpdate-debian \$ntp_host
  34. service ntp start >/dev/null 2>&1
  35.  
  36. if ! service ntp status >/dev/null 2>&1 ; then
  37.   echo "[WARNING]: ntp is stopped"
  38. fi
  39. echo
  40.  
  41. echo "$ sysctl net.ipv4.ip_forward"
  42. sysctl net.ipv4.ip_forward
  43. echo
  44.  
  45. echo "$ ip addr show | egrep \"^([0-9]| *link| *inet \")"
  46. ip addr show | egrep "^([0-9]| *link| *inet )"
  47. echo
  48.  
  49. echo "$ netstat -rn"
  50. netstat -rn
  51. echo
  52.  
  53. echo "$ ip netns show"
  54. ip netns show
  55. echo
  56.  
  57. echo "$ ls -R /var/run/netns; ls -R /etc/netns"
  58. ls -R /var/run/netns; ls -R /etc/netns
  59. echo
  60.  
  61. if [ -x /sbin/brctl ] ; then
  62.   echo "$ brctl show"
  63.   brctl show
  64.   echo
  65. fi
  66.  
  67. if [ -x /usr/bin/ovs-vsctl ] ; then
  68.   echo "$ ovs-vsctl show"
  69.   ovs-vsctl show
  70.   echo
  71. fi
  72.  
  73. echo "$ ping 10.0.164.1"
  74. ping -c2 10.0.164.1 >/dev/null 2>&1 && echo UP || echo DOWN
  75. echo
  76.  
  77. echo "$ ping 192.168.241.1"
  78. ping -c2 192.168.241.1 >/dev/null 2>&1 && echo UP || echo DOWN
  79. echo
  80.  
  81. echo "$ ping 192.168.239.1"
  82. ping -c2 192.168.239.1 >/dev/null 2>&1 && echo UP || echo DOWN
  83. echo
  84. echo
  85.  
  86. rm -f \$0   # self-destruct
  87. EOF
  88. }
  89.  
  90.  
  91. ######################################################################
  92. ### CUSTOMIZE: process_output
  93. ### -------------------------
  94. ###    If the script produces output, here's where you can tinker with
  95. ###    the presentation
  96. ######################################################################
  97. function process_output () {
  98.     local ip=$1; local hn=$2; local out="$3"
  99.  
  100.     len=$( echo | awk '{print length( a b )}' a=$ip b=$hn )
  101.  
  102.     for (( x=$((len + 1)) ; x > 0 ; x-- )) ; do echo -n "-"; done ; echo
  103.     echo $ip $hn
  104.     for (( x=$((len + 1)) ; x > 0 ; x-- )) ; do echo -n "-"; done ; echo
  105.  
  106.     if [[ -n "$out" ]] ; then
  107.         echo "$out"
  108.     else
  109.         echo "script-FAILED"
  110.     fi
  111.  
  112.     echo ; echo
  113. }
  114.  
  115.  
  116. ######################################################################
  117. ######################################################################
  118. #---------------CHANGE BELOW AT YOUR OWN RISK-------------------------
  119. ######################################################################
  120. ######################################################################
  121.  
  122.  
  123. me=$(basename $0)
  124. USAGE=$( cat <<-EOF
  125.  
  126.     USAGE: $me [-b] [-u <username>] [-s]
  127.  
  128.         -b  login to Ubuntu system as user ubuntu; -s is implied
  129.         -u  login as user <username>
  130.         -s  run remote script with sudo
  131.     EOF
  132. )
  133.  
  134.  
  135. ######################################################################
  136. ### VARIABLES
  137. ######################################################################
  138. user=root; sudo=
  139.  
  140. tscr="/tmp/tink-$$.sh"  # payload script, copied to host targets
  141. tscr_x="/tmp/tink-$$_x.sh"  # script name on taregt hosts
  142. ssh_x="ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o PasswordAuthentication=no"
  143. scp_x="scp -q -o StrictHostKeyChecking=no -o BatchMode=yes -o PasswordAuthentication=no"
  144.  
  145. while getopts "hbu:s" opt ; do
  146.     case $opt in
  147.         h) echo "$USAGE"; echo; exit 1;;
  148.         b) user='ubuntu'; sudo='sudo';;
  149.         u) user=$OPTARG ;;
  150.         s) sudo='sudo' ;;
  151.     esac
  152. done
  153. shift $((OPTIND - 1))
  154.  
  155. me=$(basename $0)
  156. nmap_range="$@"
  157.  
  158. declare -a IPS  # Array of IP targets
  159.  
  160.  
  161. ######################################################################
  162. ### SANITY CHECK
  163. ######################################################################
  164.  
  165. function USAGE () {
  166.     echo
  167.     echo "USAGE: $me <ip|hostname|subnet|nmap-target>"
  168.     echo
  169.     echo "Edit script to customize write_script() and process_output() functions"
  170.     echo
  171.     exit 1
  172. }
  173.  
  174. if [[ -z "$nmap_range" ]] ; then
  175.     echo "[ERROR]: argument missing"
  176.     USAGE;
  177. elif [[ ! $nmap_range =~ "^[0-9]+\.[0-9]+\.[0-9,-]+\.[0-9,-]+" ]] ; then
  178.     echo "[ERROR]: invalid argument \"$nmap_range\""
  179.     USAGE;
  180. fi
  181.  
  182.  
  183. ######################################################################
  184. ### HELPER FUNCTIONS
  185. ######################################################################
  186.  
  187. function run_nmap () {
  188.     local targ="$@"
  189.     nmap -sP -oG - ${targ} |
  190.         awk '/^Host:/ {print $2}'
  191. }
  192.  
  193. function check_ssh () {
  194.     local host=$1
  195.     echo | nc -w 3 $host 22 > /dev/null 2>&1
  196.     return $?
  197. }
  198.  
  199.  
  200. ######################################################################
  201. ### MAIN
  202. ######################################################################
  203.  
  204. IPS=( $( run_nmap $nmap_range ) )
  205.  
  206. echo
  207. echo "${#IPS[@]} pingable IPs discovered"
  208. echo
  209.  
  210. write_script $tscr
  211.  
  212. for IP in ${IPS[@]} ; do
  213.  
  214.     search_dom=".$(awk '$1 == "search"{print $2}' /etc/resolv.conf)."
  215.     HN=$(dig +short -x $IP)
  216.     HN=${HN%$search_dom}
  217.  
  218.     if check_ssh $IP; then
  219.  
  220.         if $scp_x $tscr ${user}@${IP}:${tscr_x} >/dev/null 2>&1 ; then
  221.             ssh_out="$( $ssh_x $user@$IP $sudo bash $tscr_x 2>/dev/null )"
  222.             process_output $IP $HN "$ssh_out"
  223.         else
  224.             echo "$HN ($IP): scp-FAILED"
  225.         fi
  226.  
  227.     else
  228.  
  229.         echo "$HN ($IP): ssh-FAILED"
  230.  
  231.     fi
  232.  
  233. done
  234.  
  235. ### Axe the temporary tink script
  236. rm -f $tscr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement