LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-22-2015, 05:29 PM   #1
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
Search and insert after matched expression


I have a one liner which is apart of a perl script utilizing the use Net::OpenSSH module which allows me to ssh to a remote server and run commands

below is the command I want to run...it works when I log into the remote system and run it manually.

Code:
 perl -p -e 's/(\[agent\])/$1\n report=true/' /etc/puppet/puppet.conf
but in the script it acts weird and inserts after each line and cant match [agent] .

Been searching for days, can anyone help?
 
Old 09-22-2015, 09:59 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Show how you are actually calling it via ssh
 
Old 09-22-2015, 11:43 PM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Original Poster
Rep: Reputation: 42
I call it by :
Quote:
$ssh->system("perl -p -e 's/(\[agent\])/$1\n report=true/' /etc/puppet/puppet.conf")
it should also be noted , the script works fine as it has many lines issuing many different commands, but for some reason this doesnt work .
 
Old 09-23-2015, 04:22 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Have you considered specifying the path to the perl executable - its possible that when you shell out, its not available as part of the sub-shell env.
Code:
which perl
/usr/bin/perl

 $ssh->system("/usr/bin/perl -p -e 's/(\[agent\])/$1\n report=true/' /etc/puppet/puppet.conf")
In any case, you should always check for the error eg
Code:
  $ssh->system("ls /tmp") or
    die "remote command failed: " . $ssh->error;
as shown in the CPAN page http://search.cpan.org/~salva/Net-Op...Net/OpenSSH.pm

Last edited by chrism01; 09-23-2015 at 04:24 AM.
 
Old 09-23-2015, 10:26 AM   #5
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Original Poster
Rep: Reputation: 42
Thank you very much..but it didn't work, and its not an system error(although I understand what you are saying)...the problem is I get two different results, one (works) when I am at the command line, and the second (doesn't work) when ran in the script

at the command line: (works as it should)
Code:
~]# perl -p0 -e 's/(\[agent\])/$1\n report=true/' /etc/puppet/puppet.conf
[main]
server=puppetmaster.intranet.com

[agent]
 report=true
In the script: (doesn't work)
Code:
 $ssh->system("perl -p0 -e 's/(\[agent\])/$1\n report=true/' /etc/puppet/puppet.conf") ;
[m
 report=truein]
server=puppetmaster.intranet.com

[agent]
In the script it seems to not be searching for the word agent, and just input at the first [a] , which makes no sense.

Last edited by cbtshare; 09-23-2015 at 10:32 AM.
 
Old 09-23-2015, 10:45 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
try [[]agent[]] (avoid backslash)
 
1 members found this post helpful.
Old 09-23-2015, 01:29 PM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Original Poster
Rep: Reputation: 42
Quote:
Originally Posted by pan64 View Post
try [[]agent[]] (avoid backslash)
Thank you, your solution worked, but not entirely, I was able to atleast find agent, but cant seem to store the value ...meaning

Code:
perl -i -p0 -e 's/[[]agent[]]/$1\n report=true/' /etc/puppet/puppet.conf

returned: 

report=true
So I have to manually specify what should replace it..which isnt elegant but works.Is there a way to save the searched for value and recall it with your solution?
Neither $1 or \1 works
Code:
perl -i -p0 -e 's/[[]agent[]]/[agent]\n report=true/' /etc/puppet/puppet.conf

returned:
[agent]
 report=true

Last edited by cbtshare; 09-23-2015 at 01:46 PM.
 
Old 09-23-2015, 03:01 PM   #8
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 517

Rep: Reputation: 177Reputation: 177
Try escaping the $ in your original script. i.e.
Code:
$ssh->system("perl -p -e 's/(\[agent\])/\$1\n report=true/' /etc/puppet/puppet.conf")
 
Old 09-23-2015, 04:23 PM   #9
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Original Poster
Rep: Reputation: 42
THANK for your suggestion, I tried it but didn't work at the time either.
 
Old 09-23-2015, 10:11 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
1. you should ALWAYS code to include the error handler as a matter of course. Never assume ....
2. whilst I expect this is (probably) solvable via shelling out, this approach is a bit fragile anyway. Personally I'd seriously look at just opening the file in Perl and re-writing it from there.
More robust, better/finer control, easier to code, inc error handling.
 
Old 09-24-2015, 02:01 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
also you can try [\$], but it is now a guess only
 
Old 09-24-2015, 12:25 PM   #12
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Original Poster
Rep: Reputation: 42
Quote:
Originally Posted by chrism01 View Post
1. you should ALWAYS code to include the error handler as a matter of course. Never assume ....
2. whilst I expect this is (probably) solvable via shelling out, this approach is a bit fragile anyway. Personally I'd seriously look at just opening the file in Perl and re-writing it from there.
More robust, better/finer control, easier to code, inc error handling.
1) ok
2)Not sure what you mean, why would it be fragile? the entire code works and is written in perl

Code:
#!/usr/bin/perl

use strict;
use warnings;
use Net::OpenSSH;

our $keypath="";
my $key=" ";
my $ipaddr="";
my $user="root";
our $ssh="";
my $password="!mundo123";
our $passU="";
our $userU="";
our $ubuntu="";
open(FILE, "clients.txt") || die ("Error:Couldnt open files $!");

DOVER:
while(<FILE>)
{
chomp;
($ipaddr,$key)=split(" ",$_);

if ($key)
{
$keypath=`find keys/ -type f -name $key`;
chomp($keypath);
}


sub centos
{

         $ssh->system({tty => 1}, qq{rpm  -qa | grep puppetlabs-release ; echo $? ; if \[ $? != 0 \] ; then rpm -ivh http://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm ; else echo $? ; echo 'alr$
         $ssh->system({tty => 1}, qq{rpm  -qa | grep puppet ; if (( $? != 0 )); then yum -y install puppet;  else echo $?; echo 'Already installed'; fi }) ;
         my ($output, $errput) = $ssh->capture2({timeout => 11}, "rpm  -qa | grep puppet >/dev/null");
                 if($ssh->error)
                 {
                 $ssh->system("yum -y install puppet");
                 }
                        elsif(! $ssh->error)
                          {
                          goto CONFIG ;
                          }
                                else
                                {
                                goto DOVER;
                                }
        CONFIG:
        $ssh->system("grep -q -F 'server=puppetmaster.intranet.com' /etc/puppet/puppet.conf || sed -i -e '0,/\[main\]/a server=puppetmaster.intranet.com' /etc/puppet/puppet.conf") ;
        #$ssh->system("grep -q -F 'report=true' /etc/puppet/puppet.conf || sed -i -e '0,/\[agent\]/a report=true' /etc/puppet/puppet.conf") ;
        $ssh->system("perl -i -p0 -e 's/[[]agent[]]/[agent]\n report=true/' /etc/puppet/puppet.conf") ;
        $ssh->system("grep -q -F 'puppetmaster.intranet.com' /etc/hosts || echo '96. puppetmaster.intranet.com' >> /etc/hosts ");
        #$ssh->system("service puppet restart");
        $ssh->system("chkconfig puppet on");
                        my ($output3, $errput3) = $ssh->capture2({timeout => 11}, "dnsdomainname");
                        if($ssh->error)
                        {
                        $ssh->system(qq{perl -p -i -e "s/127.0.0.1/127.0.0.1 `egrep 'HOSTNAME=' /etc/sysconfig/network | cut -d '=' -f2`/g" /etc/hosts});
                        print "Added hostsname\n";
                        }
                                else
                                {
                                print  "Already added\n";
                                }


}

sub ubuntu
{
 $ssh->system({tty => 1}, qq{ if [ ! -e "/etc/puppet/puppet.conf" ]; then sudo  wget https://apt.puppetlabs.com/puppetlabs-release-trusty.deb; else echo "Nothing to do! "; exit;fi});
 $ssh->system({tty => 1}, qq{ if [ ! -e "/etc/puppet/puppet.conf" ]; then sudo dpkg -i puppetlabs-release-trusty.deb;  else echo "Nothing to do! "; exit;fi});
 $ssh->system({tty => 1}, qq{ if [ ! -e "/etc/puppet/puppet.conf" ]; then sudo apt-get -y --force-yes update; else echo "Nothing to do! "; exit;fi});
 $ssh->system({tty => 1}, qq{ if [ ! -e "/etc/puppet/puppet.conf" ]; then sudo apt-get install -y --force-yes puppet ; else echo "Nothing to do! "; exit;fi});
 $ssh->system({tty => 1}, qq{echo "Will change default puppet\n"; sudo perl -p -i -e 's/START=no/START=yes/g' /etc/default/puppet});
 $ssh->system({tty => 1}, qq{echo "Will remove SSL\n"; sudo rm -Rf "/home/$userU/.puppet/ssl/*" });
 $ssh->system({tty => 1}, qq{ if [ ! -e "/etc/puppet/puppet.conf" ]; then echo "Will generate config\n"; sudo puppet agent  --genconfig /etc/puppet/puppet.conf"; fi});
 $ssh->system({tty => 1}, qq{echo "Will input server in conf file\n"; echo -e "server=puppetmaster.intranet.com" | sudo tee -a /etc/puppet/puppet.conf"});
 $ssh->system({tty => 1}, qq{grep -q -F 'report = true' /etc/puppet/puppet.conf || sed -i -e '/\[agent\]/a report = true' /etc/puppet/puppet.conf}) ;
 $ssh->system({tty => 1}, qq{echo "Will change hosts file\n"; grep -q -F 'puppetmaster.intranet.com' /etc/hosts || echo '96. puppetmaster.intranet.com' | sudo tee -a /etc/$
 $ssh->system({tty => 1}, qq{ sudo  perl -p -i -e "s/Manifestdir/#Manifestdir/g" /etc/puppet/puppet.conf});
 $ssh->system({tty => 1}, qq{ sudo  perl -p -i -e "s/Manifest/#Manifest/g"  /etc/puppet/puppet.conf });
 $ssh->system({tty => 1}, qq{ sudo  perl -p -i -e "s/Masterlog/#Masterlog/g"  /etc/puppet/puppet.conf});
 $ssh->system({tty => 1}, qq{ sudo perl -p -i -e "s/Modulepath/#Modulepath/g" /etc/puppet/puppet.conf});

Last edited by cbtshare; 09-24-2015 at 12:27 PM.
 
Old 09-24-2015, 11:10 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Re 2: I was thinking along these lines http://search.cpan.org/~nwiger/File-...te.pm#EXAMPLES

Basically, any time you have to shell out, you have the issues of not always knowing what is available in the new process env that gets created and at what point variable interpolation takes place.
Plus, you have to think in at least 2 langs (possibly 3 if eg you shell out to a bash cmd/script that then calls an SQL cmd ).

If it can all be done in native Perl code, you sidestep those issues and error handling is easier.

Some things have to be done by shelling out, but I generally try to avoid that if possible.

PS: I meant to ask; why not use Puppet itself to manage that file ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Javascript regular expression, search function fhleung Programming 1 09-07-2014 01:17 AM
[SOLVED] AWK: Insert a blank line after each group of patterns matched cristalp Programming 6 10-22-2013 09:07 AM
Awk - substitution with parts of the matched regular expression used. c_moriarty Programming 4 04-30-2011 09:38 AM
Help with Regular Expression in VIM search and replace vijay_babu1981 Linux - Newbie 8 10-22-2009 08:49 AM
regular expression search/replace question (HELP!!! :) amytys Programming 5 09-06-2004 03:36 PM


All times are GMT -5. The time now is 12:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration