-
Notifications
You must be signed in to change notification settings - Fork 0
/
ups_tool.sh
executable file
·56 lines (52 loc) · 2.22 KB
/
ups_tool.sh
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
#!/bin/bash
# Define the variables
address="192.168.2.4" # Replace with the desired IP address
command="snmpwalk -v2c -c homelab 192.168.2.4 .1.3.6.1.4.1.8072.1.3.2.4.1.2 | grep -i string | awk -F'\"' '{print \$2}'"
user="labuser"
# Function to display script usage
usage() {
echo "Usage: $0 [-a <about>] [-v <volts>] [-p <percent>] [-o <on-line>] [-t <time>]"
echo "Options:"
echo " -a UPS description"
echo " -v Votage of UPS (Healthy: 27v)"
echo " -p Percent of battery left (Full: 100)"
echo " -o On line or Disconnected (Good: "OL")"
echo " -t Seconds estimated left (1620)"
exit 1
}
# Ping the address to check if it is reachable
if ping -c 1 -W 1 "$address" &> /dev/null; then
# Parse command line options
while getopts "avpom" opt; do
case "$opt" in
a)
about="2"
command="snmpwalk -v2c -c homelab 192.168.2.4 .1.3.6.1.4.1.8072.1.3.2.4.1.2 | grep -i string | awk -F'\"' '{print \$2}' | sed -n '2p'"
;;
v)
volts="3"
command="snmpwalk -v2c -c homelab 192.168.2.4 .1.3.6.1.4.1.8072.1.3.2.4.1.2 | grep -i string | awk -F'\"' '{print \$2}' | sed -n '3p' | bc -l" #Save as a float
;;
p)
percent="6"
command="snmpwalk -v2c -c homelab 192.168.2.4 .1.3.6.1.4.1.8072.1.3.2.4.1.2 | grep -i string | awk -F'\"' '{print \$2}' | sed -n '6p' | awk '{print \$0+0}'" #Save as an int
;;
o)
online="5"
command="snmpwalk -v2c -c homelab 192.168.2.4 .1.3.6.1.4.1.8072.1.3.2.4.1.2 | grep -i string | awk -F'\"' '{print \$2}' | sed -n '5p'"
;;
m)
time="7"
command="snmpwalk -v2c -c homelab 192.168.2.4 .1.3.6.1.4.1.8072.1.3.2.4.1.2 | grep -i string | awk -F'\"' '{print \$2}' | sed -n '7p' | awk '{print \$0+0}'" #Save as an int
;;
*)
usage
;;
esac
done
# If ping is successful, execute the command over SSH and capture the response
response=$(ssh "$user"@"$address" $command)
echo "$response"
else
echo "Server is not reachable"
fi