Empty

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
#!/bin/bash

last_ip=$(</root/last_ip.txt)
this_ip=`dig +short myip.opendns.com @resolver1.opendns.com`

# if IP has changed, and IP isn't an empty string
if [[ "$last_ip" != "$this_ip" && ! -z "$this_ip" ]]; then
    echo "Last IP: $last_ip"
    echo "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
fi

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.

Category: