-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_network_services.sh
More file actions
executable file
·85 lines (69 loc) · 2.18 KB
/
check_network_services.sh
File metadata and controls
executable file
·85 lines (69 loc) · 2.18 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
#!/bin/bash
# Constants for colors
GREEN="\033[32m"
RED="\033[31m"
RESET="\033[0m"
# Constants for indicators
allOK="${GREEN}✦${RESET}"
oShit="${RED}✧${RESET}"
### Cooler alternatives to colored indicators
# allOK="👌 "
# oShit="💩 "
# FQDN to lookup
FQDN_LOOKUP="google.com"
get_active_network_interfaces() {
# Get the list of network services in the preferred order
local services
services=$(networksetup -listnetworkserviceorder | grep -o 'Device: [^)]*' | awk '{print $2}')
# Iterate through each service and check its status using ifconfig
for service in $services; do
# Check if the device exists and is active
if ifconfig "$service" >/dev/null 2>&1; then
local status
status=$(ifconfig "$service" | grep 'status:' | awk '{print $2}')
if [ "$status" = "active" ]; then
echo "$service"
fi
fi
done
}
get_interface_details() {
local interface=$1
# Get the IP address and CIDR notation
local ip_cidr
ip_cidr=$(ifconfig -f inet:cidr "$interface" inet | grep 'inet ' | awk '{print $2}')
local ip=${ip_cidr%/*}
local cidr=${ip_cidr#*/}
# Get the default gateway
local gateway
gateway=$(netstat -nr | grep 'default' | grep "$interface" | awk '{print $2}')
# Ping the default gateway once
if ping -t 1 -c 1 "$gateway" > /dev/null 2>&1; then
gateway_status=$allOK
else
gateway_status=$oShit
fi
echo -e "Interface: $interface, IP: $ip/$cidr, Gateway: $gateway $gateway_status"
}
check_dns_servers() {
local nameservers
nameservers=$(grep -e "^nameserver" /etc/resolv.conf | awk '{ print $2 }')
for nameserver in $nameservers; do
echo -n "DNS: $nameserver "
if nslookup "$FQDN_LOOKUP" "$nameserver" > /dev/null 2>&1; then
echo -e "$allOK" # Green star for success
else
echo -e "$oShit" # Red star for failure
fi
done
}
main() {
local active_interfaces
active_interfaces=$(get_active_network_interfaces)
for interface in $active_interfaces; do
get_interface_details "$interface"
done
# Check DNS servers
check_dns_servers
}
main