I am trying to connect a unix server from php and execute .exe (C language). Earlier I used phptelnet for this purpose, but now I need to shift to phpseclib due to security issues. I had 100% success rate when I use phptelnet. I could run some of the external programs like 'C' programs with arguments as input in php script. In phptelnet I use
$telnet->DoCommand('cd public_html');
$telnet->DoCommand('cd abc');
$telnet->DoCommand('demo.exe');
$telnet->DoCommand("$inputs", $result);
echo $result;
This works perfect. But now I am using phpseclib. I could connect to the unix server via ssh and execute programs in which the inputs are hard coded in the program. I am using
echo $ssh->exec('./demo.exe');
Now the problem is how to provide inputs to the program. How can I use exec() to accept arguments as inputs. For example, demo.exe is a program to add two numbers. so can I say
echo $ssh->exec("./demo.exe, '10 20'");
Also how can I use exec() to execute multiple lnes of code in a single execution. I am a bit confused. Any inputs on this are greatly appreciated.
Thanks in advance.
Where you have multiple options to commands you'd use:
$ssh->exec("./demo.exe '10' '20'");
Leave out the comma, and enclose individual parameters each. Or convert a list with $opts = implode(" ", array_map("escapeshellarg", $opts)).
If you are using a SSH1 connection, you can only execute one command at a time. If you are connecting to a Windows server (which this looks like), then you can't have two commands on a line.
Only for a BSD/Linux server you could use:
$ssh->exec("cmd1 ; cmd2");
Related
Still in need of help :)
I'm trying to adapt the following chunk of code to VBA for Mac (as the final implementation has to be on Mac):
Dim ws as Object
Dim result as String
Set ws = VBA.CreateObject("wscript.shell")
cd = "php " & dirPHP & "\verificar.php " & FileName
result = ws.Run(cd)
Set ws = Nothing
It runs perfectly on Windows, but when trying to adapt it for Mac I'm encountering many problems.
What I am basically doing on the previous chunk is calling a PHP file that takes the first argument (in this case, FileName) and calls the verify function, returning some values.
The thing is that there are some posts explaining how to do this, but I have seen no examples on how to do it for PHP, and especially for PHP passing an input argument.
This is what I've tried so far:
result = AppleScriptTask("PHPCommand.applescript", "PHPCommandHandler", FileName)
e = "php " & dirPHP & "/verificar.php " & FileName cd = "do shell script """ & e & """" result = MacScript(cd)
(On the Mac Terminal I am able to run the PHP file fine, with the resulting "e" string).
And some other fruitless things, like the shell() function, or some other user-defined functions (I saw someone defined a "system()" function). I also tried many ways of putting the double and simple quotes, and simplified the path to the PHP file (dirPHP) and the path + filename of the argument (FileName) by removing all blank spaces and thus the need of using additional quotes.
Please help me! I'd be really grateful, as yesterday I spent the whole day on this and I can't keep wasting time on something that is so simple on Windows... But I have to do it on Mac.
Thanks so much!!!
Use the VBA Shell function.
Shell
Runs an executable program and returns a Variant (Double) representing
the program's task ID if successful; otherwise, it returns zero.
Syntax
Shell(pathname, [ windowstyle ])
Currently I need to merge that 50+ PDF files into 1 PDF. I am using PDFTK. Using the guide from: http://www.johnboy.com/blog/merge-multiple-pdf-files-with-php
But it is not working. I have verified the following:
I have tried the command to merge 2 pdfs from my PHP and it is working.
I have echo the final command and copied that command and paste into command prompt and run manually and all the 50 PDFs are successfully merged.
Thus exec in my PHP and the command to merge 50 PDFs are both correct but it is not working when done together in PHP. I have also stated set_time_limit(0) to prevent any timeout but still not working.
Any idea what's wrong?
You can try to find out yourself:
print exec(str_repeat(' ', 5000) . 'whoami');
I think it's 8192, at least on my system, because it fails with strings larger than 10K, but it still works with strings shorter than 7K
I am not sure if there is a length restriction on how long a single command can be but I am pretty sure you can split it accross multiple lines with "\" just to check if thats the problem. Again I dont think it is... Is there any error output when you try to run the full command with PHP and exec, also try system() instead of exec().
PDFTK versions prior to 1.45 are limited to merge 26 files cuz use "handles"
/* Collate scanned pages sample */
pdftk A=even.pdf B=odd.pdf shuffle A B output collated.pdf
as you can see "A" and "B" are "handles", but should be a single upper-case letter, so only A-Z can be used, if u reach that limit, maybe you script outputs an error like
Error: Handle can only be a single, upper-case letter
but in 1.45 this limitation was removed, changelog extract
You can now use multi-character input handles. Prior versions were
limited to a single character, imposing an arbitrary limitation on
the number of input PDFs when using handles. Handles still must be all
upper-case ASCII.
maybe you only need update your lib ;)
I have a great Python program on my webserver, which I want to use from inside my PHP web app.
Here's an example of the python command, and output as you would see it in terminal:
>>> print MBSP.parse('I ate pizza with a fork.')
I/PRP/I-NP/O/NP-SBJ-1/O/i
ate/VBD/I-VP/O/VP-1/A1/eat
pizza/NN/I-NP/O/NP-OBJ-1/O/pizza
with/IN/I-PP/B-PNP/O/P1/with
a/DT/I-NP/I-PNP/O/P1/a
fork/NN/I-NP/I-PNP/O/P1/fork ././O/O/O/O/.
You might recognize this as a typical POS tagger.
In any case, I'm confused about how to use a PHP-based web app to send this program a string like "I ate pizza with a fork", and somehow get the response back in a way that can be further parsed in PHP.
The idea is to use PHP to pass this text to the Python program, and then grab the response to be parsed by PHP by selecting certain types of words.
It seems like in PHP the usual suspects are popen() and proc_open(), but popen() is only for sending, or receiving information - not both? Is popen() able to give me access to this output (above) that I'm getting from the Python program? Or is there a better method? What about curl?
Here are all my options in terms of functions in PHP:
http://us.php.net/manual/en/function.proc-open.php
I'm lost on this, so thanks for your wise words of wisdom!
I use exec() for this purpose.
exec($command, $output);
print_r($output);
If you want to get a little heavier / fancier... give your python script an http (or xmlrpc) front end, and call that with a GET/POST. Might not be worth all that machinery though!
You could use popen(), and pass the input to your Python script as a command line argument, then read the output from the file descriptor popen gives you, or proc_open() if you want to interact bi-directionally with the Python script.
Example 1 in the proc_open manual: http://us.php.net/manual/en/function.proc-open.php gives an example of this.
If your Python needs it as stdin, you could try popening a command line:
echo "I ate pizza!"|my_python_progam.py
and just read the output. As usual, do proper input validation before sending it to the command-line.
Something like this would work
$command = '/usr/bin/python2.7 /home/a4337/Desktop/script.py'
$pid = popen('$command',r)
........
........
.........
pclose($pid)
I have a program on my linux server that asks the same series of questions each time it executes and then provides several lines of output. My goal is to automate the input and output with a php script.
The program is not designed to accept input on the command line. Instead, the program asks question 1 and waits for an answer from the keyboard, then the program asks question 2 and waits for an answer from the keyboard, etc.
I know how to capture the output in an array by writing:
$out = array();
exec("my/path/program",$out);
But how do I handle the input?
Assume the program asks 3 questions and valid answers are: left 120 n
What is the easiest way using php to pass that input to the program?
Can I do it somehow on the exec line?
I’m not a php noob but simply have never needed to do this before.
Alas, my googling is going in circles.
First up, just to let you know that you're trying to reinvent the wheel. What you're really looking for is expect(1), which is a command-line utility intended to do exactly what you want without involving PHP.
However, if you really want to write your own PHP code you need to use proc_open. Here are some good code examples on reading from STDOUT and writing to STDIN of the child process using proc_open:
http://www.php.net/manual/en/function.proc-open.php#79665
How to pass variables as stdin into command line from PHP
http://camposer-techie.blogspot.com/2010/08/ejecutando-comandos-sobre-un-programa.html (this one is in Spanish, sorry, but the code is good)
Finally, there is also an Expect PECL module for PHP.
Hope this helps.
Just add the arguments to the exec line.
exec("/path/to/programname $arg1 $arg2 $arg3");
... but don't forget to apply escapeshellarg() on every argument! Otherwise, you're vulnerable to injected malicious code.
$out = array();
//add elements/parameters/input to array
string $execpath = "my/path/program ";
foreach($out as $parameter) {
$execpath += $parameter;
//$execpath += "-"+$execpath; use this if you need to add a '-' in front of your parameters.
}
exec($execpath);
Does file_get_contents maintain line breaks? I thought it did but I have tried this:
if($conn){
$tsql = file_get_contents('scripts/CreateTables/SLR05_MATCH_CREATETABLES.sql');
$row = sqlsrv_query($conn, $tsql);
print_r(sqlsrv_errors());
}
The errors I get is that SQL Server complains that there is incorrect syntax. I get the same errors when I run the SQL Script without any line breaks, which suggests file_get_contents removes them?
When I run the script normally (open the file in SQL Server Management Studio) and execute it, it works perfectly.
So is there something that I can use that maintains line breaks etc? Or is there another problem here in using queries from a file with the SQL Server PHP Driver from Microsoft?
Thanks all for any help
file_get_contents() does preserve the file. Perhaps your file contains the wrong "type" of line-breaks? Linux -vs- Windows style?
Also, I'm not familiar with sqlsrv_query() ... can that be used to run multiple queries (as a script might), or only single queries, one at a time?