?
Solved

Perl issue with @ in backticks for curl

Posted on 2014-01-23
6
Medium Priority
?
1,291 Views
Last Modified: 2014-05-22
I have a perl script that calls a curl command via backticks.
Because of the locked down environment I work in, I can not add any modules to the basic perl install ( 5.05 on most or 5.8.8 on the newest machines).
I have a problem with the @ in a curl command that allows input from a file: curl -d@filename

perl compalins that   :
Can't use string ("home/xymon/server/ext/check_webl") as an ARRAY ref while "strict refs" in use at ./xy_weblogic_chk.pl line 155.
 `$CURL --connect-timeout 5 -m 15 -d@$XML_FILE  http://$opt_IP/middleware 2>&1 | grep "/result"  > $infile` ;
 this cmmand works fine in a shell script
 
 How do I escape or fix the command to get it to work in backticks  or other solution.

 
 Thanks in advance.
0
Comment
Question by:SysExpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39804269
Did you try to escape the @ with \ ?

`$CURL --connect-timeout 5 -m 15 -d\@$XML_FILE  http://$opt_IP/middleware 2>&1 | grep "/result"  > $infile` ;
0
 
LVL 63

Author Comment

by:SysExpert
ID: 39804322
That messes up the curl command

curl: (7) Failed to connect to 0.0.0.1: Invalid argument
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39804429
Strange. \ is a valid escape char in perl.

You could try single quotes, but that's just a guess:

`$CURL --connect-timeout 5 -m 15 -d '@'$XML_FILE  http://$opt_IP/middleware 2>&1 | grep "/result"  > $infile` ;
0
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
LVL 84

Expert Comment

by:ozo
ID: 39804688
What would the curl command be if you gave it directly instead of from perl?

Also, what is in $opt_IP?
0
 
LVL 62

Expert Comment

by:gheist
ID: 39808528
Better use perl module instead of external program.
http://curl.haxx.se/libcurl/perl/easy.pm.html
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 2000 total points
ID: 39846715
You end up in a world of pain when you use unescaped variables as parameters in backticks - this is dangerous, it can even be a security issue (code injection).

So better escape them:
sub shell_escape{$_=shift; s/\'/\'\"\'\"\'/g; $_;}

$XML_FILE_esc = shell_escape($XML_FILE);
...
`$CURL --connect-timeout 5 -m 15 -d \@'$XML_FILE_esc'  http://$opt_IP/middleware 2>&1 | grep "/result"  > $infile` ;

And unless you need stdout in a Perl variable, don't use backticks. Use system() instead. You can create the system() call's shell string in a separate variable which you can easily verify first before executing it.

What you are trying to do looks like "Perl-enhanced shell scripting", which normally gets messy very fast.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Six Sigma Control Plans
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question