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: Beginner:
Introducing a 2nd lookup to an already existing script

 



mike3point0
Novice

Aug 17, 2013, 4:54 PM

Post #1 of 5 (1171 views)
Introducing a 2nd lookup to an already existing script Can't Post

Got some assistance on a script I was working on a couple of months ago here, and wanted to ask another question. I've been trying to add another lookup to add to the mix. Here is the script:

#!/usr/bin/perl


use
5.10.0;
use strict;
use warnings;

my $server_lst =
'new_unixlist';
my $non_prod =
'new_itmlist';
my $itm_found =
'InITMToday';

open my $srv_fh,
'<', $server_lst or die "failed to open '$server_lst' $!";
open my $non_fh,
'<', $non_prod or die "failed to open '$non_prod' $!";
open my $itm_fh,
'>>', $itm_found or die "failed to open '$itm_found' $!";

my %server = map { chomp; $_ =>
1 } <$srv_fh>;
close $srv_fh;

my $agentCounter =
0;


while ( my $str = <$non_fh> ) {

next if $str =~ /^
#/;

my $hostname = (split /:/, $str)[-
2];

next unless $hostname;
# sanity check to make sure the hostname field wasn't empty

if ( exists $server{$hostname} ) {
say {$itm_fh}
"$hostname was found on line $. => $str";
$agentCounter++;
}
}
say {$itm_fh}
"Total number of servers with Agents is $agentCounter";

close $non_fh;
close $itm_fh;


I want to add another variable to the line where I write to the itm_fh filehandle and say "$hostname $itsos was found on line $. => $str"

Noting that $itsos is a new field, taken from a file with the values:

server1 HP-UX
server2 AIX

There are 2 values on each line in the new text file, separated by a space/tab. I'm thinking I have to read these values into an hash, something to the effect of:

my %serveroses = <$os_fh>;
close $os_fh;


while (($samehost, $itsos) = each %serveroses) {

say {$itm_fh} "$hostname was found on line $. => $str running $itsos";

}



Place this right after the statement that reads "if ( exists $server{$hostname} ) {"



Any thoughts or suggestions are appreciated..

Thanx.



Mike-


Laurent_R
Veteran / Moderator

Aug 18, 2013, 2:26 AM

Post #2 of 5 (1165 views)
Re: [mike3point0] Introducing a 2nd lookup to an already existing script [In reply to] Can't Post

Please use the code tags for your code, it will be easier to understand it (just as I did for the two code fragments below).

This:

Code
my %serveroses = <$os_fh>; 
close $os_fh;


is probably not going to do what you want. The odd lines of your file will end up as keys in your hash, and the even lines as values.

You probably want to do something like this prior to reading your main file:


Code
my %serveroses; 
open my $os_fh, "<", $input_file or die "could not open $input_file $!";
while (<$input_file>) {
chomp;
my ($key, $value) = split /\s+/, $_;
$serveroses{$key} = $value;
}


For the rest of your questions, I don't understand exactly what you want to do, and the poor formating of your code does not help understanding what you are already doing. Use the code tags, I really mean it, your posts will be far more effective in conveying your ideas.


mike3point0
Novice

Aug 19, 2013, 7:07 AM

Post #3 of 5 (1149 views)
Re: [Laurent_R] Introducing a 2nd lookup to an already existing script [In reply to] Can't Post

Hello-

My apologies. I will make a better attempt to use the code tags. Sometimes, I move too fast for my own good.. Angelic

I will try your code snippet today and let you know how it works out.

Mike-


BillKSmith
Veteran

Aug 19, 2013, 11:13 AM

Post #4 of 5 (1140 views)
Re: [mike3point0] Introducing a 2nd lookup to an already existing script [In reply to] Can't Post

As I understand you task, You want to copy each item(that also appears in the file 'new_unixlist') from the file 'new_itmlist' to the file 'InITMToday' and append the corresponding os from a new unspecified file.


Code
#!/usr/bin/perl  
use 5.10.0;
use strict;
use warnings;

my $server_lst = 'new_unixlist';
my $non_prod = 'new_itmlist';
my $itm_found = 'InITMToday';
my $os_list = 'unspecified';

open my $srv_fh, '<', $server_lst or die "failed to open '$server_lst' $!";
my %server = map { chomp; $_ => 1 } <$srv_fh>;
close $srv_fh;

open my $os_fh, '<', $os_list or die "failed to open '$os_list' $!";
my %serveroses = map { chomp; split /[\s\t]+/ } <$os_fh>;
close $os_fh;

my $agentCounter = 0;
open my $itm_fh, '>>', $itm_found or die "failed to open '$itm_found' $!";
open my $non_fh, '<', $non_prod or die "failed to open '$non_prod' $!";
while ( my $str = <$non_fh> ) {
next if $str =~ /^#/;
my $hostname = ( split /:/, $str )[-2];
next unless $hostname;
if ( exists $server{$hostname} ) {
say {$itm_fh} "$hostname was found on line $."
. " => $str running $serveroses{$hostname}";
$agentCounter++;
}
}
close $non_fh;
say {$itm_fh} "Total number of servers with Agents is $agentCounter";
close $itm_fh;

Good Luck,
Bill


mike3point0
Novice

Aug 21, 2013, 7:32 PM

Post #5 of 5 (1104 views)
Re: [mike3point0] Introducing a 2nd lookup to an already existing script [In reply to] Can't Post

Hello-



My apologies, as its been a couple of days. Adding the code snippet worked, and now I'm getting lines like these:



lxd1132 lxd1132 RedHat



So the two server names that are listed confirms that I found a match in both files, and then the third column indicates the OS.



Many thanx.



Mike-

 
 


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

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