Total: $0.00

vsFTPd Passive Mode with Dynamic IP

By IT Dan, 06/13/2018 - 17:08

Below is a script that checks the current external IP address, and if it has changed since last check, updates the pasv_address line in the vsftpd.conf file.

I'm using Debian Stretch. For dig you may need to install dnsutils like so:

apt-get install dnsutils

Create the shell script file:

vi /root/vsftpd-pasv_address.sh

[[ -f /root/last_ip.txt ]] && last_ip=$(/last_ip.txt) || last_ip='' # If file exists read value else set empty
this_ip=$(dig +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)
if [ $? -ne 0 ]
    # Exit if the dig command failed
    exit 1

# If not empty and IP changed
if [[ -n "$this_ip" && "$last_ip" != "$this_ip" ]]
    echo $(date +"%Y-%m-%d %T") "Old IP: $last_ip"
    echo $(date +"%Y-%m-%d %T") "New IP: $this_ip"
    echo "$this_ip" > /root/last_ip.txt
    sed -ri "s/^(pasv_address=*).*$/\1${this_ip}/" /etc/vsftpd.conf
    /etc/init.d/vsftpd restart
exit 0

Make the file executable:

chmod +x /root/vsftpd-pasv_address.sh

I added the following cronjob:

*/10 * * * * /root/vsftpd-pasv_address.sh

The script will run every 10 minutes, and update vsftpd.conf if your external IP address changes.