CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Can't use string as a HASH ref

 



cvillepete
New User

Jan 17, 2012, 2:06 PM

Post #1 of 2 (911 views)
Can't use string as a HASH ref Can't Post

Hello! I am relatively new to Perl but not so new to programming, just a bit rusty I suppose. I am trying to connect to a server's registry and find a specific value, "heartbeatSvc". So far, I can get to the point of connecting to the machine, finding the registry entries, and using Dumper to dump everything out so I can see what I'm reading.

Here is my output from the perl script, including some debugging lines and the Dumper dump. I have altered existing code from a colleague, which is why I was able to get this far. The script breaks when the registry value isn't a number and isn't a word, but a word with numbers, like "myPassword1234" (I'm guessing). I have searched around and googled alot and sorta understand the problem (in that I can't use one type of object as another) but I don't know how to get around this or why this is the wrong way to accomplish this task. If this was VBScript, I could probably CAST the "myPassword1234" as a STRING and move on. Any suggestions would be appreciated.

The ultimate goal is to replace every instance of "check-nteventlog" with "check-nteventlogALT". Right now, I'm focusing on the "heartbeatSvc" value in the root of the registry tree. See the script below. Sorry for the lack of formatting and indentation. I tried to carry them over from my script to this text box but it didn't work out too well.

Thanks,
Pete

#########

Here is the gist of my script:

use strict;
use Win32::TieRegistry;
use Data::Dumper;
use Win32::Service;

my $oldSvcName = shift
or die "Usage: Nagios-ChangeNTServiceName.pl oldServiceName newServiceName myMachineName";
my $newSvcName = shift
or die "Usage: Nagios-ChangeNTServiceName.pl oldServiceName newServiceName myMachineName";

while (my $remoteMachine = shift) {
print "Connecting to //$remoteMachine/LMachine/SOFTWARE/Wow6432Node/Cheshire Cat/Nagios ...\n";

my $remoteKey= $Registry->Open( "//$remoteMachine/LMachine/SOFTWARE/Wow6432Node/Cheshire Cat/Nagios", {Delimiter=>"/"} )
or die "Can't connect to $remoteMachine or can't open subkey: $^E\n";
print Dumper(%$remoteKey); print "\n\n\n";

print "Looking for the current service name...\n";

my $needToRestartService = 0;

foreach my $thing ( keys %$remoteKey ) {
print "thing = $thing \n\n\n";

if ( lc($remoteKey->{$thing}->{'/heartbeatSvc'}) eq lc($oldSvcName) ) { #this is line 32
print "\tFound the old service name!\n";
$remoteKey->{$thing}->{'/heartbeatSvc'} = $newSvcName;
$needToRestartService++;
} else {
print "\tThe current service name doesn't match the \"oldServiceName\" you supplied.\n";
print "--$remoteKey->{$thing}->{'/heartbeatSvc'}--\n";
}
}
}

Here is the actual output of the debugging lines (print) and the Dumper dump:

Connecting to //MyMachineName/LMachine/SOFTWARE/Wow6432Node/Cheshire Cat/Nagios ...
$VAR1 = '14/';
$VAR2 = bless( {}, 'Win32::TieRegistry' );
$VAR3 = '15/';
$VAR4 = bless( {}, 'Win32::TieRegistry' );
$VAR5 = '16/';
$VAR6 = bless( {}, 'Win32::TieRegistry' );
$VAR7 = '17/';
$VAR8 = bless( {}, 'Win32::TieRegistry' );
$VAR9 = 'Filter3/';
$VAR10 = bless( {
'/filterDesc' => 'Application Log CatchAll',
'/eventLog' => '0x00000001',
'/eventLogName' => 'Application',
'/serviceName' => 'check-nteventlog',
'/ID' => '902, 903, 64, 65, 1301',
'/matchString' => '',
'/source' => '',
'/Information' => '0x00000000',
'/status' => '0x00000002',
'/Warning' => '0x00000000',
'/Error' => '0x00000001',
'/Audit Success' => '0x00000000',
'/Audit Failure' => '0x00000000',
'/notID' => '0x00000001',
'/notMatch' => '0x00000000',
'/notSource' => '0x00000000'
}, 'Win32::TieRegistry' );
$VAR11 = 'Filter4/';
$VAR12 = bless( {
'/filterDesc' => 'System Log CatchAll',
'/eventLog' => '0x00000007',
'/eventLogName' => 'System',
'/serviceName' => 'check-nteventlog',
'/ID' => '50, 1111, 36888, 36887',
'/matchString' => '',
'/source' => '',
'/Information' => '0x00000000',
'/status' => '0x00000002',
'/Warning' => '0x00000000',
'/Error' => '0x00000001',
'/Audit Success' => '0x00000000',
'/Audit Failure' => '0x00000000',
'/notID' => '0x00000001',
'/notMatch' => '0x00000000',
'/notSource' => '0x00000000'
}, 'Win32::TieRegistry' );
$VAR13 = '/nscaPassword';
$VAR14 = 'myPassword1234';
$VAR15 = '/MyHostname';
$VAR16 = 'MyMachineName';
$VAR17 = '/NSCAD';
$VAR18 = '10.0.0.111';
$VAR19 = '/Port';
$VAR20 = '0x00001623';
$VAR21 = '/encryptionMethod';
$VAR22 = '0x00000003';
$VAR23 = '/DEBUG';
$VAR24 = '0x00000000';
$VAR25 = '/logAlerts';
$VAR26 = '0x00000000';
$VAR27 = '/maxFilter';
$VAR28 = '0x00000004';
$VAR29 = '/processDelay';
$VAR30 = '0x0000001E';
$VAR31 = '/heartbeatFreq';
$VAR32 = '0x0000000F';
$VAR33 = '/heartbeatSvc';
$VAR34 = 'check-nteventlog';
$VAR35 = '/LastRun';
$VAR36 = '0x4F15E5AC';

Looking for the current service name...
thing = 14/
The current service name doesn't match the "oldServiceName" you supplied.
----
thing = 15/
The current service name doesn't match the "oldServiceName" you supplied.
----
thing = 16/
The current service name doesn't match the "oldServiceName" you supplied.
----
thing = 17/
The current service name doesn't match the "oldServiceName" you supplied.
----
thing = Filter3/
The current service name doesn't match the "oldServiceName" you supplied.
----
thing = Filter4/
The current service name doesn't match the "oldServiceName" you supplied.
----
thing = /nscaPassword

Can't use string ("myPassword1234") as a HASH ref while "strict refs" in use at Nagios-ChangeNTServiceName.pl line 32

Code




rovf
Veteran

Jan 18, 2012, 2:24 AM

Post #2 of 2 (894 views)
Re: [cvillepete] Can't use string as a HASH ref [In reply to] Can't Post

$remoteKey is a string, but to the left of the -> operator, you need a reference to a hash.

BTW, you should add

use warnings;

to your program.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives