- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl script help
Operating System - HP-UX
1753524
Members
4968
Online
108795
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2009 08:23 AM
тАО10-12-2009 08:23 AM
Hello,
Attached below is my code. I would like to add an option so that I can get the results for all the 5 regions at once instead of running the script 5 times. An option like run it for all the regions.
Thanks in advance.
#!/usr/bin/perl
use strict;
use warnings;
## Machine Number hashes
my $regionMachine = {'01' =>['pl010200','pl010300','pl010400','pl010500'],
'02' => ['pl020200','pl020300','pl020400','pl020500'],
'03' => ['pl030200','pl030300','pl030400','pl030500','pl030600','pl030700','pl030800'],
'04' => ['pl040200', 'pl040300', 'pl040400'],
'05' => ['pl050200', 'pl050300', 'pl050400', 'pl050500', 'pl050600', 'pl050700', 'pl050800'],
};
## Set the data file on remote machine
my $auditFilePath = "/usr/local/bin/mpi.lis";
## Get the home dir for user
my $home = ( $ENV{HOME} );
chomp ($home);
## Date in MMDDYYYY
my $dateFormat = `date +%m%d%Y`;
chomp($dateFormat);
my @regions = get_user_input();
foreach my $region(@regions){
## Get servers based on region chosen
my @servers=();
push(@servers, @{$regionMachine->{$region}});
## File creation in user's home directory
my $filename = $home."/region".$region;
foreach my $server(@servers){
my $HANDLE;
open(HANDLE,">>", $filename) or die "Could not open $filename";
## Get the file contents from remote machine
my $str=`ssh -l user $server cat $auditFilePath`;
my @records = split("\n",$str);
foreach my $record(@records){
my @fields = split(":",$record);
if (@fields < 6) {
warn "Warning: '$record' has fewer than 6 fields\n";
next;
}
## Store the required data in file
print HANDLE $fields[0].":".$fields[1].":".$fields[2].":".$fields[3].":".$fields[5].":".$server." \n";
}
close HANDLE;
## Changing file permissions to 0755
chmod(0755, "$filename");
}
}
print "\nYour request has been completed\n";
sub get_user_input
{
my $date = `date`;
chomp($date);
print "\033[2J";
print "#########################################################\n";
print "# #\n";
print "# WELCOME #\n";
print "# $date #\n";
print "# #\n";
print "# #\n";
print "#########################################################\n";
print "\nChoose from the option given below: \n\n";
print "1. Select this option to continue \n\n";
print "2. exit \n\n";
print "Please make your selection:";
my $response = <>;
chomp ($response);
if ($response eq '2'){
print "Bye \n";
exit;
}elsif ($response eq '1'){
return main_input();
}
if ( ($response ne 1) || ($response ne 2) ){
return get_user_input();
}
}
sub main_input{
my $continue = "yes";
my @regions=();
while ($continue eq 'yes'){
print "\nPlease enter the region number.";
my $region = <>;
chomp ($region);
if ( ($region < 1) || ($region > 05) || (length($region) > 2) {
print "Invalid entry. Program will now exit\n";
exit;
}
if(length($region) ==1){
$region = "0".$region;
}
push(@regions, $region);
$continue="no";
}
print "\nProcessing your request\n";
return @regions;
}
Attached below is my code. I would like to add an option so that I can get the results for all the 5 regions at once instead of running the script 5 times. An option like run it for all the regions.
Thanks in advance.
#!/usr/bin/perl
use strict;
use warnings;
## Machine Number hashes
my $regionMachine = {'01' =>['pl010200','pl010300','pl010400','pl010500'],
'02' => ['pl020200','pl020300','pl020400','pl020500'],
'03' => ['pl030200','pl030300','pl030400','pl030500','pl030600','pl030700','pl030800'],
'04' => ['pl040200', 'pl040300', 'pl040400'],
'05' => ['pl050200', 'pl050300', 'pl050400', 'pl050500', 'pl050600', 'pl050700', 'pl050800'],
};
## Set the data file on remote machine
my $auditFilePath = "/usr/local/bin/mpi.lis";
## Get the home dir for user
my $home = ( $ENV{HOME} );
chomp ($home);
## Date in MMDDYYYY
my $dateFormat = `date +%m%d%Y`;
chomp($dateFormat);
my @regions = get_user_input();
foreach my $region(@regions){
## Get servers based on region chosen
my @servers=();
push(@servers, @{$regionMachine->{$region}});
## File creation in user's home directory
my $filename = $home."/region".$region;
foreach my $server(@servers){
my $HANDLE;
open(HANDLE,">>", $filename) or die "Could not open $filename";
## Get the file contents from remote machine
my $str=`ssh -l user $server cat $auditFilePath`;
my @records = split("\n",$str);
foreach my $record(@records){
my @fields = split(":",$record);
if (@fields < 6) {
warn "Warning: '$record' has fewer than 6 fields\n";
next;
}
## Store the required data in file
print HANDLE $fields[0].":".$fields[1].":".$fields[2].":".$fields[3].":".$fields[5].":".$server." \n";
}
close HANDLE;
## Changing file permissions to 0755
chmod(0755, "$filename");
}
}
print "\nYour request has been completed\n";
sub get_user_input
{
my $date = `date`;
chomp($date);
print "\033[2J";
print "#########################################################\n";
print "# #\n";
print "# WELCOME #\n";
print "# $date #\n";
print "# #\n";
print "# #\n";
print "#########################################################\n";
print "\nChoose from the option given below: \n\n";
print "1. Select this option to continue \n\n";
print "2. exit \n\n";
print "Please make your selection:";
my $response = <>;
chomp ($response);
if ($response eq '2'){
print "Bye \n";
exit;
}elsif ($response eq '1'){
return main_input();
}
if ( ($response ne 1) || ($response ne 2) ){
return get_user_input();
}
}
sub main_input{
my $continue = "yes";
my @regions=();
while ($continue eq 'yes'){
print "\nPlease enter the region number.";
my $region = <>;
chomp ($region);
if ( ($region < 1) || ($region > 05) || (length($region) > 2) {
print "Invalid entry. Program will now exit\n";
exit;
}
if(length($region) ==1){
$region = "0".$region;
}
push(@regions, $region);
$continue="no";
}
print "\nProcessing your request\n";
return @regions;
}
Solved! Go to Solution.
- Tags:
- Perl
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2009 08:56 AM
тАО10-12-2009 08:56 AM
Solution
Hi:
For openers, you script has a syntax error. Line-116 reads:
if ( ($region < 1) || ($region > 05) || (length($region) > 2) {
...it needs a closing parentheses and can be cleaned up to :
if ( $region < 1 || $region > 5 || length($region) > 2) ) {
As for your question, you should be able to make a subroutine to encapsulate the region of code from:
## Set the data file on remote machine
...
print "\nYour request has been completed\n";
Then, simply add a option to trigger that subroutine. For example, call the subroutine 'dowork' and pass to it the value for which it needs to do work:
...
sub dowork {
my ($value) =@_;
...
Call it like:
dowork($_) for (1..5);
...when you want to do "ALL" regions.
Regards!
...JRF...
For openers, you script has a syntax error. Line-116 reads:
if ( ($region < 1) || ($region > 05) || (length($region) > 2) {
...it needs a closing parentheses and can be cleaned up to :
if ( $region < 1 || $region > 5 || length($region) > 2) ) {
As for your question, you should be able to make a subroutine to encapsulate the region of code from:
## Set the data file on remote machine
...
print "\nYour request has been completed\n";
Then, simply add a option to trigger that subroutine. For example, call the subroutine 'dowork' and pass to it the value for which it needs to do work:
...
sub dowork {
my ($value) =@_;
...
Call it like:
dowork($_) for (1..5);
...when you want to do "ALL" regions.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2009 08:58 AM
тАО10-12-2009 08:58 AM
Re: perl script help
Thanks.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP