As am very new to php.
I have the following command as:
cd E:\PHP\home_php\img\tmp
E:
montage -mode concatenate -tile 3x3 *.png result.png
I want to execute the command from cmd.exe in php(if possible from javascript too).
Right now I have to execute .bat file but that file has some static value. I need to pass few dynamic value. How can I open cmd with required command.
I tried php montage but I need .bat kind of thing in cmd from php/JS.
Try this :echo exec('your command');
http://php.net/manual/en/function.exec.php you can also use backticks for some weird reason: http://php.net/exec
You can use number of PHP funcions to execute system code:
shell_exec("your command") would execute code and return an output to you
system("your command", $returnCode) would execute your command and return numerical status of your command
exec("your command", $outputArray, $returnCode) would execute command and return your output line by line in outputArray + return code
Before asking such a simple question you really should first go to http://php.net and search for required functionality instead of asking it here.
Related
I am learning to use electron js with python and I am using python-shell so I have the following simple python script:
import sys, json
# simple JSON echo script
for line in sys.stdin:
print(json.dumps(json.loads(line)))
and in my main.js:
let {PythonShell} = require('python-shell')
let pyshell = new PythonShell('/home/bassel/electron_app/pyapp/name.py', {mode : 'json'});
pyshell.send({name:"mark"})
pyshell.on('message', function (message) {
// received a message sent from the Python script (a simple "print" statement)
console.log("hi");
});
but the hi is not getting printed, what is wrong?
This problem can also occur when trying to suppress the newline from the end of print output. See Why doesn't print output show up immediately in the terminal when there is no newline at the end?.
Output is often buffered in order to preserve system resources. This means that in this case, the system holds back the Python output until there's enough to release together.
To overcome this, you can explicitly "flush" the output:
import sys, json
# simple JSON echo script
for line in sys.stdin:
print(json.dumps(json.loads(line)))
sys.stdout.flush() # <--- added line to flush output
If you're using Python 3.3 or higher, you may alternatively use:
import sys, json
# simple JSON echo script
for line in sys.stdin:
print(json.dumps(json.loads(line)), flush=True) # <--- added keyword
I am trying to run a python script, and gather the printed output sent to the console using the following code in Node/Express:
app.post("/compute", (req, res) => {
var data = req.body.mat;
const value = runScript(data);
value.stdout.on("data", (data) => {
console.log(data.toString());
});
});
function runScript(data) {
return spawn("python", [
"-u",
path.join(__dirname, "scripts/guess.py"),
"--data",
data,
]);
}
It runs the following python script (simplified):
def main(argv):
opt, data = argv
model.load_state_dict(torch.load("model.pt")
print("Output")
if __name__ == "__main__":
main(sys.argv[1:])
The python script does exactly what it is meant to do when it is run in the terminal using python guess.py --data 0010100101000. The Node/Express does exactly what it is supposed to do, except when the print statement in the python code is after the line model.load_state_dict(torch.load("model.pt")). When the print statement is before this line, the Node code catches the python output properly.
Does this have something to do with the method taking too long, and the Node code not catching new stdouts anymore? Or does it have to do with the python code accessing another file, messing with the stdout? Any help would be appreciated.
I found that this line was throwing an error because the current working directory is different when I run the script from the terminal vs the node code. I just edited the path I passed to the method.
In my code I have a button that calls a JS file, which in turn calls a PHP file that has the API endpoints that I need to generate certificate, and sends the response back that I use in certificate variable below.
I'm generating the CSR myself using the openssl command.
My code to install cert on the browser is as follows (certificate is where I pass the certificate as response that I get from Entrust APIs)-
I'm using the code off of this page- https://blogs.msdn.microsoft.com/alejacma/2009/01/28/how-to-create-a-certificate-request-with-certenroll-javascript/ (the 2nd grey block right below- following Javascript sample shows how to install the response from the CA)
function installCertificate(certificate) {
try {
var objEnroll = objCertEnrollClassFactory.CreateObject("X509Enrollment.CX509Enrollment");
objEnroll.Initialize(1); // ContextUser
objEnroll.InstallResponse(0, certificate, 6, "");
} catch (ex) {
swal('Error', 'Something went wrong installing Client Certificate', 'error');
console.log("exception- " + ex.description);
}
}
The error that I have is (from the catch block)-
CertEnroll::CX509Enrollment::InstallResponse: Cannot find object or property. 0x80092004 (-2146885628 CRYPT_E_NOT_FOUND)
I'm not sure what it means by Cannot find object or property as it's not too verbose.
PS: If I save the response from the API as a .crt file and open it (simple double click), the certificate values look correct along with the certificate chain.
After a lot of trawling I finally made it work. For anyone wondering, heres my solution-
The part of the code that generates the CSR is the 1st grey block on this page-
https://blogs.msdn.microsoft.com/alejacma/2009/01/28/how-to-create-a-certificate-request-with-certenroll-javascript.
The CSR that it comes back with has a -----BEGIN NEW CERTIFICATE REQUEST----- at the top and -----END NEW CERTIFICATE REQUEST----- at the bottom, and line breaks after each line.
I removed the top line and all line breaks using preg_replace like below, and as at this point the CSR does not have a line break and is just a simple string, I used a str_replace to remove the last part of it.
$csr = preg_replace('/^.+\n/', '', $csr);
$csr = str_replace("-----END NEW CERTIFICATE REQUEST-----","", $csr);
TLDR- Removed the top and bottom line from the CSR and all the line breaks that were there. I think it had to do with how the CSR was formatted.
For executing a single query script (query.js) from the command line the following code was enough.
mongo db-name < query.js
I would like to execute (or match) multiple query files, such as query1.js, query2.js and so on. I tried the following code with no success.
mongo db-name < query*
Please help me out here.
mongo will not allow you to do it you will get an amibgous redirect error. Do this
cat query* | mongo --nodb
the | takes the output of cat query* passes it as input to mongo which in turn executes any thing it gets. cat does not execute the queries it only outputs the content of the files and passes it to mongo.
I am trying to get the output from my node.js script via PHP exec thats wrapped inside an ajax call I am able to call it i think and get some feed back, but console.log doesn't seem to return to the output var
This is how i call the script
$ex = exec('/usr/local/bin/node '.$path.'doAjax.js >/dev/null/ 2>&1 &',$_POST,$out);
var_dump($ex,$out);
In doAjax.js i do
console.log('hhhhhhhhhhhhh');
But all i get for output is
string(0) "" array(0) { }
Is there another way todo output or capture it ?
yes the code and output where switched, but then i always got code 8
i semi solved this problem by moving code to the httpd doc root and changing to shell_exec
though i still need to make sure apache won't be able to display the node code