Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Regtool

by hopes (Friar)
on Sep 29, 2002 at 16:59 UTC ( [id://201569]=sourcecode: print w/replies, xml ) Need Help??
Category: NT Admin
Author/Contact Info Fermin Palacios hopes at permonks
Description: regtool.pl is a simple utility to list win32 registry keys.
One day, I posted this node. It was simple a win32 registry tool.
I've improve the regtool.pl and here is the result.
Now it's also a simple tool, but it has its own documentation.
Now I'm considering to make other functions, like "edit", "new" and "delete" :-)
Hopes
perl -le '$_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s,$_^=q,$\^-]!,,print'
######################################################################
+######
### Title:     regtool.pl
### Description: Win32 Registry key listing
######################################################################
+######

use Win32::TieRegistry;
use Getopt::Long;
use Pod::Usage;
use strict;
use vars qw($pound $key $help $man);

my %regtypes = (1=>'REG_SZ', 2=> 'REG_EXPAND_SZ', 3=>'REG_BINARY', 4=>
+'REG_DWORD', 7=>'REG_MULTI_SZ');

sub ListKey
{
    #$RRef is a TieRegistry ref
    my $RRef = shift;
    for my $key ($RRef->SubKeyNames)    {
        print "[$key]\n";
    }
    for my $key ($RRef->ValueNames)    {
        my ($ValueData,$ValueType)= $RRef->GetValue( $key );
        $key = "(Default)" unless $key;
        if ($regtypes{$ValueType} eq 'REG_BINARY')     {
           $ValueData = unpack ("H*", $ValueData);
           $ValueData =~ s/([0-9a-f]{2})/$1 /gi;
        }
        elsif ($regtypes{$ValueType} eq 'REG_MULTI_SZ')     {
              $ValueData= join "\n\t",@$ValueData;
        }
        print "$key ($regtypes{$ValueType})\n\t$ValueData\n";
    }        
    printf ("%0d keys, %0d values", scalar $RRef->SubKeyNames, scalar 
+$RRef->ValueNames);
}

sub ExtendedPath
{
    my $rpath=shift;
    $$rpath =~ s|/?[^/]*/\.\.||g;
}

GetOptions('help|?' => \$help, man => \$man);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

#First of all, the registry delimiter (to avoid backslashing)
$pound= $Registry->Delimiter("/");
#Without these two lines, we can't read REG_MULTI_SZ or REG_EXPAND_SZ 
+keys
$Registry->FixSzNulls(1);
$Registry->SplitMultis(1);
#If there is a parameter, we will try to use it as "current key"
my $subkey = shift;
eval {
     $Registry->Open($subkey);
};
#If there is an error, current key is the root of the registry
$subkey ="" if ($@);
for (;;)    {
    #Interactive Mode
    #First, we print the prompt (path and $)
    print "\n[/$subkey]\$ ";
    #Read the command
    my $command = <STDIN>;
    chomp $command;
    my ($commandname, @arguments) = split /\s+/, $command;
    my $arguments = join " ",@arguments;
    
    if (lc $commandname eq 'quit' or lc $commandname eq 'exit')    {
        last;
    }
    elsif ( lc $commandname eq 'dir' or lc $commandname eq 'ls') {
        #List command
        #If there is a paremeter, could be a key that we want to list
        $arguments = $subkey unless $arguments;
        ExtendedPath(\$arguments);
        #If no, we'll list the current key
        eval    {
            $key = $Registry->Open($arguments);
        };
            if ($@ or ref $key ne 'Win32::TieRegistry')    {
                #If error, $arguments maybe a relative path
            eval    {
                my $path = "$subkey/$arguments";
                       ExtendedPath(\$path);
                $key = $Registry->Open($path);
            };
                if ($@ or ref $key ne 'Win32::TieRegistry')    {
                print "Error: $arguments doesn't exist\n";
                next;
                }
        }
            ListKey($key);
    }
    elsif (lc $commandname eq 'cd')             {
        #"Change Directory" command
        unless ($arguments)    {
            #If not parameter, "current key" is shown
            print "$subkey\n";
        }
        #If there is a paremeter, we try to open this registry key
        eval {
            $key = $Registry->Open($arguments);
        };
        if ($@ or ref $key ne 'Win32::TieRegistry')      {
            #If error, may be a "relative path" to the current key
                   eval {
                       my $path = "$subkey/$arguments";
                       ExtendedPath(\$path);
                       $key = $Registry->Open($path);
                   };
               if ($@ or ref $key ne 'Win32::TieRegistry')    {
                       print "Error: $arguments doesn't exist\n";
                       next;
               }
               else {
                   $arguments="$subkey/$arguments";
                   ExtendedPath(\$arguments);
               }
        }
        $subkey = $arguments; 
    }
    else {
         print "$commandname is not a valid command.\n";
    }
}
=pod

=head1 NAME

    regtool.pl - Win32 Registry list keys

=head1 SYNOPSIS

    regtool.pl is a simple utility to list win32 registry keys.

=head2 regtool.pl sintax

        regtool.pl
        regtool.pl --help, regtool -h
        regtool.pl --man, regtool -m

=head1 OPTIONS

=over 4

=item cd key

    Change current key

=item dir [key] (or ls [key])

    List key. If key is not provided, actual registry key is listed

=item quit (or exit)

    Quit regtool.pl

=back

=head1 DESCRIPTION
    
    This utility allows to list keys in the Win32 Registry.
    When you execute regtool.pl you see a console like this:
    
    [/]$ 
    
    You can move arround the keys of the registry like if you where in
+ a directory tree.
    When you use cd (change directory) or ls (list directory) it is po
+ssible to use '.'
    and '..' to refer the "actual registry key" and the "previous regi
+stry key"
    
    [/LMachine]$ cd SOFTWARE
    [/LMachine/SOFTWARE]$ cd ../SYSTEM
    [/LMachine/SYSTEM]$ 
    
    To list a key, you can use 'ls' or dir command
   
    [/LMachine/Software]$ ls ..
    [HARDWARE]
    [SAM]
    [SECURITY]
    [SOFTWARE]
    [SYSTEM]
    5 keys, 0 values
    
    [/LMachine/Software/Perl]$ ls
    (Default) (REG_SZ)
        D:\Perl\
    BinDir (REG_SZ)
        D:\Perl\bin\perl.exe
        
    regtool.pl can list:
        REG_SZ
        REG_EXPAND_SZ
        REG_BINARY
        REG_DWORD
        REG_MULTI_SZ
    keys

=head1 AUTHOR
    
    Fermin Palacios (hopes at perlmonks.org)
    larioja.pm@fitipaldis.com

=cut

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://201569]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found