Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Shell Script to Update "/etc/cyberpanel/machineIP" File of CyberPanel for Dynamic WAN IP (IPv4/ IPv6) Server

Dynamically Update CyberPanel Server IP using Bash Script with Crontab/ Systemd Timer

  • NO LONGER MAINTAINED!
  • This script will check if external IP is changed or not and will update the external IP of this file /etc/cyberpanel/machineIP so that you can access the panel anytime without any issue.

Script Requirements

How to Use it?

  1. Put the cpip.sh file to anyhwere you like. E.g., /home/scripts.

  2. For crontab:

  • Open terminal.
  • Give the file execute permission, type/ copy sudo chmod +x /location/cpip.sh press Enter.
  • Open crontab, type/ copy crontab -e press Enter.
  • If you want to run the job every minute, type/ copy * * * * * /location/cpip.sh.
  • If you want to run the job every 5 minute, type/ copy 5 * * * * /location/cpip.sh.
  • If you want more time flexibility then goto this link.
  • After setting cron press Esc and type wq then press Enter.
  • Setting up cron is completed!
  1. For systemd timer:
  • Open terminal.
  • Give the file execute permission, type/ copy sudo chmod +x /location/cpip.sh press Enter.
  • Create a systemd service unit, type/ copy /etc/systemd/system/cpip.service press Enter.
  • Copy all of the content from cpip.service down below.
  • After setting service unit press Esc and type wq then press Enter.
  • Create a systemd timer unit at the same location of service unit, type/ copy /etc/systemd/system/cpip.timer press Enter.
  • Copy all of the content from cpip.timer down below.
  • If you want to run the timer unit every minute, edit/ copy *:0/1 (This is given on the cpip.timer file).
  • If you want to run the timer unit every 5 minute, edit/ copy *:0/5.
  • If you want more time flexibility then goto this link.
  • After setting timer unit press Esc and type wq then press Enter.
  • Reload systemd, type/ copy sudo systemctl daemon-reload.
  • Enable timer unit, type/ copy sudo systemctl enable cpip.timer.
  • Start timer unit, type/ copy sudo systemctl start cpip.timer.
  • Setting up systemd timer is completed!

How it Works?

This is a verbal representation of the script explaining how the script works.

Steps:

  1. Script will start executing and shows [CyberPanel IP] IP CHECK INITIATED....

  2. Now it will check for INTERNET AVAILABILITY and shows [CyberPanel IP] CHECKING FOR INTERNET AVAILABILITY....

  • If internet is available then it will show [CyberPanel IP] INTERNET IS AVAILABLE! and go to next step.
  • If internet is not available then it will show [CyberPanel IP] INTERNET IS UNAVAILABLE! and exit.
  1. Now it will check for MACHINE IP and shows [CyberPanel IP] GETTING MACHINE IP....
  • If machineIP file is present then it will show [CyberPanel IP] MACHINE IP: 1.2.3.4.
  • If machineIP file is not present then it will show [CyberPanel IP] MACHINE IP FILE NOT FOUND! and exit.
  1. Now it will check for CURRENT IP and shows [CyberPanel IP] CHECKING FOR NEW IP....
  • If public IP is unchanged and valid then it will show [CyberPanel IP] NO NEW IP DETECTED! and exit.
  • If public IP is changed and valid then it will show [CyberPanel IP] NEW IP DETECTED! and go to next step.
  • If selected interface can not check for Current IP due to curl error or IP is invalid then it will show [CyberPanel IP] CHECKING FOR NEW IP FAILED! and exit.
  1. Now it will check for IP change. if it get's new IP from Step 4 it will show [CyberPanel IP] UPDATING IP....
  • If it failed to update the IP then it will show [CyberPanel IP] IP UPDATE FAILED!, [CyberPanel IP] SAVING LOG... and exit.
  • If it succeeded to update the IP then it will show [CyberPanel IP] IP UPDATED TO: 1.2.3.4, [CyberPanel IP] SAVING IP... , [CyberPanel IP] SAVING LOG... and exit.

Notes

  • I have written the instructions based on CentOS 7.6.x.
  • Files name are started with 0, 1, 2, 3 because of orderly manner.
  • I am new to this Scripting Business that's why if I made any mistake please point out any issue!
  • Keep in mind that whether you use crontab or systemd timer both will create huge size log files! It will be better if you use logrotate to keep the log files at a minimum size.
[Unit]
Description=CyberPanel IP Service
After=network.target
[Service]
Type=oneshot
StandardOutput=null # journalctl will not output debug message
StandardError=journal # journalctl will only output error message
ExecStart=/location/cpip.sh
#If you want to add more script to one service then just add more ExecStart
#ExecStart=/location/a.sh
#ExecStart=/location/b.sh
#ExecStart=/location/c.sh
[Install]
WantedBy=multi-user.target
[Unit]
Description=CyberPanel IP Timer
[Timer]
OnCalendar=*:0/1
Persistent=true
[Install]
WantedBy=timers.target
#!/bin/bash
# CyberPanel IP
ip_file="/etc/cyberpanel/machineIP" # DO NOT EDIT THIS PATH!
log_file="/location/cp.log" # Edit "/location" and put your path location
# LOGGER
log()
{
if [[ $1 ]];
then
echo -e "[$(date)] - $1" >> $log_file
fi
}
# SCRIPT START
echo -e "\n[CyberPanel IP] IP CHECK INITIATED...\n"
# INTERNET AVAILABILITY
echo "[CyberPanel IP] CHECKING FOR INTERNET AVAILABILITY..."
internet_check=$(wget -q --spider -t 3 -T 5 https://1.1.1.1/)
internet_check_ev=$?
if [[ $internet_check_ev == 0 ]];
then
echo -e "[CyberPanel IP] INTERNET IS AVAILABLE!\n"
else
echo "[CyberPanel IP] INTERNET IS UNAVAILABLE!"
echo -e "[CyberPanel IP] EXITING...\n"
exit 1
fi
# MACHINE IP
echo "[CyberPanel IP] GETTING MACHINE IP..."
mac_ip=$(cat $ip_file)
if [[ -f $ip_file ]];
then
echo -e "[CyberPanel IP] MACHINE IP: $mac_ip\n"
else
echo "[CyberPanel IP] MACHINE IP FILE NOT FOUND!"
echo -e "[CyberPanel IP] EXITING...\n"
exit 1
fi
# CURRENT IP
echo "[CyberPanel IP] CHECKING FOR NEW IP..."
# FOR IPV6 EDIT THE LINK TO THIS -> (https://api6.ipify.org)
cur_ip=$(curl -s --retry 3 --connect-timeout 5 https://api.ipify.org)
cur_ip_ev=$?
cur_ip_val=$(ipcalc -sc $cur_ip)
cur_ip_val_ev=$?
pre_ip=$sav_ip
if [[ $cur_ip_ev == 0 ]] && [[ $cur_ip_val_ev == 0 ]];
then
if [[ $cur_ip == $pre_ip ]];
then
echo "[CyberPanel IP] NO NEW IP DETECTED!"
echo -e "[CyberPanel IP] EXITING...\n"
exit 0
else
echo -e "[CyberPanel IP] NEW IP DETECTED!\n"
fi
else
echo "[CyberPanel IP] CHECKING FOR NEW IP FAILED!"
echo -e "[CyberPanel IP] EXITING...\n"
exit 1
fi
# UPDATE IP
if [[ $cur_ip != $pre_ip ]];
then
echo "[CyberPanel IP] UPDATING IP..."
message="[CyberPanel IP] IP UPDATED TO: $cur_ip"
echo "$message"
echo "[CyberPanel IP] SAVING IP..."
echo "$cur_ip" > $ip_file
echo "[CyberPanel IP] SAVING LOG..."
log "$message"
echo -e "[CyberPanel IP] EXITING...\n"
exit 0
else
message="[CyberPanel IP] IP UPDATE FAILED!"
echo "$message"
echo "[CyberPanel IP] SAVING LOG..."
log "$message"
echo -e "[CyberPanel IP] EXITING...\n"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment