I am trying to run the command prompt using a php script on camps but I am unable to do it.
I could run the Explorer using
exec("explorer");
But when I try to run
exec("C:\Windows\system32\cmd.exe");
It doesn't execute
How do it do it?
I want to run a command like
ping Google.com
This reads commands from the standard input and executes them:
while (true) {
$command = readline("Command: ");
passthru($command);
}
Note that if you run a command like ping, you might not be able to stop it this way (usually you would hit Ctrl + C). However you can specify the number of pings to send:
ping -c 3 google.com
You can use the "exec()" function to run the code in background. The command prompt won't be displayed but will directly run the code.
Now if you had to run a python script from php:
You could use
exec("py "location of python script");
exec('py C:\xampp\htdocs\pro\helloworld.py');
The output would be in the directory where your php script is present.
Related
I'm trying to execute a command on my Raspberry Pi via SSH and get the result of it in my PHP script on my Windows machine. Currently I can execute the command on my RasPi, but I do not get any results back into the PHP script.
The code I'm Using for this:
<?php
$cmd = "C:\\path_to_putty\\putty.exe -ssh pi#RasPiIP -pw raspberry -m C:\\path_to_test.txt\\test.txt";
$result = shell_exec($cmd);
echo $result;
?>
For sending commands to my RasPi the code works. I have tested multiple times by as example changing test.txt to sudo reboot and it worked as intended.
I'm using PuTTY to send my command (test.txt is currently nfc-list which returns connected Scanners etc not important right here) to the RasPi.
What I want to achieve is that $result contains the returned data when my command is executed.
Is it even possible to do that? If yes how (any help appreciated). If no, are they maybe other ways to approach this?
Addressing the possible duplicate: I am using a Windows Machine and also I'm trying to get the result (of the one command) to reuse in my PHP script. In the other question, user is trying to save the full console log and save it to another file.
First, do not use PuTTY. PuTTY is a GUI application intended for an interactive use. Use Plink, which is command-line/console equivalent of PuTTY intended for command automation. Being a console application, it has a standard output, which can be read in PHP (PuTTY as a GUI application does not have standard output).
With Plink, you can also specify the command on Plink command line, so you do not need to create the test.txt command file.
In any case, there's no way to make PuTTY or Plink separate an output of command only (at least not from a command-line).
But what you can do, is to print some header/trailer to distinguish the start and end of the command output, like:
plink.exe -ssh pi#RasPiIP -pw raspberry "echo start-of-command && command && echo end-of-command"
And then in PHP, you can look for the start-of-command and end-of-command to identify what part of Plink output is really the command output.
In any case, you better use a PHP SSH library to achieve what you want, rather then driving an external application. For example phpseclib. But that's a completely different question.
I am unable to execute a source command in linux using php.All other commands are working except this one. I need to execute the following command.
source /root/Envs/ate/bin/activate
This activates the ate-Automatic Test Equipment.Once I activate it then I need to run a python script as the script accesses the remote server.
I am able to manually run it but I am creating a tool which will automatically do it.
<?php
exec("source /root/Envs/ate/bin/activate", $output, $return);
echo "Command returned $return, and output:\n";
echo exec("python box_upgrade-pradeepa.py");
?>
The above commands returns 1 which means there is an error.But I am not sure how to run the 'source command'. The python script will run only if the source command is successful.(the python command is correct as I replaced hello.py and it ran fine.)
Could you pls help me as I am really stuck for a week?
Thanks a lot..
I found out the error. Since I am doing it using php (for a web tool) the user is Apache. 'Apache' user is unable to access the script in root folder. Moving it to another directory, I am able to run the script fine.
Thanks all..
I'm new to Perl and i'm using Windows Machine
Well i'm using PHP as well as Perl.. My complete web application was written in PHP. I have written a Perl Script for DB interaction for every 20 minutes. My Perl script goes like this.
use strict;
use warnings;
sub Process1()
{
use DBI;
use Date::Format;
my $DBH=DBI->connect("DBI:mysql:dbname:localhost","root","");
my $sth=$DBH->prepare("UPDATE tablename SET column1='blah'"); #update query
$sth->execute();
while (my #row=$sth->fetchrow_array)
{
print $row[0] . "\n";
}
}
while(1) {
&Process1();
sleep 900;
}
If I have to run my perl script, I would just open Perl Commad line and execute the command
perl C:xampp\htdocs\samplescript.pl
My questions are -
How do I run the same Perl script in my Linux server.
How to execute the command?
If I close the perl command line in windows, the script will be stopped. How to stop the same script in linux server?
IMHO, the best way to do this is to write your perl code as a simple "one-shot" treatment, and rely on the features provided by the system to run it repetitively:
Scheduled Tasks on Windows
Crontab on Linux
How do I run the same Perl script in my Linux server?
There are several ways to execute an perl script on linux.
You need to execute your Script with an interpreter. In the most cases you use:
/usr/bin/env perl /Path/To/Your/script.pl
Or
/usr/bin/perl /Path/To/Your/script.pl
Atleast there is a better way:
You define the Interpreter Path into the first row in your script.
#!/usr/bin/env perl
use strict;
use warnings;
sub Process1()
{
use DBI;
use Date::Format;
my $DBH=DBI->connect("DBI:mysql:dbname:localhost","root","");
my $sth=$DBH->prepare("UPDATE tablename SET column1='blah'"); #update query
$sth->execute();
while (my #row=$sth->fetchrow_array)
{
print $row[0] . "\n";
}
}
while(1) {
&Process1();
sleep 900;
}
Then you are able to execute your script with the following command:
/path/to/your/script.pl
How to execute the command?
You can execute the same command at the Linux Terminal?
If I close the perl command line in windows, the script will be stopped.
How to stop the same script in linux server?
You can use the linux "kill" command. If you run it in Background. The script will also stop if you close the Terminal Session or pressing strg+c.
You can also look at this module and build your own Perl Command to stop the script.
Getopt::Long
My Tip
Daemonize your Script and insert some commands to handle the script. So it can run in Background.
Explanation: Daemon(computing)
This may help if you didn't find them online.
To run the script in Linux perl "path"/script.pl. To run the script continuously in linux either use crontab or run the script via shell script and run it background.
And to stop the script in Linux find and kill the process using ps -ef|grep script.pl command and kill -9 "process_id" respectively.
i have 2 shell commands and i want to exec in php the first command that act like an application and stay into it and exec another command there (in this application).
i write in my shell: wpa_cli
and get the result:
Interactive mode
>
now i can write commands in that software, like:
Interactive mode
> status
and get the result:
wpa_state=DISCONNECTED
p2p_device_address=34:b1:f7:1f:2d:bb
address=34:b1:f7:1f:2d:bb
>
my problem is that i want to do that outside the shell by php.
how can i do that?
Your requirement is similar to this interactive shell using PHP, but it requires execution of PHP script in terminal, which you don't want.
So, in this case, you can use any of these php functions: shell_exec, exec, system or passthru and run both wpa_cli status commands together, you will get the required result.
e.g. run the following script in your php file and see the output,
echo '<pre>'. shell_exec('wpa_cli status') .'</pre>';
You can also use PHP Ajax Shell or similar scripts to execute your commands and get response on the same page without reloading it.
I am willing to execute a shill command through PHP but i faced that the command is not executing , here is the command:
exec('/cutycapt/CutyCapt --url="' . $source . '" --out="/home/user/NetBeansProjects/PhpProject1/htmlImage/example.png"');
i tried as testing to execute the following :
echo exec(' ls /cutycapt/');//print_r is the same
only one file returned while this command returned them all
echo system(' ls /cutycapt/');
i tried to use the "system" method instead of exec in the first command and the result was the same
what could affect the command so it wan't execute ?
update
the case i'm talking bout the the first command work whether i run it in the terminal or i run the PHP script in terminal too but when i run it from the browser (the php script )it doesn't work !!
Look into manual - http://de3.php.net/manual/en/function.exec.php
exec and system returns "The last line from the result of the command"
In case of system and exec the last line from the result of the command gets returned.
If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
I had the same problem working with external commands in php. The problem was related to file permissions. I was using "vchiq" library and the error was "* failed to open vchiq instance". This page might work for you.