-
Notifications
You must be signed in to change notification settings - Fork 0
/
mahimana.sh
828 lines (779 loc) · 27 KB
/
mahimana.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
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
#! /bin/bash
# Color codes
# Reset
NC='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Bold
BBlack='\033[1;30m' # Black
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
BYellow='\033[1;33m' # Yellow
BBlue='\033[1;34m' # Blue
BPurple='\033[1;35m' # Purple
BCyan='\033[1;36m' # Cyan
BWhite='\033[1;37m' # White
# Underline
UBlack='\033[4;30m' # Black
URed='\033[4;31m' # Red
UGreen='\033[4;32m' # Green
UYellow='\033[4;33m' # Yellow
UBlue='\033[4;34m' # Blue
UPurple='\033[4;35m' # Purple
UCyan='\033[4;36m' # Cyan
UWhite='\033[4;37m' # White
# Background
On_Black='\033[40m' # Black
On_Red='\033[41m' # Red
On_Green='\033[42m' # Green
On_Yellow='\033[43m' # Yellow
On_Blue='\033[44m' # Blue
On_Purple='\033[45m' # Purple
On_Cyan='\033[46m' # Cyan
On_White='\033[47m' # White
# High Intensity
IBlack='\033[0;90m' # Black
IRed='\033[0;91m' # Red
IGreen='\033[0;92m' # Green
IYellow='\033[0;93m' # Yellow
IBlue='\033[0;94m' # Blue
IPurple='\033[0;95m' # Purple
ICyan='\033[0;96m' # Cyan
IWhite='\033[0;97m' # White
# Bold High Intensity
BIBlack='\033[1;90m' # Black
BIRed='\033[1;91m' # Red
BIGreen='\033[1;92m' # Green
BIYellow='\033[1;93m' # Yellow
BIBlue='\033[1;94m' # Blue
BIPurple='\033[1;95m' # Purple
BICyan='\033[1;96m' # Cyan
BIWhite='\033[1;97m' # White
# High Intensity backgrounds
On_IBlack='\033[0;100m' # Black
On_IRed='\033[0;101m' # Red
On_IGreen='\033[0;102m' # Green
On_IYellow='\033[0;103m' # Yellow
On_IBlue='\033[0;104m' # Blue
On_IPurple='\033[0;105m' # Purple
On_ICyan='\033[0;106m' # Cyan
On_IWhite='\033[0;107m' # White
# Check if the script is being run as root
if [[ $EUID -ne 0 ]]; then
echo -e "${Red}This script must be run as root!${NC}"
exit 1
fi
# Define a function to display my logo
displayLogo() {
printf "${BIGreen}"
cat << "EOF"
__ __ _ _ __ __
| \/ | | | (_) \/ |
| \ / | __ _| |__ _| \ / | __ _ _ __ __ _
| |\/| |/ _` | '_ \| | |\/| |/ _` | '_ \ / _` |
| | | | (_| | | | | | | | | (_| | | | | (_| |
|_| |_|\__,_|_| |_|_|_| |_|\__,_|_| |_|\__,_|
EOF
printf "${NC}"
}
# Update and upgrade the system
updateAndUpgrade() {
printf "${Blue} 🚀 Starting Update and upgrade the system ... ${NC} \n";
{
apt-get update > /dev/null 2>&1 && apt-get upgrade -y > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Update and Upgrade the system is complete ${NC} \n";
# wait 5 secound
sleep 5;
main;
} || {
printf "${RED}❌ An error occurred while updating the system ${NC}\n";
exit 1;
}
}
# Find SSH port and return
FindSSHPort() {
# Check if ssh is installed
if command -v ssh &> /dev/null; then
# Check if sshd_config file not exists
if [ ! -f /etc/ssh/sshd_config ]; then
echo "SSH is not installed";
exit 1;
fi
# Find port in sshd_config
port=$(grep "#\?Port" /etc/ssh/sshd_config | head -1 | awk '{print $2}')
echo "Current port is $port";
exit 0;
else
echo "SSH is not installed";
exit 1;
fi
}
# Change SSh port
changeSSHPort() {
{
# Check ssh and sshd is installed
dpkg -s ssh > /dev/null 2>&1 || {
printf "${Blue} 🚀 Starting Install SSH ... ${NC} \n";
apt-get install -y ssh > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Install SSH is complete ${NC} \n";
}
read -p "Enter the new SSH port: " new_port
printf "${Blue} 🚀 Starting Change SSH port ... ${NC} \n";
# Find old port in sshd_config
old_port=$(grep "#\?Port" /etc/ssh/sshd_config | head -1 | awk '{print $2}')
# Replace old port with new port in sshd_config
sed -i -E "s/^#?Port\s+[0-9]+$/Port ${new_port}/" /etc/ssh/sshd_config
printf "${Green} 🎉 Change SSH port is complete ${NC} \n";
service ssh restart > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 SSH service is restarted ${NC} \n";
printf "${Green} 🎉 SSH port is changed to $new_port ${NC} \n";
# check if ufw is installed
if command -v ufw &> /dev/null; then
printf "${Blue} 🚀 Starting Close Firewall for old port... ${NC} \n";
ufw deny $old_port > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Firewall is closed sucessfully ${NC} \n";
printf "${Blue} 🚀 Starting Open Firewall ... ${NC} \n";
ufw allow $new_port > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Firewall is opened sucessfully ${NC} \n";
fi
# wait 5 secound
sleep 5;
main;
} || {
printf "${RED}❌ An error occurred while changing SSH port ${NC}\n";
exit 1;
}
}
# Bind a domain to server by bind9
BindDomain() {
# If Bind9 not installed then install
if ! command -v bind &> /dev/null; then
printf "${Blue} 🚀 Installing bind9 ... ${NC} \n";
apt-get install -y bind9 > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Install bind9 is complete ${NC} \n";
fi
# Starting bind 9
printf "${Blue} 🚀 Starting Bind9 ... ${NC} \n";
systemctl start bind9 > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Bind9 is running ${NC} \n";
# Enabling bind 9
printf "${Blue} 🚀 Enabling Bind9 ... ${NC} \n";
systemctl enable bind9 > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Bind9 is enabled ${NC} \n";
# get domain
read -p "Enter the domain: " domain
domainWithoutExtension="${domain%.*}"
# Set the domain in /etc/bind/named.conf.local
printf "${Blue} 🚀 Setting the domain... ${NC} \n";
namedConfLocal="include "/etc/bind/zone/${domain}/${domainWithoutExtension}.conf";";
echo "$namedConfLocal" > /etc/bind/named.conf.local;
namedConfLocalSave="include "/etc/bind/zone/${domain}/${domainWithoutExtension}.conf";";
echo "$namedConfLocalSave" > /etc/bind/named.conf.local.save;
# make directory zone and make directory with name of domain
# Check if directory exists
if [ -d /etc/bind/zone ]; then
rm -r /etc/bind/zone > /dev/null 2>&1 & spinner;
fi
mkdir -p /etc/bind/zone > /dev/null 2>&1 & spinner;
mkdir -p /etc/bind/zone/${domain} > /dev/null 2>&1 & spinner;
#create file with name domain without extension
touch /etc/bind/zone/${domain}/${domainWithoutExtension}.conf;
# Get Ip
ip=$(ip -4 addr | sed -ne 's|^.* inet \([^/]*\)/.* scope global.*$|\1|p' | head -1);
reverseIp=$(echo "$ip" | awk -F '.' '{print $4 "." $3 "." $2 "." $1}');
reverseIpWithoutLastDot=$(echo $reverseIp | sed 's/\.[^.]*$//')
reverseIpLastDot=$(echo $reverseIp | awk -F'.' '{print $4}')
domainWithoutExtensionConf="
zone "${domain}" {
type master;
file "/etc/bind/zone/${domain}/db.${domainWithoutExtension}";
};
zone "${reverseIpWithoutLastDot}.in-addr.arpa" {
type master;
file "/etc/bind/zone/${domain}/db.${reverseIpLastDot}";
};
"
echo "$domainWithoutExtensionConf" > /etc/bind/zone/${domain}/${domainWithoutExtension}.conf;
# Create db.${reverseIpLastDot}
touch /etc/bind/zone/${domain}/db.${reverseIpLastDot};
reverseIpLastDotDb=";
; BIND reverse data file for local loopback interface
;
\$TTL 604800
@ IN SOA ${domain}. root.${domain}. (
1 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS ${domain}.
200 IN PTR ${domain}.
";
echo "$reverseIpLastDotDb" > /etc/bind/zone/${domain}/db.${reverseIpLastDot};
# Create db.${domainWithoutExtension}
touch /etc/bind/zone/${domain}/db.${domainWithoutExtension};
dbDotDomainWithoutExtension=";
; BIND data file for local loopback interface
;
\$TTL 604800
@ IN SOA ${domain}. root.${domain}. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS ${domain}.
@ IN A ${ip}
@ IN PTR ${domain}.
";
echo "$dbDotDomainWithoutExtension" > /etc/bind/zone/${domain}/db.${domainWithoutExtension};
# Restart bind9
printf "${Blue} 🚀 Restarting Bind9 ... ${NC} \n";
systemctl restart bind9 > /dev/null 2>&1;
rndc reload > /dev/null 2>&1;
printf "${Green} 🎉 Bind9 is restarted ${NC} \n";
# check domain is bind or not
printf "${Blue} 🚀 Checking domain is bind or not ... ${NC} \n";
# Check if dig is installed
dpkg -s dnsutils > /dev/null 2>&1 || {
printf "${Blue} 🚀 Installing dig ... ${NC} \n";
apt-get install -y dnsutils > /dev/null 2>&1;
printf "${Green} 🎉 dig is installed ${NC} \n";
}
#Get First ip of domain with dig
digIp=$(dig +short $domain | head -1)
if [ "$ip" = "$digIp" ]; then
printf "${Green} 🎉 Domain is binded successfully ${NC} \n";
else
printf "${Red} ❌ Domain is not binded ${NC} \n";
exit 1;
fi
# wait 5 secound
sleep 5;
main;
}
#Remove a domain
RemoveDomain() {
read -p "Enter the domain name: " domain
# Check if domain is binded
printf "${Blue} 🚀 Checking domain is binded or not ... ${NC} \n";
# Check if dig is installed
dpkg -s dnsutils > /dev/null 2>&1 || {
printf "${Blue} 🚀 Installing dig ... ${NC} \n";
apt-get install -y dnsutils > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 dig is installed ${NC} \n";
}
# Get Ip
ip=$(ip -4 addr | sed -ne 's|^.* inet \([^/]*\)/.* scope global.*$|\1|p' | head -1);
#Get First ip of domain with dig
digIp=$(dig +short $domain | head -1)
if [ "$ip" = "$digIp" ]; then
printf "${Green} 🎉 Domain is not binded ${NC} \n";
else
domainWithoutExtension=$(echo $domain | cut -d '.' -f1);
# empty file /etc/bind/named.conf.local
echo > /etc/bind/named.conf.local
# empty file /etc/bind/named.conf.local.save
echo > /etc/bind/named.conf.local.save
# empty file /etc/bind/zone/${domain}/db.${domainWithoutExtension}
rm -r /etc/bind/zone/${domain}
# Restart bind9
printf "${Blue} 🚀 Restarting Bind9 ... ${NC} \n";
systemctl restart bind9 > /dev/null 2>&1 & spinner;
rndc reload > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Bind9 is restarted ${NC} \n";
# check domain is bind or not
printf "${Blue} 🚀 Checking domain is bind or not ... ${NC} \n";
#Get First ip of domain with dig
digIp=$(dig +short $domain | head -1)
if [ "$ip" = "$digIp" ]; then
printf "${Green} 🎉 Domain is removed successfully ${NC} \n";
else
printf "${Red} ❌ Domain is not removed ${NC} \n";
exit 1;
fi
# wait 5 secound
sleep 5;
main;
fi
}
spinner() {
local pid=$!
local delay=0.75
local spinstr='|/-\'
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
#Get SSL
getSSL() {
read -p "Enter the domain name: " domain
# Check if certbot is installed
if ! command -v certbot &> /dev/null; then
printf "${Blue} 🚀 Installing certbot ... ${NC} \n";
# Check snap is installed
dpkg -s snapd > /dev/null 2>&1 || {
printf "${Blue} 🚀 Installing snap ... ${NC} \n";
apt-get install -y snapd > /dev/null 2>&1;
printf "${Green} 🎉 snap is installed ${NC} \n";
}
snap install --classic certbot > /dev/null 2>&1;
ln -s /snap/bin/certbot /usr/bin/certbot > /dev/null 2>&1;
printf "${Green} 🎉 certbot is installed ${NC} \n";
fi
# Check if ufw is installed then port 80 and 443 is open
if command -v ufw &> /dev/null; then
ufw allow 80 > /dev/null 2>&1;
ufw allow 443 > /dev/null 2>&1;
fi
printf "${Blue} 🚀 Starting get SSL ... ${NC} \n";
# Check port 80 is not used and open
# Check if lsof is not installed then install
dpkg -s lsof > /dev/null 2>&1 || {
printf "${Blue} 🚀 Installing lsof ... ${NC} \n";
apt-get install -y lsof > /dev/null 2>&1;
printf "${Green} 🎉 lsof is installed ${NC} \n";
}
# if port 80 is used then exit
if lsof -i :80 > /dev/null 2>&1; then
# Get the name of process using port 80
process=$(lsof -i :80 | awk '{print $1}' | tail -n 1)
printf "${Red} ❌ Port 80 is already in use by $process ${NC} \n";
exit 1;
fi
# Get SSL Certificate
certbot certonly --standalone --non-interactive --agree-tos --register-unsafely-without-email -d $domain > /dev/null 2>&1 & spinner;
# Check if certificates are created
if [ -f /etc/letsencrypt/live/$domain/fullchain.pem ]; then
printf "${Green} 🎉 Certificates are created successfully ${NC} \n";
printf "${Green} 💁 Your certificate information is: ${NC} \n";
certbot certificates -d $domain
# wait 5 secound
sleep 5;
main;
else
printf "${Red} ❌ Certificates are not created ${NC} \n";
exit 1;
fi
}
# Install Docker
installDocker() {
# Check if docker is installed or not
if command -v docker &> /dev/null; then
printf "${Green} ✅ Docker is already installed ${NC} \n";
printf "${Green} 💁 Your Docker information is: ${NC} \n";
docker info;
else
printf "${Blue} 🚀 Starting Install Docker ... ${NC} \n";
bash -c "$(curl -sSL https://get.docker.com)" > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Install Docker is complete ${NC} \n";
printf "${Green} 💁 Your Docker information is: ${NC} \n";
docker info;
fi
# wait 5 secound
sleep 5;
main;
}
# Show current hostname
showHostname() {
hostnamectl --static;
}
# Change Hostname
changeHostname() {
read -p "Enter the new hostname: " hostname
hostnamectl set-hostname $hostname
printf "${Green} 🎉 Hostname is changed ${NC} \n";
printf "${Green} 💁 Your Hostname information is: ${NC} \n";
hostnamectl
# wait 5 secound
sleep 5;
main;
}
# Install Nginx
installNginx() {
# Check if nginx is installed or not
if command -v nginx &> /dev/null; then
printf "${Green} ✅ Nginx is already installed ${NC} \n";
else
printf "${Blue} 🚀 Starting Install Nginx ... ${NC} \n";
apt-get install -y nginx > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Install Nginx is complete ${NC} \n";
fi
# Start and enable nginx
systemctl start nginx > /dev/null 2>&1 & spinner;
systemctl enable nginx > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Nginx is running ${NC} \n";
printf "${Green} 💁 Your Nginx information is: ${NC} \n";
nginx -v
# wait 5 secound
sleep 5;
main;
}
# Get SSL for domain with Nginx
getSSLWithNginx() {
# Check if Nginx is installed
if ! command -v nginx &> /dev/null; then
# Install nginx
printf "${Blue} 🚀 Installing Nginx ... ${NC} \n";
apt-get install -y nginx > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Nginx is installed ${NC} \n";
fi
# Check if certbot is installed
if ! command -v certbot &> /dev/null; then
printf "${Blue} 🚀 Installing certbot ... ${NC} \n";
# Check snap is installed
dpkg -s snapd > /dev/null 2>&1 || {
printf "${Blue} 🚀 Installing snap ... ${NC} \n";
apt-get install -y snapd > /dev/null 2>&1;
printf "${Green} 🎉 snap is installed ${NC} \n";
}
snap install --classic certbot > /dev/null 2>&1;
ln -s /snap/bin/certbot /usr/bin/certbot > /dev/null 2>&1;
printf "${Green} 🎉 certbot is installed ${NC} \n";
fi
# Check if ufw is installed then port 80 and 443 is open
if command -v ufw &> /dev/null; then
ufw allow 80 > /dev/null 2>&1;
ufw allow 443 > /dev/null 2>&1;
fi
printf "${Blue} 🚀 Starting get SSL ... ${NC} \n";
# Get domain
read -p "Enter the domain: " domain
# Get SSL
certbot --nginx --non-interactive --agree-tos --register-unsafely-without-email -d $domain > /dev/null 2>&1 & spinner;
# Check if certificates are created
if [ -f /etc/letsencrypt/live/$domain/fullchain.pem ]; then
printf "${Green} 🎉 Certificates are created successfully ${NC} \n";
printf "${Green} 💁 Your certificate information is: ${NC} \n";
certbot certificates -d $domain
# wait 5 secound
sleep 5;
main;
else
printf "${Red} ❌ Certificates are not created ${NC} \n";
exit 1;
fi
}
# Install NVM
installNVM() {
printf "${Blue} 🚀 Starting install NVM ... ${NC} \n";
# Check if nvm is installed
if command -v nvm &> /dev/null; then
printf "${Green} ✅ NVM is already installed ${NC} \n";
printf "${Green} 💁 Your NVM information is: ${NC} \n";
nvm --version;
# wait 5 secound
sleep 5;
main;
fi
# Check if curl is not installed
if ! command -v curl &> /dev/null; then
# Install curl
printf "${Blue} 🚀 Installing curl ... ${NC} \n";
apt-get install -y curl > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 curl is installed ${NC} \n";
fi
bash <(curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh) > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 NVM is installed ${NC} \n";
# Ask for reopen shell
read -p "Do you want to reopen shell? (y/n): " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
# Reopen shell
exec $SHELL
fi
# Show nvm version
printf "${Blue} 💁 Your nvm version is: ${NC} \n";
nvm --version;
# wait 5 secound
sleep 5;
main;
}
# Add SSH key
addSSHKey() {
printf "${Blue} 🚀 Starting add SSH key ... ${NC} \n";
# Check if .ssh folder not exists
if [ ! -d /root/.ssh ]; then
mkdir /root/.ssh
fi
# Check if authorized_keys file not exists
if [ ! -f /root/.ssh/authorized_keys ]; then
touch /root/.ssh/authorized_keys
fi
# Show info about how to create SSH key
printf "${Green} 💁 How to create SSH key ${NC} \n";
printf "${Green} Run: ssh-keygen -t rsa ${NC} \n";
printf "${Green} Run: cat ~/.ssh/id_rsa.pub ${NC} \n";
# Add SSH key
read -p "Enter your SSH key: " -r ssh_key
echo "$ssh_key" >> /root/.ssh/authorized_keys
printf "${Green} 🎉 SSH key is added ${NC} \n";
# Sleep 5 secound
sleep 5;
main;
}
# Show Open ports
showOpenPorts() {
if command -v ufw &> /dev/null; then
message=$(ufw status | grep -q "Status: active" && echo "ufw is enabled" || echo "ufw is disabled");
# Check if message is "ufw is enabled" then show open ports
if [ "$message" == "ufw is enabled" ]; then
# Get all open ports and concat together with - ==> | tr '\n' '-' | sed 's/-$//'
openPorts=$(ufw status | grep "ALLOW" | awk '{print $1}')
printf "${Purple}Open ports:\n$openPorts\n ${NC}";
else
echo "$message";
fi
else
echo "ufw is not installed"
fi
# Ask for back to main
read -p "Do you want to back to main menu? (y/n): " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
main
fi
}
# Check Firewall
CheckFirewall() {
if command -v ufw &> /dev/null; then
message=$(ufw status | grep -q "Status: active" && echo "ufw is enabled" || echo "ufw is disabled");
echo "$message";
else
echo "ufw is not installed"
fi
}
# Change Firewall status
ChangeFirewallStatus() {
if command -v ufw &> /dev/null; then
# Check if ufw is disabled the enable it and if it is enabled then disable it
if ufw status | grep "Status: inactive" > /dev/null 2>&1; then
read -p "Enter the ports that you want to open: (separated by comma)" port;
printf "${Blue} 🚀 Starting Firewall and opening ports ... ${NC} \n";
for p in $(echo $port | sed "s/,/ /g"); do
ufw allow $p > /dev/null 2>&1;
done
ufw enable > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Firewall is started ${NC} \n";
else
printf "${Blue} 🚀 Stopping Firewall ... ${NC} \n";
ufw disable > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Firewall is stopped ${NC} \n";
fi
else
echo "ufw is not installed";
fi
# Sleep 5sec
sleep 5;
main;
}
# Open new port
openNewPort() {
read -p "Enter the new port: " new_port
printf "${Blue} 🚀 Opening port ... ${NC} \n";
ufw allow $new_port > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Firewall is opened sucessfully ${NC} \n";
sleep 5;
main;
}
# Close port
closePort() {
read -p "Enter the port: " port
printf "${Blue} 🚀 Closing port ... ${NC} \n";
ufw deny $port > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Firewall is closed sucessfully ${NC} \n";
sleep 5;
main;
}
# Add user
addUser() {
read -p "Enter the username: " username
stty -echo
read -p "Enter the password: " password
stty echo
printf "\n${Blue} 🚀 Adding user ... ${NC} \n";
adduser --disabled-password --gecos "" $username > /dev/null 2>&1 & spinner;
echo "$username:$password" | chpasswd > /dev/null 2>&1 & spinner;
# Ask to be admin
read -p "Are $username is admin? (y/n): " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
usermod -aG sudo $username
fi
printf "${Green} 🎉 User is added sucessfully ${NC} \n";
sleep 5;
main;
}
changePassword() {
read -p "Enter the username: " username
# Check if user exists
if ! id -u $username > /dev/null 2>&1; then
printf "${RED}❌ User $username does not exist ${NC}\n";
sleep 5;
main;
fi
stty -echo
read -p "Enter the password: " password
stty echo
printf "\n${Blue} 🚀 Changing password ... ${NC} \n";
echo "$username:$password" | chpasswd > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 Password is changed sucessfully ${NC} \n";
sleep 5;
main;
}
# Delete User
deleteUser() {
read -p "Enter the username: " username
printf "${Blue} 🚀 Deleting user ... ${NC} \n";
userdel -r $username > /dev/null 2>&1 & spinner;
printf "${Green} 🎉 User is deleted sucessfully ${NC} \n";
sleep 5;
main;
}
# Install x-ui
installXUI() {
printf "${Blue} 🚀 Starting install x-ui ... ${NC} \n";
bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh)
printf "${Green} 🎉 x-ui is installed ${NC} \n";
sleep 5;
main;
}
InstallHiddify() {
printf "${Blue} 🚀 Starting install Hiddify ... ${NC} \n";
bash <(curl i.hiddify.com/release)
printf "${Green} 🎉 Hiddify is installed ${NC} \n";
sleep 5;
main;
}
InstallMarzban() {
printf "${Blue} 🚀 Starting install Marzban ... ${NC} \n";
bash -c "$(curl -sL https://github.com/Gozargah/Marzban-scripts/raw/master/marzban.sh)" @ install
printf "${Green} 🎉 Marzban is installed ${NC} \n";
sleep 5;
main;
}
TorSina() {
bash <(curl -Ls https://raw.githubusercontent.com/sinasims/torsina/refs/heads/main/install.sh)
}
# Main
main() {
clear
displayLogo
printf "${Yellow}Welcome to MahiMana script - a server tool!${NC}\n"
printf "${Yellow}-------------------------------------------${NC}\n"
printf "${Yellow}Choose an option:${NC}\n"
printf "${Cyan}1. Update and upgrade the system${NC}\n"
printf "${Cyan}2. Change SSH port ${Purple}($(FindSSHPort)) ${Red}[Server]${NC}\n"
printf "${Cyan}3. Bind a domain ${Blue}(bind9) ${Red}[Server]${NC}\n"
printf "${Cyan}4. Remove a domain ${Blue}(bind9) ${Red}[Server]${NC}\n"
printf "${Cyan}5. Get single SSL certificate for a domain ${Red}[Server]${NC}\n"
printf "${Cyan}6. Install Docker${NC}\n"
printf "${Cyan}7. Change Hostname ${Purple} ($(showHostname)) ${Red}[Server]${NC}\n"
printf "${Cyan}8. Install Nginx ${Red}[Server]${NC}\n"
printf "${Cyan}9. Get SSL for domain with Nginx ${Red}[Server]${NC}\n"
printf "${Cyan}10. Install NVM (Node Version Manager)${NC}\n"
printf "${Cyan}11. Add SSH Key ${Red}[Server]${NC}\n"
printf "${Cyan}12. Active or deactive Firewall ${Purple}($(CheckFirewall))${NC}\n"
printf "${Cyan}13. Show all Firewall open ports${NC}\n"
printf "${Cyan}14. Open new port Firewall${NC}\n"
printf "${Cyan}15. Close port Firewall${NC}\n"
printf "${Cyan}16. Add new OS user${NC}\n"
printf "${Cyan}17. Change Password for OS user${NC}\n"
printf "${Cyan}18. Delete OS user${NC}\n"
printf "${Cyan}19. Install x-ui${NC}\n"
printf "${Cyan}20. Install Hiddify${NC}\n"
printf "${Cyan}21. Install Marzban${NC}\n"
printf "${Cyan}22. Install TorSina${NC}\n"
read -p "Enter your choice: " choice
case $choice in
1)
updateAndUpgrade
;;
2)
changeSSHPort
;;
3)
BindDomain
;;
4)
RemoveDomain
;;
5)
getSSL
;;
6)
installDocker
;;
7)
changeHostname
;;
8)
installNginx
;;
9)
getSSLWithNginx
;;
10)
installNVM
;;
11)
addSSHKey
;;
12)
ChangeFirewallStatus
;;
13)
showOpenPorts
;;
14)
openNewPort
;;
15)
closePort
;;
16)
addUser
;;
17)
changePassword
;;
18)
deleteUser
;;
19)
installXUI
;;
20)
InstallHiddify
;;
21)
InstallMarzban
;;
22)
TorSina
;;
*)
printf "${Red}Invalid choice. Exiting.${NC}\n"
exit 1
;;
esac
}
#Execute the main function
main