Ruby snippet to help find product keys

Discussion in 'Scripting' started by drewbug, Dec 15, 2012.

  1. drewbug

    drewbug MDL Member

    Aug 15, 2010
    232
    42
    10
    #1 drewbug, Dec 15, 2012
    Last edited by a moderator: Apr 20, 2017
    Code:
    puts ARGF.read.scan(/[A-Z1-9]{5}-[A-Z1-9]{5}-[A-Z1-9]{5}-[A-Z1-9]{5}-[A-Z1-9]{5}/)



    puts is a method that writes to STDOUT with a carriage return at the end.

    ARGF is a stream that process files given as command-line arguments or passed in via STDIN.

    read is a method that reads in an entire file as a string.

    scan is a method that searches through a string for a given regular expression.

    /[A-Z1-9]{5}-[A-Z1-9]{5}-[A-Z1-9]{5}-[A-Z1-9]{5}-[A-Z1-9]{5}/ is a regular expression that matches Windows product keys.


    So, placing a file named productkeyfind.rb containing the code snippet into the same folder as a file named produtkeyin.txt that contains at least one Windows product key lets you do:

    Code:
    ruby productkeyfind.rb productkeyin.txt > productkeysout.txt
    This will create a file named productkeysout.txt that contains each product key found in the input file on a separate line.

    If you (or anyone else) has any more questions, please, just let me know :)
     
  2. calpol

    calpol MDL Junior Member

    Dec 5, 2009
    98
    5
    0
    Can you explain more about this command please ? What is ARGF.read.scan ?
     
  3. drewbug

    drewbug MDL Member

    Aug 15, 2010
    232
    42
    10
    #3 drewbug, Dec 16, 2012
    Last edited: Dec 16, 2012
    (OP)
    Oh, yes, of course!

    I've added to the original post :)