Logic Machine Forum
FTPS example - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: FTPS example (/showthread.php?tid=1002)

Pages: 1 2


FTPS example - npinguin - 19.09.2017

Hello

I am trying to secure ftp a backup file to a nas

Does anyone have an example?

I use currently the folowing code wich fails with error : * string: 504 TLS/SSL protection required.
Code:
    -- ftp to NAS
 log("System Backup: transfering backup to NAS, host:" .. FTP_TARGET_IP .. ", target:" .. target)
 res, err = socket.ftp.put({
     host = FTP_TARGET_IP,
     user = FTP_TARGET_LOGIN,
     password = FTP_TARGET_PW,
     type = "i",
     argument = target,
     source = ltn12.source.file(io.open(dst, "rb")) })
 
    if (err) then
   log(err)
   log(res)
   alert("System Backup: failed, could not ftp to NAS, host:" .. FTP_TARGET_IP .. ", target:" .. target)
    end

Thanks
Nicky


RE: Sftp example - Erwin van der Zwart - 19.09.2017

Hi,

Try this:
Code:
require('socket.ftp')

-- Get hostname
hostname = io.readfile('/proc/sys/kernel/hostname'):trim()

-- ftp file
ftpfile = string.format('ftp://user:password@192.168.10.1/backup-' .. hostname .. '-' .. os.date('%Y.%m.%d') .. '.tar.gz')

--Create attachment
src = 'backup-' .. os.date('%Y.%m.%d') .. '.tar.gz'
dst = '/home/ftp/' .. src

-- prepare files for backup
os.execute('sh /lib/genohm-scada/web/general/backup.sh')

-- create cleanup archive
os.execute('cd /lib/genohm-scada/storage && tar -c -z -f ' .. dst .. ' ./')

-- upload to ftp
result, err = socket.ftp.put(ftpfile, io.readfile(dst))

-- error while uploading
if err then
 log('FTP upload error: %s', tostring(err))
else
 log('file uploaded to external ftp')
end

--Delete created backup file from tmp folder inside HL
os.execute('cd /lib/genohm-scada/storage && rm -rf user userlib.luas blockly.luas initscript.lua helpers.js genohm-scada.config filter*')

--Delete temp file from HL ftp
os.remove(dst)

BR,

Erwin


RE: Sftp example - npinguin - 20.09.2017

Thank Erwin
But that looks to be a regular FTP, i need sftp?
Thanks
Nicky


RE: Sftp example - admin - 21.09.2017

Which LM version do you have?


RE: Sftp example - npinguin - 21.09.2017

(21.09.2017, 14:24)admin Wrote: Which LM version do you have?
Reactor v2 running latest firmware


RE: Sftp example - admin - 22.09.2017

Update this package through system config > packages:
https://dl.openrb.com/lm-17/pkg/luasocket_2.0.2-20_mxs.ipk

Then add secure = true to your ftp.put config table. If you get an error that data connection must be encrypted try adding datasecure = true as well. This might not work for all FTP servers and it depends on how the server is configured.


RE: Sftp example - npinguin - 22.09.2017

Thanks Admin

Close but not yet there

I updated the package and added the option secure = true
on the synology nas: empty file is created 
on the logic machine I have an error:"closed"

I added the option secure = true and datasecure = true
on the synology nas: no file is created
on the logic machine I have an error:"503 Use AUTH and PBSZ commands first"


RE: FTPS example - admin - 22.09.2017

Ok, can you enable remote diagnostics on your LM and forward port 22, then send me access via PM?


RE: FTPS example - admin - 25.09.2017

Thanks for access providing, please install updated package and try again:
https://dl.openrb.com/lm-17/pkg/luasocket_2.0.2-21_mxs.ipk


RE: FTPS example - npinguin - 25.09.2017

(25.09.2017, 11:29)admin Wrote: Thanks for access providing, please install updated package and try again:
https://dl.openrb.com/lm-17/pkg/luasocket_2.0.2-21_mxs.ipk

Thanks a lot, Works perfectly ! 

Are you going to include this in future firmwares, or will it be a seperate install?

Nicky


RE: FTPS example - admin - 26.09.2017

Yes, it will be included in the future fw releases.


RE: FTPS example - DGrandes - 22.02.2023

Hi!

Is there any update about this?

Can I use sftp in Logic Machine to make a backup in a NAS? Those links are broken

Thanks


RE: FTPS example - admin - 23.02.2023

Updated packages are already included in the latest firmware. No need to install anything manually.
SFTP (file transfer over SSH) is not supported yet. This topic is about FTPS which is a secure version of the FTP protocol. Your NAS should support it.


RE: FTPS example - Kilogica - 13.03.2024

I would like to point out that the script is used in many web servers, both Schneider and LM, and has always worked and still works every day.
when I try to upload the backup to ftp, the upload happens, but the backup has size 0 and if I try to open it it gives me an error. Furthermore, it is logged in the error log
os.remove call blocked. What is going on?
Code:
require('socket.ftp')

-- Get hostname
hostname = io.readfile('/proc/sys/kernel/hostname'):trim()

-- ftp file
ftpfile = string.format('ftp://user:pass@ip/backup/' .. os.date('%Y.%m.%w') .. '-' .. hostname .. '.tar.gz;type=i')

--Create attachment
src = 'backup-' .. os.date('%Y.%m.%d') .. '.tar.gz'
dst = '/home/ftp/' .. src

-- prepare files for backup
os.execute('sh /lib/genohm-scada/web/general/backup.sh')

-- create cleanup archive
os.execute('cd /lib/genohm-scada/storage && tar -c -z -f ' .. dst .. ' ./')

-- upload to ftp
result, err = socket.ftp.put(ftpfile, io.readfile(dst))

-- error while uploading
if err then
  log('FTP upload error: %s', tostring(err))
else
  log('file uploaded to external ftp')
end

--Delete created backup file from tmp folder inside HL
os.execute('cd /lib/genohm-scada/storage && rm -rf user userlib.luas blockly.luas initscript.lua helpers.js genohm-scada.config filter*')

--Delete file from HL ftp
os.remove(dst)



RE: FTPS example - Daniel - 13.03.2024

Which firmware do you use?


RE: FTPS example - admin - 13.03.2024

Erwin's code is outdated. This script should be used instead: https://kb.logicmachine.net/scripting/backup-file-email-ftp/


RE: FTPS example - Kilogica - 13.03.2024

now it gives me

io.readproc call blocked
os.tmpname call blocked

the problem is on a wiser with firmware 3.0.0

the file si uploaded with dimension zero kylobyte

Code:
host = 'ip'
user = 'user'
pass = 'pass'

path = 'backup/webserver-' .. os.date('%Y-%m-%d') .. '.zip'

ftp = require('socket.ftp')
ltn12 = require('ltn12')
webrequest = require('webrequest')
data = webrequest('general', 'backup')

res, err = ftp.put({
  host = host,
  user = user,
  password = pass,
  argument = path,
  source = ltn12.source.string(data)
})

if not res then
  log('FTP error', err)
end



RE: FTPS example - Daniel - 13.03.2024

Block unsafe functions in scripts: make sure this is disabled


RE: FTPS example - Kilogica - 13.03.2024

(13.03.2024, 11:29)Daniel Wrote: Block unsafe functions in scripts: make sure this is disabled

Thanks,
the problem partially solved, file loaded but corrupt

   


RE: FTPS example - Daniel - 13.03.2024

I just tried it on SL 3.0.0 and it works just fine. Use 7zip for editing.