Remove user, add dryRun

* Remove user option, shoud be defined in config or SSH will use default
* Improve usage()
* Add dryRun option
* Some syntax enhancement
This commit is contained in:
Flo 2021-03-28 19:05:03 +02:00
parent 14d7d8c503
commit f3c83317eb
2 changed files with 86 additions and 42 deletions

View File

@ -1,13 +1,28 @@
# csc : cluster ssh command
# csc : Cluster SSH Command
simple script to execute command over ssh on remote hosts with "dialog" DEBIAN_FRONTEND
Launch command over SSH on cluster hosts with "dialog" DEBIAN_FRONTEND.
## Tree
```bash
$ tree
.
├── csc
└── README.md
0 directories, 2 files
```
## Usage
```bash
$ csc -h
Usage: csc [OPTION]... [COMMAND]
-v enable verbose mode
-c specify config dir, default is /$HOME/.config/csc
-u specify user, default is $USER
-g specify hosts group
Host group file must exists under config dir, one host per line
-h show help
-v enable verbose mode
-c specify config dir, default is /home/flo/.config/csc
-g specify group
-d enable dry run mode
```
Host group file must exists under config dir, one host per line.

97
csc
View File

@ -1,35 +1,43 @@
#!/bin/bash
# csc : cluster ssh command
# launch command over ssh on cluster hosts with "dialog" DEBIAN_FRONTEND
#
# csc : Cluster SSH Command
# Launch command over SSH on cluster hosts with "dialog" DEBIAN_FRONTEND.
#
# Usage : csc [OPTION]... [COMMAND]
# options :
# -v enable verbose mode
# -c specify config dir, default is $HOME/.config/csc
# -u specify user, default is $USER
# -g specify hosts group
usage () {
echo "Usage: csc [OPTION]... [COMMAND]"
echo " -h show help"
echo " -v enable verbose mode"
echo " -c specify config dir, default is $HOME/.config/csc"
echo " -g specify group"
echo " -d enable dry run mode"
}
# defaults
# variable defaults
usage=false
verbose=false
confdir="$HOME/.config/csc"
user=$USER
dryRun=false
# get options
while getopts ":vc:u:g:" opt; do
while getopts "hvc:g:d" opt; do
case $opt in
h)
usage=true
;;
v)
verbose=true
;;
c)
confdir=$OPTARG
;;
u)
user=$OPTARG
;;
g)
group=$OPTARG
;;
d)
dryRun=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
@ -44,28 +52,38 @@ done
# clean $@ of options
shift $((OPTIND-1))
cmd=$@
cmd=$*
if [ "$usage" = true ]
then
usage
exit 0
fi
if [ "$verbose" = true ]
then
echo "> starting csc"
echo "> options :"
echo " - help: $usage"
echo " - verbose: $verbose"
echo " - confdir: $confdir"
echo " - group: $group"
echo " - dryRun: $dryRun"
fi
if [ -z "$cmd" ]
then
echo "Error : command is missing"
echo "Usage: csc [OPTION]... [COMMAND]"
echo " -v enable verbose mode"
echo " -c specify config dir, default is $HOME/.config/csc"
echo " -u specify user, default is $USER"
echo " -g specify group"
echo
usage
exit 1
fi
if [ -z "$user" ]
then
echo "Error: user is not defined !"
exit
fi
if [ -z "$group" ]
then
echo "Error: group is not defined !"
echo
usage
exit
fi
@ -74,6 +92,8 @@ hosts="$confdir/$group"
if [ -z "$hosts" ]
then
echo "Error: hosts file $confdir/$group do not exists !"
echo
usage
exit
fi
@ -84,16 +104,25 @@ FRONTEND="TERM=$TERM DEBIAN_FRONTEND=dialog"
if [ "$verbose" = true ]
then
echo "will do :"
echo "for i in `cat $hosts | grep -v '^#'`; do ssh -t $user@\$i "$FRONTEND $cmd"; done"
echo "> will do :"
echo " grep -v '^ *#' < \"$hosts\" | while IFS= read -r line; do ssh -n -t \"\$line\" \"$FRONTEND $cmd\"; done"
fi
for i in `cat $hosts | grep -v '^#'`
if [ "$dryRun" = false ]
then
for line in $(grep -v '^#' "$hosts")
do
echo -e "\e[34m---------------\e[0m"
echo -e "\e[34m$i\e[0m"
echo -e "\e[34m---------------\e[0m"
ssh -t $user@$i "$FRONTEND $cmd"
done
echo -e "\e[34m------------------------------------------------------------\e[0m"
echo -e "\e[34m> \e[32m\"$cmd\" \e[34mon \e[32m$line\e[0m"
echo -e "\e[34m> \e[33mssh -t \"$line\" \"$FRONTEND $cmd\"\e[0m"
echo -e "\e[34m------------------------------------------------------------\e[0m"
ssh -t "$line" "$FRONTEND $cmd"
done
fi
if [ "$verbose" = true ]
then
echo "> All done, bye."
fi
exit