#!/bin/bash

# File to store the last known external IP address
LAST_IP_FILE="last_external_ip.txt"

# Fetch external IP address using a service like ifconfig.me
EXTERNAL_IP=$(curl -s ifconfig.me)

echo "External IP: $EXTERNAL_IP"

# Check if the last known IP is different from the current IP
if [ -f "$LAST_IP_FILE" ]; then
    LAST_IP=$(cat "$LAST_IP_FILE")
    echo "Last ip: $LAST_IP"
    if [ "$EXTERNAL_IP" != "$LAST_IP" ]; then
        # Update the vsftpd configuration file
        sed -i "s/^pasv_address=.*/pasv_address=$EXTERNAL_IP/" /etc/vsftpd.conf

        # Restart vsftpd service
        systemctl restart vsftpd

        # Update the last known IP
        echo "$EXTERNAL_IP" > "$LAST_IP_FILE"
    fi
else
    # If the last IP file doesn't exist, create it and save the current IP
    echo "$EXTERNAL_IP" > "$LAST_IP_FILE"
fi
