Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. # author: Micheal Garner (michael.garner@v<SNIP>.com)
  5. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6.  
  7. # usage function to describe script.
  8. usage() {
  9. cat <<EOM
  10. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. Usage:
  12. $(basename $0) is for running show commands on a list of hosts using ssh.
  13.  
  14. It requires 2 inputs:
  15.  
  16. 1) your hosts file (default=hosts.txt)
  17. 2) your commands file (default=commands.txt)
  18.  
  19. It will generate a output per host (hostname{datestamp}.txt)
  20.  
  21. Please provide the requred inputs;
  22.  
  23. ./show2many.sh hosts.txt commands.txt
  24.  
  25. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. ~~~~~~~~~~ Written by Micheal Garner (michael.garner@<SNIP>.com) ~~~~~~~~~~
  27. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. EOM
  29. exit 0
  30. }
  31.  
  32. # if less than two arguments supplied, display usage
  33. if [ $# -le 1 ]
  34. then
  35. usage
  36. exit 1
  37. fi
  38.  
  39. # check whether user had supplied -h or --help . If yes display usage
  40. if [[ ( $# == "--help") || $# == "-h" ]]
  41. then
  42. usage
  43. exit 0
  44. fi
  45.  
  46. # Define a timestamp as a function (not used yet)
  47. timestamp() {
  48. date +"%T"
  49. }
  50. # Define a date variable called now
  51. now=`date '+%H-%M-%S_%d-%m-%Y'`;
  52. # Define a time variable called time
  53. time=`date '+%H:%M:%S'`;
  54. # take first argument ($1) and add to hostfile variable
  55. hostfile=$1
  56.  
  57. # take second argument and add to commandfile variable
  58. commandfile=$2
  59. # a for loop using hostfile variable
  60. for host in $(cat $hostfile)
  61. do
  62. for command in "$(cat ${commandfile[@]})"
  63. do
  64. echo -e "connecting to $host at $time and running the following commands:${command}"
  65. ssh $host "$command" >> $host$now.txt
  66. done
  67. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement