Chef spec tests with not_if guard - chef-recipe

I have the following chef resoutce
rpm_package 'Install BESAgent' do
source node['besclient']['package_url']
action :install
not_if { "rpm -qa | grep -qx 'BESAgent-9.5.9.62-rhe6.x86_64'" || "rpm -qa | grep -qx 'BESAgent-9.5.9.62-sle11.x86_64'" }
end
Corresponding to this I Have the following chef spec test
it 'installs rpm_package for BES Client' do
expect(chef_run).to install_rpm_package('Install BESAgent').with(source: 'http://software.bigfix.com/download/bes/95/BESAgent-9.5.9.62-rhe6.x86_64.rpm' , not_if: '{ \"rpm -qa | grep -qx \'BESAgent-9.5.9.62-rhe6.x86_64\'\" || \"rpm -qa | grep -qx \'BESAgent-9.5.9.62-sle11.x86_64\'\"}'))
end
which gives a warning
[2018-04-17T21:18:03+05:30] WARN: not_if block for rpm_package[Install BESAgent]
returned "rpm -qa | grep -qx 'BESAgent-9.5.9.62-rhe6.x86_64'", did you mean to
run a command? If so use 'not_if "rpm -qa | grep -qx 'BESAgent-9.5.9.62-rhe6.x86_64'"' in your code.
and the following error
1) besclient::linux redhat 6.8: when all attributes are default installs rpm_
ackage for BES Client
Failure/Error: expect(chef_run).to install_rpm_package('Install BESAgent')
with(source: 'http://software.bigfix.com/download/bes/95/BESAgent-9.5.9.62-rhe6
x86_64.rpm', not_if: '{ \"rpm -qa | grep -qx \'BESAgent-9.5.9.62-rhe6.x86_64\'\
|| \"rpm -qa | grep -qx \'BESAgent-9.5.9.62-sle11.x86_64\'\"}')
expected "rpm_package[Install BESAgent]" actions [] to include :install

When you use command inside not_if block, you don't need to use curly braces. You can use simply use double or single quotes
not_if "rpm -qa | grep -qx 'BESAgent-9.5.9.62-rhe6.x86_64'" || "rpm -qa | grep -qx 'BESAgent-9.5.9.62-sle11.x86_64'"

Related

Issue in executing a command in perl

I have the below code:
chdir glob "/home/test/test1/test2/perl*";
#testList = exec "cat test.in | grep build | awk '{print \$2}'";
foreach my $testList(#testList) {
chdir "/home/test/test1/$testList";
exec "cat test.out | grep -w 'PASSED'";
}
After I run the above code, it gives the output for only the below:
chdir glob "/home/test/test1/test2/perl*";
#testList = exec "cat test.in | grep build | awk '{print \$2}'";
but not the other two lines below. I only want the output for the lines:
foreach my $testList(#testList) {
chdir "$opts->{/home/test/test1$testList";
exec "cat test.out | grep -w 'PASSED'";
and not
chdir glob "/home/test/test1/test2/perl*";
#testList = exec "cat test.in | grep build | awk '{print \$2}'";
Please help.
exec and system are returning exit code.
Try:
#testList = `cat test.in | grep build | awk '{print \$2}'`;

Escaping pipe in perl

I'm having some trouble with backtics and pipe in perl. I have following code:
my #arr_lsdev = `lsdev -C | grep inet | awk '{print \$1}'` ;
print Dumper #arr_lsdev ;
But I get following error:
sh[2]: 0403-057 Syntax error : `|' is not expected
I'm guessing it has something to with my escape commands. I have tried escaping the | but it still results in the same error.
OS: AIX
Shell: KSH
Notice that the error is on line 2. You are actually executing
my #arr_lsdev = `lsdev -C | grep inet
| awk '{print \$1}'` ;
You can reduce the number of pipes:
my #arr_lsdev = map {(split ' ')[0]} grep {/inet/} `lsdev -C`;

How does ps aux | grep '[p]attern' exclude grep itself?

The title says it all. I've seen this idiom used alot instead of adding an additional grep -v grep in some ps pipeline. For example it could be used like this:
$ ps aux | grep '[f]irefox' | awk '{ print $8 }'
instead of
$ ps aux | grep 'firefox' | grep -v grep | awk '{ print $8 }'
It's super-convenient, but how does it work and why?
The pattern [f]irefox will not match the literal string [f]irefox. Instead it will match strings with exactly one char from the 1-character class [f], followed by irefox.

Perl telnet command not sending every command

I have the following program below which telnets into another device and prints serial number and Mac address.
My problem is that for some reason if I send the command once it skips the first command and sends the second, but if I copy the same command twice it will send the command.
What is the correct way to send a command multiple commands successively?
Should the buffer be flushed after every command sent ?
My Env
Eclipse Ide
Ubuntu 12.10
perl 5, version 14, subversion 2 (v5.14.2)
Snippet of my code:
$telnet = Net::Telnet->new($remoteSystem);
$| = 1;
$telnet->buffer_empty();
$telnet->buffer_empty();
$result = $telnet->input_log($errorlog);
#$_ = "#lines";
#TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2');
#TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2');
#mac = $telnet->cmd('ifconfig | grep eth0 | cut -d" " -f 11');
print "#TSN AND #TSN #mac";
print FH "$remoteSystem\n";
print "Telnetting into $remoteSystem .\n"; # Prints names of the tcd
close(telnet);
}
foreach (#host) {
checkStatus($_);
}
OUTPUT That skips the first command:
bash-2.02 AND bash-2.02 ifconfig | grep eth0 | cut -d" " -f 11
00:11:D9:3C:6E:02
bash-2.02 #
bash-2.02 Telnetting into debug79-109 .
OUTPUT That works but I have to send the same command twice:
export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2
AE20001901E2FD1
bash-2.02 #
bash-2.02 AND export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2
AE20001901E2FD1
bash-2.02 #
bash-2.02 ifconfig | grep eth0 | cut -d" " -f 11
00:11:D9:3C:6E:02
bash-2.02 #
bash-2.02 Telnetting into debug79-109
Specify the command prompt in your call to cmd(), e.g.#TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2', Prompt => 'bash-2.02 #');
Try opening a connection after creating a object for the module telnet
$telnet->open($host);
After which execute waitFor method:(waits until the pattern bash-2.02 # comes)
$telnet->waitFor(/^(bash-\d+.\d+ #)$/);
and then execute your commands , it would give you proper output.

while loop align columns

hi
I wrote a perl script where I stored columns from a text file filled with ip and port scans into variables. The variables contain many ip addreses, ports, protocols, states, and services now I need a while loop that takes all the ip's stored in the ip variable and matches them with its corresponding ports protocols and state etc.side by side look so:
192.168.3 45 tcp open smtp
heres my code
$ip_address = `cat /cygdrive/c/Windows/System32/test11.txt |
grep 'Nmap scan report for'`;
$state = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'PORT'|
grep -v 'filtered'| grep -v 'latency'| grep -v 'Nmap' | grep -v 'Discovered' |
grep -v 'Raw' | grep -v 'SYN' | grep -v 'DNS'| grep -v 'Ping' |
grep -v 'Scanning' `;
$port = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'Discovered'|
grep -v 'Nmap' | grep -v 'PORT' | grep -v 'ports'| grep -v 'Read' |
grep -v 'Raw'| grep -v 'Completed'| grep -v 'DNS' | grep -v 'hosts' |
grep -v 'Ping' | grep -v 'SYN' | grep -v 'latency' `;
$protocol = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'Discovered'|
grep -v 'Nmap' | grep -v 'PORT' | grep -v 'ports'| grep -v 'Read' |
grep -v 'Raw' | grep -v 'Completed'| grep -v 'DNS' | grep -v 'hosts' |
grep -v 'Ping' | grep -v 'SYN' | grep -v 'latency' `;
{
$service = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'Nmap' |
grep-v 'Host' | grep -v 'filtered' | grep -v 'PORT' | grep -v 'Raw'|
grep -v 'Scanning'| grep -v 'Completed'| grep -v 'Ping' |grep -v 'DNS' |
grep -v 'Discovered'| grep -v 'SYN'`;
while($ip_address, $port, $protocol, $state, #service)
{
chomp ($ip_address, $port, $protocol, $state, #service);
print "$ip_address, $port, $protocol, $state, #service";
exit 0;
}
Usually I say to use the tools that you understand and that it's OK to do things like call awk from Perl. But for this code I'll make an exception. You should be using the builtin Perl commands for this task. Namely, arrays and Perl's grep operator. Here's how I would start to rewrite this.
# do this once instead of `cat ...` several times.
open my $fh, '<', '/cygdrive/c/Windows/System32/test11.txt';
my #the_input = <$fh>;
close $fh;
# do this instead of `| grep -v ... | grep -v ...`
my #ip_addresses = grep { /Nmap scan report for/ } #the_input;
my #states = grep {
!/PORT|filtered|latency|Nmap|Discovered|Raw|SYN|DNS|Ping|Scanning/
} #the_input;
my #ports = grep {
!/Discovered|Nmap|PORT|ports|Read|Raw|Completed|DNS|hosts|Ping|SYN|latency/
} #the_input;
my #protocols = grep {
!/Discovered|Nmap|PORT|ports|Read|Raw|Completed|DNS|hosts|Ping|SYN|latency/
} #the_input;
my #services = grep {
!/Nmap|Host|filtered|PORT|Raw|Scanning|Completed|Ping|DNS|Discovered|SYN/
} #the_input;
I usually try to do these sorts of things in one pass, like...
#!/usr/bin/perl
open(F, "/cygdrive/c/Windows/System32/test11.txt");
while(<F>) {
# If the current line has something that matches an IP
# address, store the matched pattern in $ip. We'll
# use this as we process the remaining lines.
#
$ip = $1 if ( /Nmap scan report for (\d+\.\d+\.\d+\.\d+)/ );
# Try to match lines like "ddd/www www www wwww"
#
( $port, $protocol, $state, $service) = ( m|(\d+)/(\w+)\s+(\w+)\s+(\w+)| );
print "$ip, $port, $protocol, $state, $service\n" if $port;
}