Perl script for getting inet from ip command - perl

I am trying to get the inet using ip command it is working fine on cmd prompt but if i add it in perl script, it is not executing as expected. Script is below:-
ip.pl
use strict;
my $a = `ip -f inet addr show eth0| grep -Po 'inet \K[\d.]+'`;
chomp($a);
print $a;
Executing above with "perl a.pl" returns nothing but "ip -f inet addr show eth0| grep -Po 'inet \K[\d.]+'" returns inet value.
How to get it executed using perl script?

Turn on warnings to get a hint:
Unrecognized escape \K passed through at ./1.pl line 5.
Unrecognized escape \d passed through at ./1.pl line 5.
Single quotes inside backticks aren't nested, you need to backslash the backslashes:
my $a = `ip -f inet addr show eth0| grep -Po 'inet \\K[\\d.]+'`;
Using $a for a lexical variable is wrong, it can cause weird bugs when sort is later used which uses $a as a special variable. Use a more meaningful name.
Moreover, calling grep from Perl is usually not needed, you can match the string in Perl itself:
my ($ip) = `ip -f inet addr show eth0` =~ /inet ([\d.]+)/;
or
my ($ip) = `ip -f inet addr show eth0` =~ /inet \K[\d.]+/g;

Related

awk command in Perl's system does not work

