Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Crazy question about writing to a text file that is actually an executable...

by bcarroll (Pilgrim)
on Sep 07, 2012 at 02:17 UTC ( #992222=perlquestion: print w/ replies, xml ) Need Help??
bcarroll has asked for the wisdom of the Perl Monks concerning the following question:

I had a crazy idea that I wanted to pass by the fellow monks, though not exactly specific to perl.

Is it possible to create an executable (compiled perl or other) that can trick the OS into thinking it is a text file? My thought is if such a thing can be done, this could be utilized to send logfile data to anything (remote database, parser, etc...).

Here is a use case: I have a bunch of servers that run various proprietary software applications that are only capable of writing logfiles locally, and I don't have access to these remote systems. If I can somehow replace the default logfile with this "magic", anything written to this file could be sent to my database, log collector server, etc...

Is this just a pipe dream?

Comment on Crazy question about writing to a text file that is actually an executable...
Re: Crazy question about writing to a text file that is actually an executable...
by cheekuperl (Monk) on Sep 07, 2012 at 02:44 UTC
    trick the OS into thinking it is a text file
    The OS would obey your orders! Just open your executable with a text editor. The OS can't, however, show you what you wish to see. It will only show what's in the executable depending on the configuration of the text editor and OS.

    Is this just a pipe dream? Could be! Until its realized :)
Re: Crazy question about writing to a text file that is actually an executable...
by Anonymous Monk on Sep 07, 2012 at 02:51 UTC
Re: Crazy question about writing to a text file that is actually an executable...
by BrowserUk (Pope) on Sep 07, 2012 at 04:01 UTC
    Is this just a pipe dream?

    You are closer than you think.

    Have a daemon/service that creates a fifo/named-pipe with the filename you want, and then it can do whatever you want with whatever is written to the "file".

    I think that's called 'syslog' on some OSs?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      To slightly clarify what BrowserUk said, what you're looking for is already a feature of most operating systems, including pretty much any UNIX-based OS you like. The feature is usually referred to as "named pipes" or "FIFOs".

      You use a command mkfifo to create what is basically a dummy entry in the filesystem. You then write your script to read lines from that filename in a loop. When there are no lines to be read, then Perl's readline operator (i.e. <$fh>) will automatically wait until lines are available.

      Other processes can then just open that file in write mode and write lines to it, just like it was a normal file. Your script will wake up when lines are available and be able to process them. If your script is slow to process the data then the process writing the data will just act like it's writing to a slow disk. Nothing actually gets written to disk (except the directory entry for the filename); the data streams from the output filehandle of one process to the input handle of the other process.

      This is basically just the same technique as chaining together commands in the shell using the pipe (|), but rather than the pipes being anonymous, they have filenames attached to them. Hence "named pipes".

      Named pipes are pretty awesome for interprocess communication, though can be fiddly to set up, so generally people use things like TCP sockets instead. The one place where named pipes beat sockets is where (as in your question) either the reader or the writer has not been written with interprocess communication in mind, so you need to "trick" it into thinking it's reading from a plain old file.

      I seem to remember at one point using named pipes to include a randomly generated message in my e-mail signatures, when at the time my mail client could only read signatures from a static text file.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Crazy question about writing to a text file that is actually an executable...
by Marshall (Prior) on Sep 07, 2012 at 11:44 UTC
    I have a bunch of servers that run various proprietary software applications that are only capable of writing logfiles locally, and I don't have access to these remote systems.
    If you don't have access to these remote systems, then how could any possible information exchange happen?
    So you must have some kind of remote access to these systems otherwise the question doesn't appear to make sense.
    Can you provide some more details?
      My thought was that I would provide the file to someone that has access. They put the file where it needs to be then the data being written to the file would be sent to my server.

      At least now I understand what "named pipes" means (I have seen that for years without knowing what they were)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992222]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2014-12-26 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (166 votes), past polls