-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_ops.sh
More file actions
147 lines (123 loc) · 3.4 KB
/
batch_ops.sh
File metadata and controls
147 lines (123 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
set -euo pipefail
TARGET_FILE=""
IPS=""
COMMAND=""
SCRIPT_FILE=""
print_help() {
cat <<EOF
Usage: $0 [OPTIONS]
Options:
-t, --target-file FILE File containing target IPs (one IP per line)
-i, --ips IPS Target IPs separated by comma
-c, --command CMD Command to execute on remote hosts
-f, --file SCRIPT Local shell script to execute remotely
-h, --help Show this help message
Examples:
Execute command:
$0 -t ip.txt -c "uname -a"
Execute local script on remote hosts:
$0 -t ip.txt -f deploy.sh
Execute command with inline IPs:
$0 -i 10.1.1.1,10.1.1.2 -c "uname -a"
EOF
}
########################################
# 参数解析
########################################
while [[ $# -gt 0 ]]; do
case "$1" in
-t|--target-file)
TARGET_FILE="$2"
shift 2
;;
-i|--ips)
IPS="$2"
shift 2
;;
-c|--command)
COMMAND="$2"
shift 2
;;
-f|--file)
SCRIPT_FILE="$2"
shift 2
;;
-h|--help)
print_help
exit 0
;;
*)
echo "Unknown argument: $1"
print_help
exit 1
;;
esac
done
########################################
# 参数检查
########################################
# -t 和 -i 必须二选一
if [[ -z "$TARGET_FILE" && -z "$IPS" ]]; then
echo "Error: either --target-file or --ips must be specified"
exit 1
fi
if [[ -n "$TARGET_FILE" && -n "$IPS" ]]; then
echo "Error: --target-file and --ips cannot be used together"
exit 1
fi
if [[ -n "$TARGET_FILE" ]]; then
if [[ ! -f "$TARGET_FILE" ]]; then
echo "Error: target file not found: $TARGET_FILE"
exit 1
fi
if [[ ! -s "$TARGET_FILE" ]]; then
echo "Error: target file is empty"
exit 1
fi
fi
if [[ -n "$IPS" ]]; then
if [[ "$IPS" == "" ]]; then
echo "Error: --ips cannot be empty"
exit 1
fi
fi
# -c 和 -f 必须二选一
if [[ -n "$COMMAND" && -n "$SCRIPT_FILE" ]]; then
echo "Error: --command and --file cannot be used together"
exit 1
fi
if [[ -z "$COMMAND" && -z "$SCRIPT_FILE" ]]; then
echo "Error: either --command or --file must be specified"
exit 1
fi
# -c 参数不能为空
if [[ -n "$COMMAND" && "$COMMAND" == "" ]]; then
echo "Error: command cannot be empty"
exit 1
fi
# -f 文件必须存在
if [[ -n "$SCRIPT_FILE" && ! -f "$SCRIPT_FILE" ]]; then
echo "Error: script file not found: $SCRIPT_FILE"
exit 1
fi
########################################
# 执行逻辑
########################################
if [[ -n "$TARGET_FILE" ]]; then
IP_LIST=$(grep -v '^\s*$' "$TARGET_FILE")
else
IP_LIST=$(echo "$IPS" | tr ',' ' ')
fi
for ip in $IP_LIST; do
echo "=============================="
echo -e "Host: \e[31m$ip\e[0m start"
if [[ -n "$COMMAND" ]]; then
ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=5 "$ip" "$COMMAND"
else
ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=5 "$ip" "bash -s" < "$SCRIPT_FILE"
fi
echo -e "Host: \e[31m$ip\e[0m finished"
echo -e "==============================\n"
done
122,1 底端