Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # author: Micheal Garner (michael.garner@v<SNIP>.com)
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # usage function to describe script.
- usage() {
- cat <<EOM
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Usage:
- $(basename $0) is for running show commands on a list of hosts using ssh.
- It requires 2 inputs:
- 1) your hosts file (default=hosts.txt)
- 2) your commands file (default=commands.txt)
- It will generate a output per host (hostname{datestamp}.txt)
- Please provide the requred inputs;
- ./show2many.sh hosts.txt commands.txt
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ~~~~~~~~~~ Written by Micheal Garner (michael.garner@<SNIP>.com) ~~~~~~~~~~
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- EOM
- exit 0
- }
- # if less than two arguments supplied, display usage
- if [ $# -le 1 ]
- then
- usage
- exit 1
- fi
- # check whether user had supplied -h or --help . If yes display usage
- if [[ ( $# == "--help") || $# == "-h" ]]
- then
- usage
- exit 0
- fi
- # Define a timestamp as a function (not used yet)
- timestamp() {
- date +"%T"
- }
- # Define a date variable called now
- now=`date '+%H-%M-%S_%d-%m-%Y'`;
- # Define a time variable called time
- time=`date '+%H:%M:%S'`;
- # take first argument ($1) and add to hostfile variable
- hostfile=$1
- # take second argument and add to commandfile variable
- commandfile=$2
- # a for loop using hostfile variable
- for host in $(cat $hostfile)
- do
- for command in "$(cat ${commandfile[@]})"
- do
- echo -e "connecting to $host at $time and running the following commands:${command}"
- ssh $host "$command" >> $host$now.txt
- done
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement