# Variables new_ssh_port=1222 # Replace with your desired SSH port new_user="newuser"# Replace with your desired username public_key=""# Replace with your public SSH key
# Ensure the script is run as root if [ "$(id -u)" != "0" ]; then echo"This script must be run as root" 1>&2 exit 1 fi
# 1. Configure SSH echo"Configuring SSH..." sed -i "s/#Port 22/Port $new_ssh_port/" /etc/ssh/sshd_config sed -i "s/#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config
# 2. Create new user with sudo privileges echo"Creating new user '$new_user' with sudo privileges..." adduser --gecos """$new_user" echo"$new_user ALL=(ALL) ALL" >> /etc/sudoers
# Add public key for new user echo"Adding public key for new user..." mkdir -p /home/$new_user/.ssh echo$public_key > /home/$new_user/.ssh/authorized_keys chown -R $new_user:$new_user /home/$new_user/.ssh chmod 700 /home/$new_user/.ssh chmod 600 /home/$new_user/.ssh/authorized_keys
# Check if jq is installed if ! command -v jq &> /dev/null then echo"jq could not be found, attempting to install..." sudo apt-get update -y sudo apt-get install -y jq else echo"jq is already installed" fi
# List all UFW rules with numbers and filter by port 8443 rules=$(ufw status numbered | grep '8443' | awk -F '[][]''{ print $2 }')
# Check if any rules were found if [ -z "$rules" ]; then echo"No UFW rules found for port 8443." else # Reverse the rules order to avoid disrupting the numbering for rule in $(echo"$rules" | tac); do echo"Deleting rule $rule for port 8443..." yes | ufw delete $rule done
echo"UFW rules deletion complete." # Show current status ufw status verbose
echo"\n\n----------- update to new ip ranges ---------\n\n" fi
# Fetch JSON data using curl json_data=$(curl --request GET \ --url https://api.cloudflare.com/client/v4/ips \ --header 'Authorization: Bearer YOUR_BEARER_TOKEN' \ --header 'Content-Type: application/json')
# Check if curl command was successful if [ $? -ne 0 ]; then echo"Failed to fetch data from API." exit 1 fi
# Extract ipv4_cidrs using jq and convert it into a Bash array readarray -t ip_ranges < <(echo"$json_data" | jq -r '.result.ipv4_cidrs[]')
# Add UFW rules for each IP range for ip in"${ip_ranges[@]}"; do echo"Allowing IP range $ip on port 8443" ufw allow from $ip to any port 8443 done