This repository was archived by the owner on Aug 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·2419 lines (2140 loc) · 80.6 KB
/
init.sh
File metadata and controls
executable file
·2419 lines (2140 loc) · 80.6 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
export LANG=en_US.UTF-8
#异常终止执行函数
trap _exit INT QUIT TERM
#初始化函数
initself() {
selfversion='25.4.29'
datevar=$(date +%Y-%m-%d_%H:%M:%S)
#菜单名称(默认首页)
menuname='首页'
#父级函数名
parentfun=''
ipaddresses=''
gateway=''
nameservers=''
port=''
unport=''
ips=''
#字体颜色定义
_red() {
printf '\033[0;31;31m%b\033[0m' "$1"
echo
}
_green() {
printf '\033[0;31;32m%b\033[0m' "$1"
echo
}
_yellow() {
printf '\033[0;31;33m%b\033[0m' "$1"
echo
}
_blue() {
printf '\033[0;31;36m%b\033[0m' "$1"
echo
}
#分割线
next() {
printf "%-50s\n" "-" | sed 's/\s/-/g'
}
#按任意键继续
waitinput() {
echo
read -n1 -r -p "按任意键继续...(退出 Ctrl+C)"
}
#继续执行函数
nextrun() {
waitinput
#环境变量调用上一次的次菜单
if [[ -n "${FUNCNAME[3]}" ]]; then
#${FUNCNAME[3]}
$(cat /etc/s/lastfun)
else
main
fi
}
#字符跳动 (参数:字符串 间隔时间s,默认为0.1秒)
jumpfun() {
my_string=$1
delay=${2:-0.1}
# 循环输出每个字符
for ((i = 0; i < ${#my_string}; i++)); do
printf '\033[0;31;36m%b\033[0m' "${my_string:$i:1}"
sleep "$delay"
done
echo
}
# 加载动画 loading $! wait # 等待所有子进程完成
loading() {
local pids=("$@")
local delay=0.1
local spinstr='|/-\'
tput civis # 隐藏光标
while :; do
local all_done=true
for pid in "${pids[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
all_done=false
local temp=${spinstr#?}
printf "\r\033[0;31;36m[ %c ] 正在执行 ...\033[0m" "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
fi
done
[[ $all_done == true ]] && break
done
tput cnorm # 恢复光标
printf "\r\033[K" # 清除行
}
#logo
secho() {
echo
_green ' ________ '
_green ' |\ ____\ '
_green ' \ \ \___|_ '
_green ' \ \_____ \ '
_green ' \|____|\ \ '
_green ' ____\_\ \ '
_green ' |\_________\ '
_green ' \|_________| '
echo
}
#s日志读写
slog() {
local method=$1 #set or get
local file=$2
local info=$3
#检查是否已安装
if _exists 'init.sh'; then
case $method in
set) #写入#
echo $info >>/etc/s/$file.log
;;
get) #读取#
cat /etc/s/$file.log
;;
*)
echo 'log error'
;;
esac
fi
}
#安装脚本
selfinstall() {
menutop
jumpfun "welcome" 0.06
echo
if _exists 's'; then
_red '检测到已存在s程序,位置:/bin/s 请检查!'
exit
else
menuname='首页'
_blue '开始安装 '
echo
cp -f "$(pwd)/init.sh" /bin/init.sh
ln -s /bin/init.sh /bin/s
if [ -d '/etc/s' ]; then
echo "检测到 /etc/s 存在 安装更新..."
#写入日志
slog set install "$datevar | 安装更新 | v$selfversion"
else
mkdir /etc/s
#写入日志
slog set install "$datevar | 脚本全新安装 | v$selfversion"
fi
touch /etc/s/lastfun
touch /etc/s/run.log
secho
_blue '成功安装'
echo
echo '文件释放位置: /bin/init.sh /bin/s /etc/s/'
echo
echo "提示:再次执行任意位置键入 's' "
echo
echo
waitinput
clear
fi
}
#卸载脚本
removeself() {
#写入日志
slog set install "$datevar | 脚本卸载 | v$selfversion"
rm -rf /bin/init.sh
rm -rf /bin/s
rm -rf /etc/s
rm -rf init.sh
}
#脚本升级
updateself() {
_blue '下载github最新版'
wget -N http://raw.githubusercontent.com/sshpc/s/main/s.sh
# 检查上一条命令的退出状态码
if [ $? -eq 0 ]; then
_blue '卸载旧版...'
removeself
chmod +x s.sh && sudo ./s.sh
else
_red "下载失败,请重试"
fi
}
#菜单头部
menutop() {
clear
_yellow '本仓库已不再维护 !!! 请再次升级 !!!'
_green '# Author:SSHPC <https://github.com/sshpc>'
echo
_blue ">~~~~~~~~~~~~~~ Ubuntu tools 脚本工具 ~~~~~~~~~~~~< v: $selfversion"
echo
_yellow "当前菜单: $menuname "
echo
}
#菜单渲染
menu() {
menutop
options=("$@")
num_options=${#options[@]}
# 计算数组中的字符最大长度
max_len=0
for ((i = 0; i < num_options; i++)); do
# 获取当前字符串的长度
str_len=${#options[i]}
# 更新最大长度
if ((str_len > max_len)); then
max_len=$str_len
fi
done
# 渲染菜单
for ((i = 0; i < num_options; i += 4)); do
printf "%s%*s " "$((i / 2 + 1)): ${options[i]}" "$((max_len - ${#options[i]}))"
if [[ "${options[i + 2]}" != "" ]]; then printf "$((i / 2 + 2)): ${options[i + 2]}"; fi
echo
echo
done
echo
printf '\033[0;31;36m%b\033[0m' "q: 退出 "
printf '\033[0;31;36m%b\033[0m' "b: 返回 0: 首页"
echo
echo
# 获取用户输入
read -ep "请输入命令号: " number
if [[ $number -ge 1 && $number -le $((num_options / 2)) ]]; then
#找到函数名索引
action_index=$((2 * (number - 1) + 1))
#函数名赋值
parentfun=${options[action_index]}
#如果/etc/s/run.log存在 则记录日志
if [ -f /etc/s/run.log ]; then
echo "$datevar | $menuname | ${options[action_index]} (${options[action_index - 1]})" >>/etc/s/run.log
fi
#函数执行
${options[action_index]}
#执行完后自动返回
nextrun
elif [[ $number == 0 ]]; then
main
elif [[ $number == 'b' ]]; then
if [[ -n "${FUNCNAME[3]}" ]]; then
${FUNCNAME[3]}
else
main
fi
elif [[ $number == 'q' ]]; then
echo
exit
else
echo
_red '输入有误 回车返回首页'
waitinput
main
fi
}
#获取网卡
getnetcard() {
# 获取系统中可用的网卡名称
interfaces=$(ifconfig -a | sed -nE 's/^([^[:space:]]+).*$/\1/p')
# 输出供用户选择的网卡名称列表
PS3="请选择网卡名称: "
select interface in $interfaces; do
if [[ -n "$interface" ]]; then
break
fi
done
# 去掉网卡名称后面的冒号,并输出用户选择的网卡名称
interface=$(echo "$interface" | sed 's/://')
echo $interface
}
#异常终止函数
_exit() {
if [ -e "./speedtest-cli/speedtest" ]; then
rm -rf ./speedtest-cli
fi
_red "\nThe script has been terminated.\n"
exit 1
}
#检测命令是否存在
_exists() {
local cmd="$1"
which $cmd >/dev/null 2>&1
local rt=$?
return ${rt}
}
#检测大小
calc_size() {
local raw=$1
local total_size=0
local num=1
local unit="KB"
if ! [[ ${raw} =~ ^[0-9]+$ ]]; then
echo ""
return
fi
if [ "${raw}" -ge 1073741824 ]; then
num=1073741824
unit="TB"
elif [ "${raw}" -ge 1048576 ]; then
num=1048576
unit="GB"
elif [ "${raw}" -ge 1024 ]; then
num=1024
unit="MB"
elif [ "${raw}" -eq 0 ]; then
echo "${total_size}"
return
fi
total_size=$(awk 'BEGIN{printf "%.1f", '"$raw"' / '$num'}')
echo "${total_size} ${unit}"
}
to_kibyte() {
local raw=$1
awk 'BEGIN{printf "%.0f", '"$raw"' / 1024}'
}
calc_sum() {
local arr=("$@")
local s
s=0
for i in "${arr[@]}"; do
s=$((s + i))
done
echo ${s}
}
#获取操作系统的信息
get_opsy() {
[ -f /etc/redhat-release ] && awk '{print $0}' /etc/redhat-release && return
[ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
[ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}
clear
}
#软件
software() {
#更新所有已安装的软件包
aptupdatefun() {
_blue "更新所有软件包"
dpkg --configure -a
if [[ -n $(pgrep -f "apt") ]]; then
pgrep -f apt | xargs kill -9
fi
apt-get update -y && apt-get install curl -y
_blue "更新完成"
}
#修复更新
configureaptfun() {
sudo killall apt apt-get
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock*
sudo rm /var/lib/apt/lists/lock
sudo dpkg --configure -a
sudo apt update
}
#安装常用包
installcomso() {
echo "开始安装.."
install_package() {
package_name=$1
echo "开始安装 $package_name"
apt install $package_name -y
echo "$package_name 安装完成"
}
packages=(
"wget"
"curl"
"net-tools"
"vim"
"openssh-server"
"screen"
"git"
"zip"
"htop"
)
for package in "${packages[@]}"; do
package_name="${package%:*}"
install_package "$package_name"
done
echo "所有包都已安装完成"
}
#换源
changemirrors() {
cnmainland() {
bash <(curl -sSL https://linuxmirrors.cn/main.sh)
}
overseas() {
bash <(curl -sSL https://raw.githubusercontent.com/SuperManito/LinuxMirrors/main/ChangeMirrors.sh) --abroad
}
menuname='首页/软件/换源'
options=("大陆" cnmainland "海外" overseas)
menu "${options[@]}"
if [ -f /etc/apt/sources.list.bak ]; then
mv /etc/apt/sources.list.bak "/etc/apt/sources.list.bak.$datevar"
echo "sources.list.bak 已重命名为 /etc/apt/sources.list.bak.$datevar"
fi
}
#安装xray八合一
installbaheyi() {
wget -P /root -N --no-check-certificate "https://raw.githubusercontent.com/mack-a/v2ray-agent/master/install.sh" && chmod 700 /root/install.sh && /root/install.sh
vasma
}
#安装xui
installxui() {
bash <(curl -Ls https://raw.githubusercontent.com/vaxilu/x-ui/master/install.sh)
}
#安装openvpn
installopenvpn() {
_blue '即将下载sh脚本到当前目录,安装后记得修改/etc/openvpn/client-template.txt 文件路由规则'
waitinput
curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh && chmod +x openvpn-install.sh && ./openvpn-install.sh
}
removefun() {
#专项卸载
removephp() {
masterremove php
}
removenginx() {
apt-get --purge remove nginx-common -y
apt-get --purge remove nginx-core -y
masterremove nginx
}
removeapache() {
apt-get --purge remove apache2-common -y
apt-get --purge remove apache2-utils -y
masterremove apache2
}
removedocker() {
docker kill $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
apt-get autoremove docker docker-ce docker-engine docker.io containerd runc
apt-get autoremove docker-ce-*
rm -rf /etc/systemd/system/docker.service.d
rm -rf /var/lib/docker
rm -rf /etc/docker
rm -rf /run/docker
rm -rf /var/lib/dockershim
umount /var/lib/docker/devicemapper
masterremove docker
}
removev2() {
masterremove v2ray
}
removemysql() {
apt-get remove mysql-common -y
apt-get remove dbconfig-mysql -y
apt-get remove mysql-client -y
apt-get remove mysql-client-5.7 -y
apt-get remove mysql-client-core-5.7 -y
apt-get remove apparmor -y
apt-get autoremove mysql* --purge -y
masterremove mysql-server
}
#彻底卸载
masterremove() {
if [ $# -eq 0 ]; then
read -ep "请输入要卸载的软件名: " resoftname
else
resoftname=$1
fi
_red "注意:将会删除关于 $resoftname 所有内容"
waitinput
_red "开始卸载 $resoftname"
echo "关闭服务.."
service $resoftname stop
systemctl stop $resoftname
apt remove $resoftname -y
apt-get --purge remove $resoftname -y
apt-get --purge remove $resoftname-* -y
echo "清除dept列表"
apt purge $(dpkg -l | grep $resoftname | awk '{print $2}' | tr "\n" " ")
echo "删除 $resoftname 的启动脚本"
update-rc.d -f $resoftname remove
echo "删除所有包含 $resoftname 的文件"
rm -rf /etc/$resoftname
rm -rf /etc/init.d/$resoftname
find /etc -name *$resoftname* -print0 | xargs -0 rm -rf
rm -rf /usr/bin/$resoftname
rm -rf /var/log/$resoftname
rm -rf /lib/systemd/system/$resoftname.service
rm -rf /var/lib/$resoftname
rm -rf /run/$resoftname
next
echo
_blue "卸载完成"
}
menuname='首页/软件/卸载'
options=("手动输入" masterremove "卸载nginx" removenginx "卸载Apache" removeapache "卸载php" removephp "卸载docker" removedocker "卸载v2ray" removev2 "卸载mysql" removemysql)
menu "${options[@]}"
}
installbtop() {
apt install snap -y
apt install snapd -y
snap install btop
btop
}
installaapanel() {
local URL=https://www.aapanel.com/script/install_6.0_en.sh && if [ -f /usr/bin/curl ]; then curl -ksSO "$URL"; else wget --no-check-certificate -O install_6.0_en.sh "$URL"; fi
bash install_6.0_en.sh aapanel
}
installrustdeskserver() {
wget -N http://raw.githubusercontent.com/sshpc/rustdesktool/main/rustdesktool.sh && chmod +x ./rustdesktool.sh && ./rustdesktool.sh
}
snapfun() {
snapls() {
echo
_blue version:
echo
snap version
echo
_blue list:
echo
snap list
}
installsnapfun() {
apt install snap -y
apt install snapd -y
}
menuname='首页/软件/snap管理'
options=("查看 snap 状态" snapls "安装" installsnapfun)
menu "${options[@]}"
}
dockersnapinstall() {
apt install snap snapd
snap install docker
}
dockerinstall() {
bash <(curl -sSL https://gitee.com/SuperManito/LinuxMirrors/raw/main/DockerInstallation.sh)
apt install docker-compose -y
}
menuname='首页/软件'
echo "software" >/etc/s/lastfun
options=("aptupdate软件更新" aptupdatefun "修复更新" configureaptfun "换软件源" changemirrors "snap管理" snapfun "软件卸载" removefun "安装常用包" installcomso 安装docker dockerinstall "安装snap版docker" dockersnapinstall "安装btop" installbtop "安装八合一" installbaheyi "安装xui" installxui "安装openvpn" installopenvpn "安装aapanel" installaapanel "安装RustDesk-Server" installrustdeskserver)
menu "${options[@]}"
}
#网络
networktools() {
#ufw防火墙
ufwfun() {
ufwopen() {
if _exists 'ufw'; then
echo "ufw 已安装"
else
echo "ufw 未安装,正在安装..."
apt install ufw -y
echo "ufw 已安装"
fi
echo "请输入y以开启ufw"
ufw enable
echo "ufw已开启"
}
ufwdefault() {
ufw allow 22
echo "已配置允许 22 端口"
ufw default deny
echo "拒绝全部传入"
ufwstatus
}
ufwadd() {
read -ep "请输入端口号 (0-65535): " port
until [[ -n "$port" || "$port" =~ ^[0-9]+$ && "$port" -le 65535 ]]; do
echo "$port: 无效端口."
read -ep "请输入端口号 (0-65535): " port
done
ufw allow $port
echo "端口 $port 已放行"
ufwstatus
}
ufwstatus() {
ufw status verbose
echo "提示:inactive 关闭状态 , active 开启状态"
}
ufwclose() {
read -ep "请输入端口号 (0-65535): " unport
until [[ -n "$unport" || "$unport" =~ ^[0-9]+$ && "$unport" -le 65535 ]]; do
echo "$unport: 无效端口."
read -ep "请输入端口号 (0-65535): " unport
done
ufw delete allow $unport
echo "端口 $unport 已关闭"
ufwstatus
}
ufwdisablefun() {
ufw disable
echo "ufw已关闭"
ufwstatus
}
ufwlogtail() {
tail -f /var/log/ufw.log
}
setufwfromip() {
# 函数:允许特定 IP 和端口的入站流量
allow_ip_port() {
read -ep "请输入ip: " ip
read -ep "请输入端口号 (0-65535): " unport
until [[ -n "$unport" || "$unport" =~ ^[0-9]+$ && "$unport" -le 65535 ]]; do
echo "$unport: 无效端口."
read -ep "请输入端口号 (0-65535): " unport
done
echo "ip:$ip 端口:$unport"
waitinput
ufw allow from $ip to any port $unport
_blue 'ok'
ufwstatus
}
# 函数:拒绝特定 IP 和端口的入站流量
deny_ip_port() {
read -ep "请输入ip: " ip
read -ep "请输入端口号 (0-65535): " unport
until [[ -n "$unport" || "$unport" =~ ^[0-9]+$ && "$unport" -le 65535 ]]; do
echo "$unport: 无效端口."
read -ep "请输入端口号 (0-65535): " unport
done
echo "ip:$ip 端口:$unport"
waitinput
ufw deny from $ip to any port $unport
_blue 'ok'
ufwstatus
}
menuname='首页/网络/ufw/特定ip操作'
options=("允许特定IP和端口的入站流量" allow_ip_port "拒绝特定IP和端口的入站流量" deny_ip_port)
menu "${options[@]}"
}
menuname='首页/网络/ufw'
options=("开启ufw" ufwopen "关闭ufw" ufwdisablefun "ufw默认配置仅ssh" ufwdefault "ufw状态" ufwstatus "查看实时日志" ufwlogtail "添加端口" ufwadd "关闭端口" ufwclose "对特定ip操作" setufwfromip)
menu "${options[@]}"
}
fail2banfun() {
fail2banstatusfun() {
fail2ban-client status sshd
}
installfail2ban() {
echo "检查并安装fail2ban"
apt install fail2ban -y
echo "fail2ban 已安装"
echo "开始配置fail2ban"
waitinput
read -ep "请输入尝试次数 (直接回车默认4次): " retry
read -ep "请输入拦截后禁止访问的时间 (直接回车默认604800s): " timeban
if [[ "$retry" = "" ]]; then
retry=4
fi
if [[ "$timeban" = "" ]]; then
timeban=604800
fi
rm /etc/fail2ban/jail.d/sshd.local
echo "[ssh-iptables]" >>/etc/fail2ban/jail.d/sshd.local
echo "enabled = true" >>/etc/fail2ban/jail.d/sshd.local
echo "filter = sshd" >>/etc/fail2ban/jail.d/sshd.local
echo "action = iptables[name=SSH, port=ssh, protocol=tcp]" >>/etc/fail2ban/jail.d/sshd.local
echo "logpath = /var/log/auth.log" >>/etc/fail2ban/jail.d/sshd.local
echo "maxretry = $retry" >>/etc/fail2ban/jail.d/sshd.local
echo "bantime = $timeban" >>/etc/fail2ban/jail.d/sshd.local
service fail2ban start
echo "服务已开启"
echo
echo "----服务状态----"
fail2banstatusfun
}
menuname='首页/网络/fail2ban'
options=("安装配置sshd" installfail2ban "查看状态" fail2banstatusfun)
menu "${options[@]}"
}
#网络信息
netinfo() {
echo
_blue "--本机IP--"
ifconfig -a | grep "inet "
_blue "--路由表--"
route -n
_blue "--监听端口--"
netstat -tunlp
_blue "--test IPv4/IPv6..."
[[ -n ${local_curl} ]] && ip_check_cmd="curl -s -m 4" || ip_check_cmd="wget -qO- -T 4"
ipv4_check=$( (ping -4 -c 1 -W 4 ipv4.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -4 icanhazip.com 2>/dev/null)
ipv6_check=$( (ping -6 -c 1 -W 4 ipv6.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -6 icanhazip.com 2>/dev/null)
if [[ -z "$ipv4_check" && -z "$ipv6_check" ]]; then
_yellow "Warning: Both IPv4 and IPv6 connectivity were not detected.\n"
fi
[[ -z "$ipv4_check" ]] && online="$(_red "Offline")" || online="$(_green "Online")"
[[ -z "$ipv6_check" ]] && online+=" / $(_red "Offline")" || online+=" / $(_green "Online")"
echo "IPv4/IPv6 : $online"
_blue "--公网IP--"
curl cip.cc
echo
_blue "--ip地区--"
local org city country region
org="$(wget -q -T10 -O- ipinfo.io/org)"
city="$(wget -q -T10 -O- ipinfo.io/city)"
country="$(wget -q -T10 -O- ipinfo.io/country)"
region="$(wget -q -T10 -O- ipinfo.io/region)"
if [[ -n "${org}" ]]; then
echo "Organization : $(_blue "${org}")"
fi
if [[ -n "${city}" && -n "${country}" ]]; then
echo "Location : $(_blue "${city} / ${country}")"
fi
if [[ -n "${region}" ]]; then
echo "Region : $(_yellow "${region}")"
fi
if [[ -z "${org}" ]]; then
echo "Region : $(_red "No ISP detected")"
fi
_blue "--IP连接数--"
waitinput
echo ' 数量 ip'
netstat -na | grep ESTABLISHED | awk '{print$5}' | awk -F : '{print$1}' | sort | uniq -c | sort -r
echo
_blue "--ssh失败记录--"
waitinput
lastb | grep root | awk '{print $3}' | sort | uniq
echo
}
#iperf3打流
iperftest() {
if _exists 'iperf3'; then
echo "iperf3 已安装"
else
echo "iperf3 未安装,正在安装..."
apt install iperf3 -y
fi
iperf3client() {
until [[ "$serversip" ]]; do
read -ep "请输入服务器ip: " serversip
done
_blue '默认udp 手动执行'
next
_yellow "iperf3 -u -c $serversip -b 2000M -t 40"
next
iperf3 -u -c $serversip -b 2000M -t 40
}
echo "请选择运行模式 1.服务端 2.客户端"
until [[ $PROTOCOL_CHOICE =~ ^[1-2]$ ]]; do
read -rp "Protocol [1-2]: " PROTOCOL_CHOICE
done
case $PROTOCOL_CHOICE in
1)
_blue '端口为 5201 请放行端口'
iperf3 -s
;;
2)
iperf3client
;;
esac
}
#nmap扫描
nmapfun() {
if _exists 'nmap'; then
echo "nmap 已安装"
else
echo "nmap 未安装,正在安装..."
apt install nmap -y
fi
nmapdetection() {
echo '本地网络:'
ip addr show | grep "inet " | grep -v "127.0.0.1"
echo
until [[ -n "$ips" ]]; do
read -ep "请输入网段x.x.x.x/x: " ips
done
nmap -sP $ips
}
nmapportcat() {
read -ep "请输入ip: " ip
read -ep "请输入端口(1-65535): " port
nmap "$ip" -p "$port" -Pn
}
echo "1.主机探测 2.端口扫描"
until [[ $PROTOCOL_CHOICE =~ ^[1-2]$ ]]; do
read -rp "Protocol [1-2]: " PROTOCOL_CHOICE
done
case $PROTOCOL_CHOICE in
1)
_blue '扫描网段中有哪些主机在线,本质上是Ping扫描'
nmapdetection
;;
2)
_blue '示例 nmap ip -p 1-2000 -Pn'
nmapportcat
;;
esac
}
#实时网速
Realtimenetworkspeedfun() {
if _exists 'bmon'; then
bmon
else
echo "bmon 未安装,正在安装..."
apt-get install bmon -y
bmon
fi
}
#外网测速
publicnettest() {
netfast() {
apt install speedtest-cli -y
echo "开始测速"
speedtest-cli
echo "测速完成"
}
#SpeedCLI 测速
netfast2() {
echo "开始测速"
curl -fsSL git.io/speedtest-cli.sh | sudo bash
speedtest
echo "测速完成"
}
#三网测速
sanwang() {
bash <(curl -Lso- https://down.wangchao.info/sh/superspeed.sh)
}
#多地区测速
netfast3() {
if [ ! -e "./speedtest-cli/speedtest" ]; then
sys_bit=""
local sysarch
sysarch="$(uname -m)"
if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
sysarch="$(arch)"
fi
if [ "${sysarch}" = "x86_64" ]; then
sys_bit="x86_64"
fi
if [ "${sysarch}" = "i386" ] || [ "${sysarch}" = "i686" ]; then
sys_bit="i386"
fi
if [ "${sysarch}" = "armv8" ] || [ "${sysarch}" = "armv8l" ] || [ "${sysarch}" = "aarch64" ] || [ "${sysarch}" = "arm64" ]; then
sys_bit="aarch64"
fi
if [ "${sysarch}" = "armv7" ] || [ "${sysarch}" = "armv7l" ]; then
sys_bit="armhf"
fi
if [ "${sysarch}" = "armv6" ]; then
sys_bit="armel"
fi
[ -z "${sys_bit}" ] && _red "Error: Unsupported system architecture (${sysarch}).\n" && exit 1
url1="https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
url2="https://dl.lamp.sh/files/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}; then
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}; then
_red "Error: Failed to download speedtest-cli.\n" && exit 1
fi
fi
mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
rm -f speedtest.tgz
fi
printf "%-18s%-18s%-20s%-12s\n" " Node Name" "Upload Speed" "Download Speed" "Latency"
speed_test() {
local nodeName="$2"
if [ -z "$1" ]; then
./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
else
./speedtest-cli/speedtest --progress=no --server-id="$1" --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
fi
if [ $? -eq 0 ]; then
local dl_speed up_speed latency
dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
latency=$(awk '/Latency/{print $3" "$4}' ./speedtest-cli/speedtest.log)
if [[ -n "${dl_speed}" && -n "${up_speed}" && -n "${latency}" ]]; then
printf "\033[0;33m%-18s\033[0;32m%-18s\033[0;31m%-20s\033[0;36m%-12s\033[0m\n" " ${nodeName}" "${up_speed}" "${dl_speed}" "${latency}"
fi
fi
}
speed_test '' 'Speedtest.net'
speed_test '21541' 'Los Angeles, US'
speed_test '43860' 'Dallas, US'
speed_test '40879' 'Montreal, CA'
speed_test '24215' 'Paris, FR'
speed_test '28922' 'Amsterdam, NL'
speed_test '24447' 'Shanghai, CN'
speed_test '5530' 'Chongqing, CN'
speed_test '60572' 'Guangzhou, CN'
speed_test '32155' 'Hongkong, CN'
speed_test '23647' 'Mumbai, IN'
speed_test '13623' 'Singapore, SG'
speed_test '21569' 'Tokyo, JP'
rm -rf speedtest-cli
}
menuname='首页/网络/外网测速'
options=("测速1" netfast "测速2-SpeedCLI" netfast2 "多地区测速" netfast3 "三网测速" sanwang)
menu "${options[@]}"
}
#配置局域网ip
lanfun() {
staticip() {
echo "备份原文件/etc/netplan/00-installer-config.yaml"
cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak."$datevar"
#获取网卡名称
ens=$(getnetcard)
until [[ -n "$ipaddresses" ]]; do
read -ep "请输入ip地址+网络号 (x.x.x.x/x): " ipaddresses
done
until [[ -n "$gateway" ]]; do
read -ep "请输入网关(x.x.x.x): " gateway
done
until [[ -n "$nameservers" ]]; do