G
G
gadzhikuliev2019-09-13 13:54:54
Python
gadzhikuliev, 2019-09-13 13:54:54

Why is tftpd-hpa copying files to an unspecified directory?

I saw a strange phenomenon. Wrote a script for backup of network equipment configuration files. When manually running the script, the script copies everything where it is indicated, but when I wrote the rule in cron from root, the script began copying files directly to /root, creating a folder there, although the tftp folder is registered elsewhere. The folder is named after the date the script was triggered.
Rule in cron from root, since there is a need to restart the tftp service after editing the config:
0 0 * * 6 /home/tftp/backup
tftp config:

TFTP_USERNAME="tftp"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="--secure --create"
TFTP_DIRECTORY="/home/tftp/2019-09-07"

What's in root's home directory:
[email protected]:~$ sudo ls -l /root/
total 16
drwxrwxrwx 2 tftp tftp 4096 Aug 17 00:00 2019-08-17
drwxrwxrwx 2 tftp tftp 4096 Aug 24 00:00 2019-08-24
drwxrwxrwx 2 tftp tftp 4096 Aug 31 00:00 2019-08-31
drwxrwxrwx 2 tftp tftp 4096 Sep  7 00:00 2019-09-07

Note that the most recent directory 2019-09-07 is the same as TFTP_DIRECTORY="/home/tftp/2019-09-07". That is, the config editing method worked correctly.
What's in the user directory after river running the script:
[email protected]:~$ ls -l
total 8
drwxrwxrwx 2 tftp tftp 4096 Sep  1 08:27 2019-09-01
-rwxrwxrwx 1 tftp tftp 2983 Aug 26 03:22 backup

Two methods from the script that are responsible for the tftp config:
# Каталог по умолчанию для TFTP
folder = str(datetime.date.today())
param = '"/home/tftp/' + folder + '"'

# Редактирование конфигурационного файла TFTP-сервера с учётом изменения каталога по умолчанию. Каталог именуется по текущей дате
def edit():
    str1 = 'TFTP_USERNAME="tftp"'
    str2 = 'TFTP_ADDRESS="0.0.0.0:69"'
    str3 = 'TFTP_OPTIONS="--secure --create"'

    with open('/etc/default/tftpd-hpa') as cfg:
        for line in cfg:
            if line.startswith('TFTP_DIRECTORY'):
                break
    sett = line.split('=')[1].strip()
    line = line.replace(sett,param)

    with open('/etc/default/tftpd-hpa', 'w') as cfg:
        cfg.write(str1)
        cfg.write('\n' + str2)
        cfg.write('\n' + str3)
        cfg.write('\n' + line)


# Здесь создаётся папка, куда складываются конфиги, и перезапускается служба TFTP
def tftp_start():
    os.system('cd /home/tftp/')
    os.mkdir(folder)
    os.system('chown tftp:tftp ' + folder)
    os.system('chmod 777 ' + folder)
    os.system('systemctl restart tftpd-hpa')

Why is tftp ignoring path in config?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Barbolin, 2019-09-13
@dronmaxman

Where is your script, share the code with the people)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question