-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathubuntu_checker.sh
More file actions
273 lines (234 loc) · 8.35 KB
/
ubuntu_checker.sh
File metadata and controls
273 lines (234 loc) · 8.35 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash
# Định nghĩa các biểu tượng và màu sắc
INFO_ICON="ℹ️" # Information
SUCCESS_ICON="✅" # Success
WARNING_ICON="⚠️" # Warning
ERROR_ICON="❌" # Error
# Hàm trợ giúp
error_exit() {
echo -e "${ERROR_ICON} Error: $1" >&2
exit 1
}
command_exists() {
command -v "$1" &> /dev/null
}
# Tùy chọn người dùng
SHOW_ALL=1
INTERACTIVE=0
SAVE_RESULTS=0
RESULTS_FILE="system_info_$(date +'%Y%m%d_%H%M%S').txt"
# Parse các tùy chọn
while getopts "hias" opt; do
case $opt in
h)
echo "Usage: $0 [options]"
echo "Options:"
echo " -h Show this help message"
echo " -i Interactive mode"
echo " -a Show all information"
echo " -s Save results to file"
exit 0
;;
i)
INTERACTIVE=1
;;
a)
SHOW_ALL=1
;;
s)
SAVE_RESULTS=1
;;
*)
error_exit "Invalid option: $OPTARG"
;;
esac
done
# Kiểm tra và cài đặt speedtest-cli
if ! command_exists speedtest-cli; then
echo -e "================= Checking and Installing speedtest-cli =================="
echo -e "${INFO_ICON} speedtest-cli is not installed. Installing..."
if command_exists pip3; then
sudo pip3 install speedtest-cli || error_exit "Failed to install speedtest-cli using pip3."
echo -e "${SUCCESS_ICON} speedtest-cli installed successfully using pip3."
else
echo -e "${WARNING_ICON} pip3 not found. Attempting to install pip3..."
sudo apt-get update && sudo apt-get install -y python3-pip || error_exit "Failed to install pip3. Please install pip3 manually and try again."
sudo pip3 install speedtest-cli || error_exit "Failed to install speedtest-cli using pip3 after installing pip3."
echo -e "${SUCCESS_ICON} pip3 and speedtest-cli installed successfully."
fi
echo ""
fi
# Chế độ tương tác
if [ $INTERACTIVE -eq 1 ]; then
echo -e "Welcome! You are in interactive mode."
read -p "Do you want to continue? (y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Nn]$ ]]; then
echo "Goodbye!"
exit 0
fi
fi
# Hiển thị thông tin hệ thống
echo -e "================= System Information =================="
echo "Hostname: $(hostname)"
echo "Current time: $(date)"
echo "Kernel: $(uname -r)"
echo "Uptime: $(uptime -p)"
echo ""
# Hiển thị thông tin hệ điều hành
echo -e "================= Operating System =================="
os_name=$(lsb_release -d | awk -F: '{print $2}' | sed 's/^ *//;s/ *$//')
if [ -z "$os_name" ]; then
os_name=$(cat /etc/os-release | grep PRETTY_NAME | cut -d '=' -f2 | tr -d '"')
fi
if [ -z "$os_name" ]; then
echo -e "${WARNING_ICON} Unable to determine operating system name."
else
echo "Operating System: $os_name"
fi
echo ""
# Hiển thị thông tin CPU
echo -e "================= CPU Information =================="
echo "Model: $(grep "model name" /proc/cpuinfo | head -n 1 | awk -F: '{print $2}' | sed 's/^ *//;s/ *$//')"
echo "Number of cores: $(nproc)"
echo ""
# Hiển thị thông tin bộ nhớ
echo -e "================= Memory Information =================="
total_mem_kb=$(grep "MemTotal" /proc/meminfo | awk -F: '{print $2}' | sed 's/ kB//;s/^ *//')
total_mem_gb=$(echo "scale=2; $total_mem_kb / 1024 / 1024" | bc)
echo "Total Memory (RAM): ${total_mem_gb} GB"
free_mem_gb=$(free -g | awk 'NR==2 {print $4}')
if [[ "$free_mem_gb" == "" || "$free_mem_gb" == "0" ]]; then
free_mem_mb=$(free -m | awk 'NR==2 {print $7}')
free_mem_gb=$(echo "scale=2; $free_mem_mb / 1024" | bc)
fi
echo "Free Memory (RAM): ${free_mem_gb} GB"
echo ""
# Hiển thị thông tin đĩa
echo -e "================= Disk Information =================="
df -h / | awk 'NR==2{print "Total: "$2", Used: "$3", Available: "$4}'
echo ""
# Kiểm tra tốc độ mạng
echo -e "================= Network Information =================="
echo -e "${INFO_ICON} Testing network speed (using speedtest-cli)..."
speedtest-cli --simple 2>&1 > /dev/null
if [ $? -ne 0 ]; then
echo -e "${ERROR_ICON} Network speed test failed. Please check your connection or speedtest-cli installation."
else
speedtest_results=$(speedtest-cli --simple)
echo -e "${SUCCESS_ICON} Speedtest Results: ${speedtest_results}"
fi
echo ""
# Kiểm tra địa chỉ IP và NAT
echo -e "================= IP Address and NAT =================="
ipv4=$(curl -s https://api.ipify.org | head -n 1)
if [ -n "$ipv4" ] && [[ "$ipv4" != "127.0.0.1" ]]; then
echo "IPv4 Address: $ipv4"
else
echo -e "${WARNING_ICON} IPv4 address not found."
ipv4="None"
fi
ipv6=$(ip -6 addr | grep global | awk '{print $2}' | cut -d'/' -f1 | head -n 1)
if [ -n "$ipv6" ]; then
echo "IPv6 Address: $ipv6"
else
echo -e "${WARNING_ICON} IPv6 address not found."
ipv6="None"
fi
check_nat() {
local config_output
if command_exists nft; then
NAT_CONFIG_CMD="nft list ruleset"
NAT_TYPE_CHECK="nft"
else
NAT_CONFIG_CMD="iptables -t nat -L -v"
NAT_TYPE_CHECK="iptables"
fi
config_output=$(eval "$NAT_CONFIG_CMD")
if ! echo "$config_output" | grep -q "table ip nat"; then
echo "No NAT"
return
fi
if echo "$config_output" | grep -q "chain postrouting" && echo "$config_output" | grep -q 'masquerade'; then
echo "NAT1"
return
fi
if echo "$config_output" | grep -q "chain postrouting" && echo "$config_output" | grep -q 'snat to'; then
echo "NAT2"
return
fi
if echo "$config_output" | grep -q "chain prerouting" && echo "$config_output" | grep -q 'dnat to'; then
echo "NAT3"
return
fi
echo "No NAT"
}
nat_status=$(check_nat)
echo "NAT Type: $nat_status"
echo ""
# Kiểm tra ảo hóa lồng nhau
echo -e "================= Nested Virtualization Check =================="
if grep -E '(vmx|svm)' /proc/cpuinfo > /dev/null; then
CPU_SUPPORT="true"
echo "${INFO_ICON} CPU supports virtualization (VT-x or AMD-V)."
if lsmod | grep kvm > /dev/null; then
KVM_INSTALLED="true"
echo "${INFO_ICON} KVM modules are loaded."
if [[ -f /sys/module/kvm_intel/parameters/nested ]]; then
NESTED_FILE="/sys/module/kvm_intel/parameters/nested"
KVM_MODULE="kvm_intel"
elif [[ -f /sys/module/kvm_amd/parameters/nested ]]; then
NESTED_FILE="/sys/module/kvm_amd/parameters/nested"
KVM_MODULE="kvm_amd"
fi
if [[ -n "$NESTED_FILE" ]]; then
NESTED_ENABLED=$(cat "$NESTED_FILE")
if [[ "$NESTED_ENABLED" == "Y" ]]; then
echo "${SUCCESS_ICON} Nested virtualization is enabled."
else
echo "${WARNING_ICON} Nested virtualization is not enabled."
fi
else
echo "${WARNING_ICON} Nested virtualization parameter file not found. KVM may not be properly installed."
fi
else
echo "${WARNING_ICON} KVM modules are NOT loaded."
fi
else
CPU_SUPPORT="false"
echo "${ERROR_ICON} CPU does NOT support virtualization. Nested virtualization is not possible."
fi
echo ""
# Lưu kết quả
if [ $SAVE_RESULTS -eq 1 ]; then
echo -e "${INFO_ICON} Saving results to $RESULTS_FILE..."
echo "System Information:" > "$RESULTS_FILE"
echo "Hostname: $(hostname)" >> "$RESULTS_FILE"
echo "Current time: $(date)" >> "$RESULTS_FILE"
echo "Kernel: $(uname -r)" >> "$RESULTS_FILE"
echo "Uptime: $(uptime -p)" >> "$RESULTS_FILE"
echo "" >> "$RESULTS_FILE"
echo "Operating System: $os_name" >> "$RESULTS_FILE"
echo "" >> "$RESULTS_FILE"
echo "CPU Model: $(grep "model name" /proc/cpuinfo | head -n 1 | awk -F: '{print $2}' | sed 's/^ *//;s/ *$//')" >> "$RESULTS_FILE"
echo "Number of cores: $(nproc)" >> "$RESULTS_FILE"
echo "" >> "$RESULTS_FILE"
echo "Total Memory (RAM): ${total_mem_gb} GB" >> "$RESULTS_FILE"
echo "Free Memory (RAM): ${free_mem_gb} GB" >> "$RESULTS_FILE"
echo "" >> "$RESULTS_FILE"
echo "Disk Usage:" >> "$RESULTS_FILE"
df -h / | awk 'NR==2{print "Total: "$2", Used: "$3", Available: "$4}' >> "$RESULTS_FILE"
echo "" >> "$RESULTS_FILE"
echo "Network Speedtest Results: ${speedtest_results}" >> "$RESULTS_FILE"
echo "" >> "$RESULTS_FILE"
echo "IPv4 Address: $ipv4" >> "$RESULTS_FILE"
echo "IPv6 Address: $ipv6" >> "$RESULTS_FILE"
echo "NAT Type: $nat_status" >> "$RESULTS_FILE"
echo "" >> "$RESULTS_FILE"
echo "Nested Virtualization Support: $CPU_SUPPORT" >> "$RESULTS_FILE"
if [ "$CPU_SUPPORT" == "true" ]; then
echo "Nested Virtualization Enabled: $(if [[ "$NESTED_ENABLED" == "Y" ]]; then echo "Yes"; else echo "No"; fi)" >> "$RESULTS_FILE"
fi
echo -e "${SUCCESS_ICON} Results saved to $RESULTS_FILE."
fi
echo -e "\n${SUCCESS_ICON} Check complete."