Forum Discussion

crosson_16669's avatar
crosson_16669
Icon for Nimbostratus rankNimbostratus
Jan 08, 2013

Read the contents of a file as a string

Since tcl [open filename] is a disabled command how do I read the contents of a stored file as a string?

 

 

I need to store several scripts and run eval against them. It makes more sense to store these as files.

 

 

set var [read filename] ?

 

6 Replies

  • I intend on running eval against the string so it will be small snippits of tcl.
  • sorry, do i misunderstand?

    e.g.

     

    root@(ve11a)(cfg-sync Changes Pending)(Active)(/Common)(tmos) list ltm rule myrule
    ltm rule myrule {
        when RULE_INIT {
      log local0. "\[eval [ifile get info_hostname_ifile]\] [eval [ifile get info_hostname_ifile]]"
    }
    }
    root@(ve11a)(cfg-sync Changes Pending)(Active)(/Common)(tmos) list ltm ifile info_hostname_ifile
    ltm ifile info_hostname_ifile {
        file-name info_hostname_file
    }
    root@(ve11a)(cfg-sync Changes Pending)(Active)(/Common)(tmos) list sys file ifile info_hostname_file
    sys file ifile info_hostname_file {
        checksum SHA1:13:bc46a43e1e7e2fcd875eeb750872f2813896854a
        create-time 2013-01-09:01:29:03
        created-by admin
        last-update-time 2013-01-09:01:29:03
        mode 33188
        revision 1
        size 13
        updated-by admin
    }
    
    [root@ve11a:Active:Changes Pending] config  tail -f /var/log/ltm
    Jan  9 01:31:41 ve11a info tmm[11170]: Rule /Common/myrule : [eval info hostname] ve11a.acme.com
    Jan  9 01:31:41 ve11a info tmm1[11170]: Rule /Common/myrule : [eval info hostname] ve11a.acme.com
    

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    If you're on 11.x, like Nitass showed, iFiles would be a simple solution for this. Else, you could URI encode the script, add it to a datagroup (external or internal) and then retrieve the content using the class command and decode it using URI::decode.

     

     

    That said, if you are eval'ing raw content, make sure you sanitize the input. Else, you'll cause a runtime TCL error and close the client connection or worse, potentially crash TMM.

     

     

    iFiles wiki page

     

    https://devcentral.f5.com/wiki/iRules.ifile.ashx

     

     

    Class command wiki page

     

    https://devcentral.f5.com/wiki/iRules.class.ashx

     

     

    v10 data group formats

     

    https://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1086448/iRules-Data-Group-Formatting-Rules.aspx

     

     

    v11 data group formats

     

    https://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1086510/v11-iRules-Data-Group-Updates.aspx

     

     

    Aaron
  • Sadly we are currently standardized on 10.2.3.

     

     

    Else, you could URI encode the script, add it to a datagroup (external or internal) and then retrieve the content using the class command and decode it using URI::decode

     

     

    Are there any examples of this?
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus

    Here's an untested example. Please test it on a VE or non-production unit.

     

    
     Raw commands on one line separated by a delimiter
    log local0. "exec'd command 1"|log local0. "exec'd command 2"
    
     URL encoded copy (http://www.opinionatedgeek.com/DotNet/Tools/UrlEncode/Encode.aspx)
    log%20local0.%20%22exec'd%20command%201%22%0d%0alog%20local0.%20%22exec'd%20command%202%22
    
    Add to a data group:
    
    class commands_class {
       {
          "log%20local0.%20%22exec'd%20command%201%22%0d%0alog%20local0.%20%22exec'd%20command%202%22"
       }
    }
    
     Use the class command in an iRule to parse the data group
    
    when RULE_INIT {
    
    set url_encoded [class element -name 0 commands_class]
    foreach cmd [split [URI::decode $url_encoded] "|"] {
    log local0. "About to eval: $cmd"
    eval $cmd
    }
    }
    

     

    Aaron