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: Advanced:
Execute Perl script on Remote server without any CPAN module

 



kotak86
New User

Jan 28, 2014, 8:39 PM

Post #1 of 7 (9369 views)
Execute Perl script on Remote server without any CPAN module Can't Post

Hi I have Perl script (with set of system command in `sys command`). I wanted to run this script in other list of server that I can provide at the run time from my local server.

Also I can not use Expect/ Net::SSH module or any other CPAN module. My script is running perfectly fine in my local machine, but when I have place the block of code, it seems it do not have reference to the script and can not perform any other line of code after doing ssh to the specific server.

e.g. script name: servervalidation.pl my script should first take the server, and ssh to that server and then in that server login as su and then perform some set of command and get the information and send back to the local server from which I run he script. take argument: list_of_server File content :servera, serverb, serverc......

My script in local machine working fine. Please let me know if you need more information from my side.

Thank you for reading my post and your help.



Code
#!/usr/bin/perl  
use strict;
use warnings;
#use GetOpt::Long;
use Data::Dumper;

my $file = "NewPropertiesExample.txt";

open PROPERTIES, $file or die $!;

my @properties = <PROPERTIES>;

foreach my $prop (@properties){

if ($prop =~ m/^\[DISK_SETUP\]/){

my @disk_setup = split /\s+/, $prop;
my @owner_rights = split/:/, $disk_setup[3];
my %Dsetup = (
$disk_setup[1] =>
{
size => $disk_setup[2],
owner => $owner_rights[0],
group => $owner_rights[1],
perm => $disk_setup[4]
}
);


my $disk_report = `df -h | grep -w "$disk_setup[1]" | tr -s [:space:]`;
my @allocated = split /\s+/, $disk_report;
my $group = getgrgid((stat $disk_setup[1])[5]);
my $owner = getpwuid((stat $disk_setup[1])[4]);

my $perm = `stat --format=%a $disk_setup[1]`;
my $server_disk_size = $allocated[1];

my $perm_check = compare_perm($Dsetup{$disk_setup[1]}{perm}, $perm);
my $size_check = compare_disk_allocation($Dsetup{$disk_setup[1]}{size}, $server_disk_size);

print "The size for $disk_setup[1] is not matched\n", if $size_check == 0;
print "The perm for $disk_setup[1] is not matched\n", if $perm_check == 0;

foreach my $disk (keys %Dsetup) {
print $disk, ":" ;
foreach my $disk_para (keys %{$Dsetup{$disk}}){
print "\t$disk_para = $Dsetup{$disk}{$disk_para} \n";
}

}



}
elsif ($prop =~ m/^\[SOFTWARE\]/){

# print "SOFTWARE line found => $prop";

}

elsif ($prop =~ m/^\[CONFIG\]/){

# print "Configuration line found => $prop \n";
}

}


sub compare_disk_allocation {

my $size_status = 0;
my ($required_allocation, $server_allocation) = @_;
$required_allocation =~ s/[A-Za-z]+//g;
$server_allocation =~ s/[A-Za-z]+//g;
$size_status =1, if ($required_allocation == $server_allocation);
return $size_status;

}


sub compare_perm {

my $perm_status = 0;
my ($required_perm, $server_perm) = @_;
$perm_status =1, if ($required_perm == $server_perm);
return $perm_status;

}


Just sake for Information, cntent of property file is.....


Code
[DISK_SETUP]	/app	150GB	own1:grp1		755  
[DISK_SETUP] /logs 150GB own2:grp2 755
[DISK_SETUP] /home 35GB own3:grp3 755

[SOFTWARE] Introscope9.1.1
[SOFTWARE] AnthillPro3
[SOFTWARE] Apache Ant 1.8.4
[SOFTWARE] Apache Xalan (Java version) 2.7.1
[SOFTWARE] Oracle JRE 1.7.0_03 32bit
[SOFTWARE] cRON
[SOFTWARE] curl
[SOFTWARE] expect
[SOFTWARE] gzip
[SOFTWARE] Postfix
[SOFTWARE] tar

