v-update-sys-ip 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/bash
  2. # info: update system ip
  3. # options: [NONE]
  4. #
  5. # The function scans configured ip in the system and register them with hestia
  6. # internal database. This call is intended for use on vps servers, where ip is
  7. # set by hypervizor.
  8. #----------------------------------------------------------#
  9. # Variable&Function #
  10. #----------------------------------------------------------#
  11. # Importing system variables
  12. source /etc/profile
  13. # Includes
  14. source $HESTIA/func/main.sh
  15. source $HESTIA/func/ip.sh
  16. source $HESTIA/conf/hestia.conf
  17. #----------------------------------------------------------#
  18. # Verifications #
  19. #----------------------------------------------------------#
  20. #----------------------------------------------------------#
  21. # Action #
  22. #----------------------------------------------------------#
  23. # Listing system ip addresses
  24. ips=$(/sbin/ip addr |grep 'inet ' |grep global |awk '{print $2}' |cut -f1 -d/)
  25. v_ips=$(ls $HESTIA/data/ips/)
  26. ip_num=$(echo "$ips" |wc -l)
  27. v_ip_num=$(echo "$v_ips" |wc -l)
  28. # Checking primary IP change
  29. if [[ "$ip_num" -eq '1' ]] && [[ "$v_ip_num" -eq 1 ]]; then
  30. if [ "$ips" != "$v_ips" ]; then
  31. new=$ips
  32. old=$v_ips
  33. fi
  34. fi
  35. # Updating configs
  36. if [ ! -z "$new" ]; then
  37. mv $HESTIA/data/ips/$old $HESTIA/data/ips/$new
  38. # Updating PROXY
  39. if [ ! -z "$PROXY_SYSTEM" ]; then
  40. cd /etc/$PROXY_SYSTEM/conf.d
  41. if [ -e "$old.conf" ]; then
  42. mv $old.conf $new.conf
  43. sed -i "s/$old/$new/g" $new.conf
  44. fi
  45. fi
  46. # Updating WEB
  47. if [ ! -z "$WEB_SYSTEM" ]; then
  48. cd /etc/$WEB_SYSTEM/conf.d
  49. if [ -e "$old.conf" ]; then
  50. mv $old.conf $new.conf
  51. sed -i "s/$old/$new/g" $new.conf
  52. fi
  53. sed -i "s/$old/$new/g" $HESTIA/data/users/*/web.conf
  54. for user in $(ls $HESTIA/data/users/); do
  55. $BIN/v-rebuild-web-domains $user no
  56. done
  57. $BIN/v-restart-proxy
  58. $BIN/v-restart-web
  59. fi
  60. # Updating DNS
  61. if [ ! -z "$DNS_SYSTEM" ]; then
  62. sed -i "s/$old/$new/g" $HESTIA/data/users/*/dns.conf
  63. sed -i "s/$old/$new/g" $HESTIA/data/users/*/dns/*.conf
  64. for user in $(ls $HESTIA/data/users/); do
  65. $BIN/v-rebuild-dns-domains $user no
  66. done
  67. $BIN/v-restart-dns
  68. fi
  69. # Updating FTP
  70. if [ ! -z "$FTP_SYSTEM" ] && [ "$FTP_SYSTEM" = 'vsftpd' ]; then
  71. conf=$(find /etc/ -maxdepth 2 -name $FTP_SYSTEM.conf)
  72. if [ ! -z "$conf" ]; then
  73. sed -i "s/$old/$new/g" $conf
  74. $BIN/v-restart-ftp
  75. fi
  76. fi
  77. # Updating firewall
  78. if [ ! -z "$FIREWALL_SYSTEM" ]; then
  79. sed -i "s/$old/$new/g" $HESTIA/data/firewall/*.conf
  80. $BIN/v-update-firewall
  81. fi
  82. fi
  83. # Adding system IP
  84. for ip in $ips; do
  85. check_ifconfig=$(/sbin/ifconfig |grep "$ip")
  86. if [ ! -e "$HESTIA/data/ips/$ip" ] && [ ! -z "$check_ifconfig" ]; then
  87. interface=$(/sbin/ip addr |grep $ip |awk '{print $NF}' |uniq)
  88. interface=$(echo "$interface" |cut -f 1 -d : |head -n 1)
  89. netmask=$(/sbin/ip addr |grep $ip |cut -f 2 -d / |cut -f 1 -d \ )
  90. netmask=$(convert_cidr $netmask)
  91. $BIN/v-add-sys-ip $ip $netmask $interface
  92. fi
  93. done
  94. # Updating NAT
  95. pub_ip=$(curl -s https://www.hestiacp.com/what-is-my-ip/)
  96. if [ ! -e "$HESTIA/data/ips/$pub_ip" ]; then
  97. if [ -z "$(grep -R "$pub_ip" $HESTIA/data/ips/)" ]; then
  98. ip=$(ls -t $HESTIA/data/ips/ |head -n1)
  99. $BIN/v-change-sys-ip-nat $ip $pub_ip
  100. fi
  101. fi
  102. #----------------------------------------------------------#
  103. # Hestia #
  104. #----------------------------------------------------------#
  105. exit