I am writing a small Perl script that executes an Awk command :
I try to swap two columns in a file, the file is like this :
domain1,ip1
domain2,ip2
domain3,ip3
the result should be
ip1,domain1
ip2,domain2
ip3,domain3
The Perl command invoking awk is like this:
system("ssh -p 22 root\#$mainip 'awk -F, '{print $2,$1}' OFS=, /root/archive/ipdomain.txt > /root/ipdom.txt'");
This is the error I get :
awk: cmd. line:1: {print
awk: cmd. line:1: ^ unexpected newline or end of string
any suggestions, please?
With the layered commands and all that multi-level quoting and escaping that need be done right,† no wonder it fails. A complex command like that will always be tricky, but libraries help a lot.
A properly quoted string to run through a shell can be formed with String::ShellQuote ‡
use warnings;
use strict;
use feature 'say';
use String::ShellQuote qw(shell_quote);
die "Usage: $0 file outfile\n" if #ARGV != 2;
my ($file, $out) = #ARGV;
my #cmd_words =
( 'ssh', 'hostname', 'awk', q('{print $2 $1}'), $file, '>', $out );
my $cmd = shell_quote #cmd_words;
system($cmd);
Note how the q() operator from of single quotes enables us to pass single quotes nicely.
This swaps the first two words on each line of a file and prints them, using awk, and redirects the output to a file, on a remote host. It works as expected in my tests (with a real hostname). Please adjust as needed.
Another possible improvement would be to use a library for ssh, like Net::OpenSSH.
A complete command, as the one in the question, to use in the above program
my #cmd_words = (
'ssh', '-p', '22', "root\#$mainip",
'awk', '-F,', q('{print $2,$1}'), 'OFS=,', $file, '>', $out );
Tested with a file from the question.
The makeVoiceBot answer is informative and it got half way there but I find the need for
system("ssh hostname \"awk '{print \\\$2 \\\$1}' $path\"");
This works in my tests (on systems I ssh to). I try to avoid needing to deal with such quoting and escaping.
† This is a shell command which runs ssh, and then executes a command on the remote system which runs a shell (there) as well, in order to run awk and redirect its output to a file.
A bit more than an "awk command" as the title says.
‡ The library can prepare a command for bash (as of this writing), but one can look at the source for it and adjust it for their own shell, at least. There is also Win32::ShellQuote
I am using a shortened example here
system("ssh localhost 'awk '{print $2,$1}' file.txt'")
system() sees:
ssh localhost 'awk '{print $2,$1}' file.txt'
local shell expands:
ssh
localhost
awk
{print
$2,$1}
file.txt
local shell replaces $1 and $2 (positional args) with empty strings:
ssh
localhost
awk
{print
,}
file.txt
ssh executes:
ssh localhost awk {print ,} file.txt
remote shell gets:
awk
{print
,}
file.txt
So the remote shell runs awk with {print as its program argument, resulting in the described error. To prevent this, the invocation of system() can be changed to;
system("ssh localhost \"awk '{print \$2,\$1}' file.txt\"")
system() sees:
ssh localhost "awk '{print \$2,\$1}' file.txt"
local shell expands:
ssh
localhost
awk '{print \$2,\$1}' file.txt
ssh executes
ssh localhost awk '{print \$2,\$1}' file.txt
remote shell gets
awk
{print \$2,\$1}
file.txt
remote shell expands \ escapes
awk
{print $2,$1}
file.txt
Remote awk now gets {print $2,$1} as its program argument, and executes successfully.

Executing grep in perl always returns and empty string

When I run grep from my command line in CentOS 5.8 using the following command I get
grep -E "APPLIANCE=\"VPMX\"" /filepath/appliance_data.sh
with the results of
APPLIANCE="VPMX"; export APPLIANCE
When I run the following commands perl
$out = `grep -E "APPLIANCE=\"VPMX\"" /filepath/appliance_data.sh`;
`echo "Output grep TV: $out" >> /tmp/debug`;
the $out variable is always an empty string
How do I get the same output as the command line grep?
I have tried quiet grep using the -q parameter, and have also tried the command with < /dev/null with no change in the result.
You need to escape your backslashes as backticks work like a double quoted string by default:
$out = `grep -E "APPLIANCE=\\"VPMX\\"" /filepath/appliance_data.sh`;
Alternatively, you could the single quoted form of qx:
$out = qx'grep -E "APPLIANCE=\"VPMX\"" /filepath/appliance_data.sh';

grep: can't open "command"

I am logging in to the remote machine and executing a command and then grepping the result. I am using Expect module. Here is my sample code.
use Expect;
my $exp=new Expect();
$exp->spawn("ssh $hostname\r");
$exp->expect(5,"*]-> ");
$exp->send("command sent here \r");
$exp->expect(5,"*]-> ");
my $res=$exp->before(); // Here i ll get the command output in a variable. The variable contains TCPIP:1.1.1.1 in one line and UDPIP:1.2.2.2 in another line.
my $id=`grep -i TCPIP $res | cut -d ":" -f2 `;
print " The result is $id \n";
But here i am getting an error
grep: can't open "command sent " .sh: TCPIP not found sh:UDPIP not found.
Maybe you can do even without expect?
my $id = `ssh $hostname <your_command> | grep -i TCPIP | cut -d":" -f2`;
grep takes a filename as paramater not a string. You need something like this:
echo $res | grep -i TCPIP | cut -d ":" -f2 `;
Make sure the command works manually via ssh first though.
But consider using perl itself do do the match and cut rather than spawning out to grep, that would be better.

Executing shell command with pipe in perl

I want the output of the shell command captured in variable of a perl script, only the first section of the command before the pipe "|" is getting executed, and there is no error while executing the script
File.txt
Error input.txt got an error while parsing
Info output.txt has no error while parsing
my $var = `grep Error ./File.txt | awk '{print $2}'`;
print "Errored file $var";
Errored file Error input.txt got an error while parsing
I want just the input.txt which gets filtered by awk command but not happening. Please help
The $ in $2 is interpolated by Perl, so the command that the shell receives looks like:
grep Error ./File.txt | awk '{print }'
(or something else if you have recently matched a regular expression with capture groups). The workaround is to escape the dollar sign:
my $var = `grep Error ./File.txt | awk '{print \$2}'`
Always include use strict; and use warnings; in EVERY perl script.
If you had, you'd have gotten the following warning:
Use of uninitialized value $2 in concatenation (.) or string at scratch.pl line 4.
This would've alerted you to the problem in your command, namely that the $2 variable is being interpolated instead of being treated like a literal.
There are three ways to avoid this.
1) You can do what mob suggested and just escape the $2
my $var = `grep Error ./File.txt | awk '{print \$2}'`
2) You can use the qx form of backticks with single quotes so that it doesn't interpolate, although that is less ideal because you are using single quotes inside your command:
my $var = qx'grep Error ./File.txt | awk \'{print $2}\''
3) You can just use a pure perl solution.
use strict;
use warnings;
my ($var) = do {
local #ARGV = 'File.txt';
map {(split ' ')[1]} grep /Error/, <>;
};

How can I grep for a value from a shell variable?

I've been trying to grep an exact shell 'variable' using word boundaries,
grep "\<$variable\>" file.txt
but haven't managed to; I've tried everything else but haven't succeeded.
Actually I'm invoking grep from a Perl script:
$attrval=`/usr/bin/grep "\<$_[0]\>" $upgradetmpdir/fullConfiguration.txt`
$_[0] and $upgradetmpdir/fullConfiguration.txt contains some matching "text".
But $attrval is empty after the operation.
#OP, you should do that 'grepping' in Perl. don't call system commands unnecessarily unless there is no choice.
$mysearch="pattern";
while (<>){
chomp;
#s = split /\s+/;
foreach my $line (#s){
if ($line eq $mysearch){
print "found: $line\n";
}
}
}
I'm not seeing the problem here:
file.txt:
hello
hi
anotherline
Now,
mala#human ~ $ export GREPVAR="hi"
mala#human ~ $ echo $GREPVAR
hi
mala#human ~ $ grep "\<$GREPVAR\>" file.txt
hi
What exactly isn't working for you?
Not every grep supports the ex(1) / vi(1) word boundary syntax.
I think I would just do:
grep -w "$variable" ...
Using single quotes works for me in tcsh:
grep '<$variable>' file.txt
I am assuming your input file contains the literal string: <$variable>
If variable=foo are you trying to grep for "foo"? If so, it works for me. If you're trying to grep for the variable named "$variable", then change the quotes to single quotes.
On a recent linux it works as expected. Do could try egrep instead
Say you have
$ cat file.txt
This line has $variable
DO NOT PRINT ME! $variableNope
$variable also
Then with the following program
#! /usr/bin/perl -l
use warnings;
use strict;
system("grep", "-P", '\$variable\b', "file.txt") == 0
or warn "$0: grep exited " . ($? >> 8);
you'd get output of
This line has $variable
$variable also
It uses the -P switch to GNU grep that matches Perl regular expressions. The feature is still experimental, so proceed with care.
Also note the use of system LIST that bypasses shell quoting, allowing the program to specify arguments with Perl's quoting rules rather than the shell's.
You could use the -w (or --word-regexp) switch, as in
system("grep", "-w", '\$variable', "file.txt") == 0
or warn "$0: grep exited " . ($? >> 8);
to get the same result.
Using single quote it wont work. You should go for double quote
For example:
this wont work
--------------
for i in 1
do
grep '$i' file
done
this will work
--------------
for i in 1
do
grep "$i" file
done