Use iRules LX to write to the file system

Problem this snippet solves:

Whist you can log to /var/log/ltm from an iRule, the TCL implementation on TMOS has its file reading and writing capabilities disabled.

However it is still possible to write to a non-logging file to the file system using iRules LX, albeit, to the iRules LX workspace. A second script would be required to move the file to where you require it.

This demo writes a file test.txt containing a client IP address to the iRules LX workspace

How to use this snippet:

First you need an iRule that passes the client IP address into iRules LX (you'll want to put the LX call in a catch for a prod environment)


when CLIENT_ACCEPTED {
    set rpcHandle  [ILX::init "fs-pl" "fs-ex"]
    set result [ILX::call $rpcHandle "writeFile" "[IP::client_addr]"]
    log local0. "$result"
}


Then a simple iRules LX script that will write out a test file with the value of the IP you passed in.

It will write out the file to the extension folder which will vary.. it will also change slightly each time it executes

 

For example, my file was located here:

/var/sdm/plugin_store/plugins/:Common:fs-pl_83750_4/extensions/fs-ex/test.txt

 

The next time, it was located here:

/var/sdm/plugin_store/plugins/:Common:fs-pl_83750_5/extensions/fs-ex/test.txt


Code :

var f5 = require('f5-nodejs');
var ilx = new f5.ILXServer();
var fs = require('fs');
 
function writeFile(req, res) {
    var ip = req.params()[0];
    fs.writeFile('test.txt', ip, function(err) {
        if (err) {
            res.reply(err);
        } else {
            res.reply('file written successfully');
        }
    });
}
 
ilx.addMethod('writeFile', writeFile);
ilx.listen();

Tested this on version:

No Version Found
Published Jul 01, 2019
Version 1.0
No CommentsBe the first to comment