[SOFTWARE] Tomcat 7.0.27

[CONFIG] ulimit 10000



(This post was edited by kotak86 on Jan 29, 2014, 8:09 AM)


FishMonger
Veteran / Moderator

Jan 29, 2014, 6:38 AM

Post #2 of 7 (9344 views)
Re: [kotak86] Execute Perl script on Remote server without any CPAN module [In reply to] Can't Post

Since you don't want to use any module and instead want to use only shell commands executed via backticks or system function, your question has nothing to do with perl.

For now lets remove the perl portion of your question and focus on the other portion.

How would you do this directly from the command line? The answer to that question is what you'd do in the command you execute in the perl script.


FishMonger
Veteran / Moderator

Jan 29, 2014, 6:41 AM

Post #3 of 7 (9343 views)
Re: [kotak86] Execute Perl script on Remote server without any CPAN module [In reply to] Can't Post

FYI to others that may want to assist; this question is cross posted on perlmonks but with the exclusion of the code.
http://www.perlmonks.org/?node_id=1072424


kotak86
New User

Jan 29, 2014, 7:59 AM

Post #4 of 7 (9338 views)
Re: [FishMonger] Execute Perl script on Remote server without any CPAN module [In reply to] Can't Post

Hi ...



Thank you for your response. I have already saved the auth. key in my laptop for all the servers that I want to login individually. Manually I just to

ssh servername ;



In script, I want to perform some function in local machine and then it should log in to remote server and then grab some data and retrieve back to local server, where local server compare the output and produce the some result.

But I have noticed when I do ssh, I can login to different server but after that the remote server can not find the reference of that script anymore and can't execute the remaining steps.


(This post was edited by kotak86 on Jan 29, 2014, 8:08 AM)


FishMonger
Veteran / Moderator

Jan 29, 2014, 8:10 AM

Post #5 of 7 (9334 views)
Re: [kotak86] Execute Perl script on Remote server without any CPAN module [In reply to] Can't Post

The servers that you ssh into won't know about the script on your local system.

If you want to execute that script on each server that you ssh into, then you need to copy that script to each server and then execute it. You can use scp which runs over ssh to copy the file.


kotak86
New User

Jan 29, 2014, 8:37 AM

Post #6 of 7 (9327 views)
Re: [FishMonger] Execute Perl script on Remote server without any CPAN module [In reply to] Can't Post

Hi...



Thanks but I am unable to implement it in code. You mean first I have to copy my script and then have to ssh to that server ??

if that is the case, I can I return back to my local server and compare the output of the local and output of the remote ??



I have set of server, so exactly I can simulate my requirement as below.



1. Local machine: run the script, grep the content of one specific directory.

2. Go to Remote Machine: same, grep the content of that dire (as in local) in some file/variable

3. then return back to the local server and compare the output of the local and remote server.

(This call to remote may be multiple times during script).


FishMonger
Veteran / Moderator

Jan 29, 2014, 9:10 AM

Post #7 of 7 (9321 views)
Re: [kotak86] Execute Perl script on Remote server without any CPAN module [In reply to] Can't Post

You need to rethink your approach; it's not going to work the way you want.

I don't have enough details on what you need to accomplish to be able to give you any code. But here's the direction I'd look into with the assumption that this is not a one time process.

Your script needs to be copied to each server and instead of outputting the data to STDOUT, it stores all of the data in an appropriate data structure and saves it to a file via the Storable module. This script could be execute as a cron job.

A central server (i.e., your local machine) would collect each of those files from the other servers and process them as needed. That (collection) step could be done as part of the agent script that each of the servers executed (i.e., the script will scp the file to the central server) or as part of the processing script that compares the data (i.e., the collection script on your machine uses scp to collect the files).

 
 


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

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