11#! /bin/bash
2- # info: add system ip address
2+ # info: add system IP address
33# options: IP NETMASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]
44#
5- # example: v-add-sys-ip 216.239.32.21 255.255.255.0
5+ # example: v-add-sys-ip 203.0.113.1 255.255.255.0
66#
7- # This function adds ip address into a system. It also creates rc scripts. You
8- # can specify ip name which will be used as root domain for temporary aliases.
7+ # This function adds IP address into a system. It also creates rc scripts. You
8+ # can specify IP name which will be used as root domain for temporary aliases.
99# For example, if you set a1.myhosting.com as name, each new domain created on
10- # this ip will automatically receive alias $domain.a1.myhosting.com. Of course
11- # you must have wildcard record *.a1.myhosting.com pointed to ip . This feature
10+ # this IP will automatically receive alias $domain.a1.myhosting.com. Of course
11+ # you must have wildcard record *.a1.myhosting.com pointed to IP . This feature
1212# is very handy when customer wants to test domain before dns migration.
1313
1414# ----------------------------------------------------------#
1515# Variables & Functions #
1616# ----------------------------------------------------------#
1717
18+ # Argument definition
19+ ip=" ${1// / } "
20+ netmask=" $2 "
21+
1822# Get interface name
19- iface=$( /bin/ip token | awk -F ' dev ' ' {print $2}' )
23+ # First try to detect which interface the IP address resides on
24+ iface=" $( ip -d -j addr show | jq --arg IP " $ip " -r ' .[] | if .addr_info[].local == $IP then .ifname else empty end' ) "
25+ # If that fails, detect the default interface as a fallback
26+ if [ -z " $iface " ]; then
27+ iface=" $( ip -d -j route show | jq -r ' .[] | if .dst == "default" then .dev else empty end' ) "
28+ fi
2029
21- # Argument definition
22- ip=${1// / }
23- netmask=$2
2430iface=" ${3-$iface } "
2531user=" ${4-admin} "
2632ip_status=" ${5-shared} "
27- ip_name=$6
28- nat_ip=$7
33+ ip_name=" $6 "
34+ nat_ip=" $7 "
2935
3036# Includes
3137# shellcheck source=/etc/hestiacp/hestia.conf
@@ -65,31 +71,26 @@ check_hestia_demo_mode
6571# Action #
6672# ----------------------------------------------------------#
6773
68- cidr=$( convert_netmask $netmask )
69- broadcast=$( get_broadcast $ip $netmask )
74+ cidr=" $( convert_netmask " $netmask " ) "
75+ broadcast=" $( get_broadcast " $ip " " $netmask " ) "
7076
71- sys_ip_check=$( /sbin/ ip addr | grep " $ip " )
77+ sys_ip_check=" $( ip addr | grep -w " $ip " ) "
7278if [ -z " $sys_ip_check " ]; then
73- # Adding sys ip
74- /sbin/ip addr add $ip /$cidr dev $iface \
75- broadcast $broadcast label $iface
79+ # Adding system IP
80+ ip addr add " $ip /$cidr " dev " $iface " broadcast " $broadcast " label " $iface "
7681
7782 # Check if netplan is in use and generate configuration file
78- if [ ! -z $( which netplan) ]; then
79- if [ ! -z " $( netplan generate --mapping " $iface " | grep networkd) " ]; then
80- netplan=1
81- else
82- netplan=0
83- fi
83+ if [ -n " $( netplan generate --mapping " $iface " 2> /dev/null | grep networkd) " ]; then
84+ netplan=" true"
8485 else
85- netplan=0
86+ netplan=" false "
8687 fi
8788
88- if [ " $netplan " == " 1 " ]; then
89+ if [ " $netplan " = " true " ]; then
8990 if [ -f " /etc/netplan/60-hestia.yaml" ]; then
9091 sys_ip=" - $ip /$cidr "
9192 else
92- sys_ip=" # Added by hestia , please do not edit the file manually!"
93+ sys_ip=" # Added by Hestia , please do not edit the file manually!"
9394 sys_ip=" $sys_ip \nnetwork:"
9495 sys_ip=" $sys_ip \n version: 2"
9596 sys_ip=" $sys_ip \n renderer: networkd"
@@ -99,7 +100,7 @@ if [ -z "$sys_ip_check" ]; then
99100 sys_ip=" $sys_ip \n - $ip /$cidr "
100101 fi
101102 IFS=' %'
102- echo -e $sys_ip >> /etc/netplan/60-hestia.yaml
103+ echo -e " $sys_ip " >> /etc/netplan/60-hestia.yaml
103104 unset IFS
104105 else
105106 sys_ip=" \n# Added by Hestia Control Panel"
@@ -112,11 +113,9 @@ if [ -z "$sys_ip_check" ]; then
112113fi
113114
114115# Generating timestamp
115- time_n_date=$( date +' %T %F' )
116- time=$( echo " $time_n_date " | cut -f 1 -d \ )
117- date=$( echo " $time_n_date " | cut -f 2 -d \ )
116+ new_timestamp
118117
119- # Adding hestia ip
118+ # Adding Hestia IP
120119echo " OWNER='$user '
121120STATUS='$ip_status '
122121NAME='$ip_name '
@@ -130,30 +129,30 @@ DATE='$date'" > $HESTIA/data/ips/$ip
130129chmod 660 $HESTIA /data/ips/$ip
131130
132131# WEB support
133- if [ ! -z " $WEB_SYSTEM " ]; then
132+ if [ -n " $WEB_SYSTEM " ]; then
134133 web_conf=" /etc/$WEB_SYSTEM /conf.d/$ip .conf"
135- rm -f $web_conf
134+ rm -f " $web_conf "
136135
137136 if [ " $WEB_SYSTEM " = ' httpd' ] || [ " $WEB_SYSTEM " = ' apache2' ]; then
138137 if [ -z " $( /usr/sbin/apachectl -v | grep Apache/2.4) " ]; then
139- echo " NameVirtualHost $ip :$WEB_PORT " > $web_conf
138+ echo " NameVirtualHost $ip :$WEB_PORT " > " $web_conf "
140139 fi
141- echo " Listen $ip :$WEB_PORT " >> $web_conf
142- cat $HESTIA_INSTALL_DIR /apache2/unassigned.conf >> $web_conf
143- sed -i ' s/directIP/' $ip ' /g' $web_conf
144- sed -i ' s/directPORT/' $WEB_PORT ' /g' $web_conf
140+ echo " Listen $ip :$WEB_PORT " >> " $web_conf "
141+ cat $HESTIA_INSTALL_DIR /apache2/unassigned.conf >> " $web_conf "
142+ sed -i ' s/directIP/' $ip ' /g' " $web_conf "
143+ sed -i ' s/directPORT/' $WEB_PORT ' /g' " $web_conf "
145144
146145 elif [ " $WEB_SYSTEM " = ' nginx' ]; then
147- cp -f $HESTIA_INSTALL_DIR /nginx/unassigned.inc $web_conf
148- sed -i ' s/directIP/' $ip ' /g' $web_conf
146+ cp -f $HESTIA_INSTALL_DIR /nginx/unassigned.inc " $web_conf "
147+ sed -i ' s/directIP/' $ip ' /g' " $web_conf "
149148 fi
150149
151150 if [ " $WEB_SSL " = ' mod_ssl' ]; then
152151 if [ -z " $( /usr/sbin/apachectl -v | grep Apache/2.4) " ]; then
153- sed -i " 1s/^/NameVirtualHost $ip :$WEB_SSL_PORT \n/" $web_conf
152+ sed -i " 1s/^/NameVirtualHost $ip :$WEB_SSL_PORT \n/" " $web_conf "
154153 fi
155- sed -i " 1s/^/Listen $ip :$WEB_SSL_PORT \n/" $web_conf
156- sed -i ' s/directSSLPORT/' $WEB_SSL_PORT ' /g' $web_conf
154+ sed -i " 1s/^/Listen $ip :$WEB_SSL_PORT \n/" " $web_conf "
155+ sed -i ' s/directSSLPORT/' $WEB_SSL_PORT ' /g' " $web_conf "
157156 fi
158157fi
159158
@@ -169,24 +168,24 @@ if [ -n "$PROXY_SYSTEM" ]; then
169168 # mod_extract_forwarded
170169 fw_conf=" /etc/$WEB_SYSTEM /conf.d/mod_extract_forwarded.conf"
171170 if [ -e " $fw_conf " ]; then
172- ips=$( grep ' MEFaccept ' $fw_conf | grep -v ' #' | head -n1)
173- sed -i " s/$ips /$ips $ip /g" $fw_conf
171+ ips=$( grep ' MEFaccept ' " $fw_conf " | grep -v ' #' | head -n1)
172+ sed -i " s/$ips /$ips $ip /g" " $fw_conf "
174173 fi
175174
176175 # mod_rpaf
177176 rpaf_conf=" /etc/$WEB_SYSTEM /mods-enabled/rpaf.conf"
178177 if [ -e " $rpaf_conf " ]; then
179- rpaf_str=$( grep RPAFproxy_ips $rpaf_conf )
180- [ -z " $rpaf_str " ] && sed -i ' s|</IfModule>|RPAFproxy_ips\n</IfModule>|' $rpaf_conf && rpaf_str=' RPAFproxy_ips'
178+ rpaf_str=" $( grep RPAFproxy_ips " $rpaf_conf " ) "
179+ [ -z " $rpaf_str " ] && sed -i ' s|</IfModule>|RPAFproxy_ips\n</IfModule>|' " $rpaf_conf " && rpaf_str=' RPAFproxy_ips'
181180 rpaf_str=" $rpaf_str $ip "
182- sed -i " s/.*RPAFproxy_ips.*/$rpaf_str /" $rpaf_conf
181+ sed -i " s/.*RPAFproxy_ips.*/$rpaf_str /" " $rpaf_conf "
183182 fi
184183
185- # mod_remoteip
184+ # mod_remoteip
186185 remoteip_conf=" /etc/$WEB_SYSTEM /mods-enabled/remoteip.conf"
187186 if [ -e " $remoteip_conf " ]; then
188- if [ $( grep -ic " $ip " $remoteip_conf ) -eq 0 ]; then
189- sed -i " s/<\/IfModule>/RemoteIPInternalProxy $ip \n<\/IfModule>/g" $remoteip_conf
187+ if [ " $( grep -ic " $ip " " $remoteip_conf " ) " -eq " 0 " ]; then
188+ sed -i " s/<\/IfModule>/RemoteIPInternalProxy $ip \n<\/IfModule>/g" " $remoteip_conf "
190189 fi
191190 fi
192191fi
0 commit comments