Browse Source

added the host tagging functionality, and can handle multiple arguments

\/\/ \-/ ❭❬ 4 years ago
parent
commit
0bf73df738
1 changed files with 47 additions and 26 deletions
  1. 47
    26
      mhssh

+ 47
- 26
mhssh View File

@@ -20,6 +20,7 @@
20 20
 # You can specify multiple tags [a-z0-9] for each host, so you can filter easier
21 21
 # which hosts to work on
22 22
 hosts="$HOME/.config/mhssh.hosts"
23
+tags=""
23 24
 
24 25
 red=$(tput setaf 1)
25 26
 grn=$(tput setaf 2)
@@ -32,10 +33,9 @@ usage() {
32 33
 	echo "Run a single command on multiple hosts"
33 34
 	echo "Usage : $0 [-h|--help] [-n|--no-color] [-B|--background] [-D|--detach]
34 35
     [-t|--tags tags] CMD ..."
35
-	echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable
36
-    colors\n\t-B --background\tput the task in background\n\t-D --detach\tuse
37
-    dtach to put the task in background\n\t-t --tags tags\n\n"
38
-	echo "Host list configured to be found in '$hosts'"
36
+	echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\t-B --background\tput the task in background\n\t-D --detach\tuse dtach to put the task in background\n\t-t --tags tags (separated by coma)\n\n"
37
+	echo "Host list configured to be found in '$hosts', in format \"host.name tag1 ... tagn\""
38
+    echo "Tags are not mandatory for mhssh to work"
39 39
 }
40 40
 
41 41
 nocmd() {
@@ -44,29 +44,50 @@ nocmd() {
44 44
 	exit 3
45 45
 }
46 46
 
47
+host_filter() {
48
+    if [[ -z "$tags" ]]
49
+    then
50
+       cat $hosts|cut -d' ' -f1
51
+    else
52
+        tags="($(echo "$tags"|sed 's/,/\|/g'))"
53
+        cat $hosts|grep -E $tags|cut -d' ' -f1
54
+    fi
55
+}
56
+
47 57
 # arg "parsing"
48 58
 [ $# -eq 0 ] && nocmd
49
-case "$1" in
50
-	-h|--help)
51
-		usage
52
-		exit 1
53
-	;;
54
-	-n|--no-color)
55
-		[ $# -eq 1 ] && nocmd
56
-		shift
57
-		red='[ERR] '
58
-		grn=''
59
-		raz=''
60
-	;;
61
-	-B|--background)
62
-		shift
63
-		bgnd='on'
64
-	;;
65
-	-D|--detach)
66
-		shift
67
-		dtch='on'
68
-	;;
69
-esac
59
+while [[ "$#" -gt 0 ]]
60
+do
61
+    case "$1" in
62
+        -h|--help)
63
+            usage
64
+            exit 1
65
+        ;;
66
+        -n|--no-color)
67
+            [ $# -eq 1 ] && nocmd
68
+            shift
69
+            red='[ERR] '
70
+            grn=''
71
+            raz=''
72
+        ;;
73
+        -B|--background)
74
+            shift
75
+            bgnd='on'
76
+        ;;
77
+        -D|--detach)
78
+            shift
79
+            dtch='on'
80
+        ;;
81
+        -t|--tags)
82
+            shift
83
+            tags=$1
84
+            shift
85
+        ;;
86
+        *)
87
+            break
88
+        ;;
89
+    esac
90
+done;
70 91
 
71 92
 if [ ! -f "$hosts" ]
72 93
 then
@@ -77,7 +98,7 @@ then
77 98
 fi
78 99
 
79 100
 
80
-for host in $(cat $hosts)
101
+for host in $(host_filter)
81 102
 do
82 103
 	echo -e "\n$grn=====================\n$grn$host\n$grn=====================$raz\n"
83 104
 	stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz")

Loading…
Cancel
Save