Solved

PHP variables to perl

Posted on 2012-03-25
8
344 Views
Last Modified: 2012-08-14
Hello,

Is it possible to pass PHP variables to my script that backups firewall configs.

I use expect on my script

#!/usr/bin/expect
0
Comment
Question by:somicoy
[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
  • 4
  • 4
8 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37763489
Assumption: I'm assuming Linux or some other Unix type O/S because you are using
#!/usr/bin/expect

I can think of a few different ways to solve your problem, keeping in mind that I'm really short on details as to what you want to do.

1. One solution is to use popen() to launch the perl script.  When you use popen() to launch the script and open a stream between your PHP script and the perl script's standard input.

The next two skip using expect.

2. Convert your PHP CGI to a perl CGI and do all the work in a single script.

3. Change your perl script so that it accepts command line options and parse the command line options.

I don't know which solution is best for you.  #1 seems to fit your request more directly.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37763493
3. clarification -- you sould then launch the perl script, with command line options, from PHP.
0
 

Author Comment

by:somicoy
ID: 37764416
Yes, im using centos 6.2

1. does popen() support expect ? i tried to run with this example
http://www.molecularsciences.org/PHP/proc_open_tutorial_and_examples
 -Executing Perl code from PHP


2. I really don't know much perl. So i rather to this with PHP

3. Can you provide some example?

Here little from my run.pl

#!/usr/bin/expect
....
set pw1 @ARGV;
.....
spawn "/bin/bash"
send "ssh ......."  //ssh connect

So the thing im trying to do is that i can use POST to write password for the script. ( I dont want to save password to the script)

Script works fine if i write the password to it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
Hugh McCurdy earned 500 total points
ID: 37765169
Yes, you are correct, you need a 2 way pipe and proc_open() appears to be the correct approach in PHP.  I haven't used proc_open() in PHP.  I've used pipe() in C which is a similar concept.  (I understand that the PHP interpreter is written in C.)

There are 3 ways to collect command line arguments in perl

1. Directly acccess ARGV[]    
http://www.cyberciti.biz/faq/howto-pass-perl-command-line-arguments/

2. Getopt::Std
3. Getopt::Long

The approach best for you depends on more information including what information are you trying to pass?

Since you don't know perl, my suggestion is to use proc_open().

But first, do you know (for sure) that the existing perl program doesn't already accept command line arguments?  (Since you don't know much about perl, I wonder how well written the perl program is.)  If it doesn't accept command line and since you don't know perl and aren't showing a desire to learn perl, I suggest pipes.
0
 

Author Comment

by:somicoy
ID: 37765253
perl doesn't accept command line arguments.
I just found the script online and made few changes of my own.

for the ssh/scp line i need to give password.

in the script there was:
set pw1 xxx;
....
expect "\nPassword:"
send "$pw1\r"
expect "#"

So i need pw1 to be like :set pw1 $passwd (this is from GET_POST['password']
0
 

Author Comment

by:somicoy
ID: 37765279
does command line arguments work with #!/usr/bin/expect ?

php:
$passwd = $_POST['password']; (works)
exec("/var/www/html/5505.exp ". $passwd);

5505.exp:
can't read "passwd": no such variable
    while executing
"$passwd=$ARGV[0]"
0
 

Author Comment

by:somicoy
ID: 37765339
set arg1 [lindex $argv 0]

that helped to get it working with expect !
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37765348
expect takes its own commands at the command line.  You can tell expect to read from a file.
http://linux.die.net/man/1/expect

If you can do any perl programming at all, you might want to replace expect with simply reading from the standard input.  Me, I'd be nervous about using expect for communications between two processes on the same computer.  The expect program is for something like login over a serial line or network.

As for your exec attempt, I suggest the following debugging
$passwd = $_POST['password']; // (works)
$command = "/var/www/html/5505.exp ". $passwd;
die ( $command );  // Then uncomment once you think $command is what you want.
exec("/var/www/html/5505.exp ". $passwd);

Test the other end by typing in the command by hand to see how 5505.exp interacts with you.  Make sure it works when you type by hand.  (If it doesn't, nothing in the PHP file is going to work until it 5505.exp works by hand.)
0

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

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