Solved

Perl issue with @ in backticks for curl

Posted on 2014-01-23
6
1,250 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
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 500 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Six Sigma Control Plans

717 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