Let's say I have a local webpage, that when a button is pressed, executes a program in C. I already can do that. The problem is that this program outputs logs and needs input from the user. My question is: how can I display bash-like window and run this program through it, so that output and input is seen through the screen?
Perhaps you could use ajaxterm, and specify the program that you want to run as the shell:
ajaxterm.py --command=/your/command --port=yourfavoriteport
You could redirect the user to the specified port, or display the terminal via an iframe.
I develop plugin for jquery "Terminal Emulator" you can check this out. If you write your C program as JSON-RPC you can create your terminal with one line of javascript.
The other solution is to not use PHP at all, and creating your code as CGI written in C or even in Bash.
Update: You can modify your program to be CGI script
int main() {
printf("Content-Type: text/plain\n\n");
// code of your program here
}
and put it in cgi-bin directory and run it through browser http://yourserver.com/cgi-bin/program it display the output in browser
Related
I have a problem displaying the results of a Perl script that I am calling from my PHP webpage. The Perl script constantly monitors a socket and will display the output of this when run from the command line and also saves the output to a file. I know the Perl script is being called and running successfully as the text file is being updated but I do not get the output on the webpage as I was hoping for.
I have tried using the system(), exec(), passthru() and they all allow the Perl script to run but still with no output on the webpage so I am obviously missing something. Am I using the correct functions? Is there a parameter that I need to add to one of the above to push the output back to the webpage that calls the Perl script?
One example of what I have tried from the PHP manual pages:
<?php
exec('perl sql.pl', $output, $retval);
echo "Returned with status $retval and output:\n";
print_r($output);
?>
Edited to include output example as text instead of image as requested.
# perl sql.pl
Connecting to the PBX 192.168.99.200 on port 1752
04/07 10:04:50 4788 4788 3256739 T912 200 2004788 A2003827 A
I'm no PHP expert, but I guess that exec waits for the external program to finish executing before populating the $output and $return variables and returning.
You say that your sql.pl program "constantly monitors a socket". That sounds like it doesn't actually exit until the user closes it (perhaps with a Ctrl-C or a Ctrl-Z). So, presumably, your PHP code sits there waiting for your Perl program to exit - but it never does.
So I think there are a few approaches I'd investigate.
Does sql.pl have a command-line option that tells it to run once and then quit?
Does PHP have a way to send a Ctrl-C or Ctrl-Z to sql.pl a second or so after you've started it?
Does PHP have a way to deal with external programs that never end? Can you open a pipe to the external process and read output from it a line at a time?
I want to open a vm through following php code:
<?php
$output = shell_exec("virt-viewer --connect qemu:///system 1 2>&1");
echo "<pre>$output</pre>";
?>
I already set the permission of a file with read,write and execute.
I am getting the following error:
(virt-viewer:15162): Gtk-WARNING **: 22:45:33.686: cannot open display:
please help if i am missing something or doing something wrong.
Thanks
How web page works:
Browser makes request to web server, web server delivers html and optionally other asset files (css, js..), but generally, html is loaded first and it stays as it is - content is not changed (except if it's changed by JS, which is not the case here).
Your call will execute some shell command it will return some response (some text) and you'll get it inside some var. If you display that static text as part of your page how do you expect that it will act as GUI?!?
That's just not possible. Imaging that you are calling from shell_exec() exe file of some video game? Will the game then run in the browser?!?
You can call shell commands in order to trigger some action on server (i.e. rescale some image, process some video) or to collect some response (i.e. free disk space or something). But expecting that if you call something form shell will appear in browser is just not realistic.
I search about this and didn't fine an answer for it.
So my question is not about simple output redirection in Linux shell. I want it to happen inside a bash script I'm writing and I want output and error of anyscripts or commands I run in that script be redirected into separate files.
This is what im currently using:
STATUSFILE=x.out
LOGFILE=x.log
exec > ${STATUSFILE} 2>${LOGFILE}
echo "bla bla"
#some other commands
which works fine for normal outputs. But for example in part of my script I put to my backup server using ftp put. And when I run in manually I see kind of a progress bar saying how much time is passed and how much time its gonna take.
What im trying to do is to capture that kind of output which seems to be updating on the fly and kinda dynamic and currently I get other outputs stored in files but not that kinda dynamic ones.
And another question is if im following a good approach doing this. As I said I want the output and Im gonna use that by another process to see the progress and then use it to inform user trying to run a task with browser.
So for example in my case its uploading a big file into ftp storage which user will command it from browser and I run those scripts and I want to have kind of a progress bar showing up the status to the user. (I know how to handle it on client side - probably a socket connection or interval ajax request checking on a file or an event being triggered by server)
My second question is how should I get the progress of commands that have dynamic output.
If anything is unclear please let me know Ill try to fix it.
Thanks for your help guys.
Good Morning,
So my question is not about simple output redirection in Linux shell. I want it to happen inside a bash script I'm writing
That is quite simple. You just need to enclose your commands in {}.
#!/bin/bash
{
echo "hello world!"
ls -la
} >> script.log
I want output and error of anyscripts or commands I run in that script be redirected into separate files.
You could use tee if the same output should go in seperate files.
#!/bin/bash
{
echo "hello world!"
ls -la
} | tee foo bar baz
If the output of different commands should go in different files, you would just use different {} blocks.
My second question is how should I get the progress of commands that have dynamic output.
Some tools tend to put the progress bar to STDERR. You can redirect STDOUT and STDERR to same file using 2>&1. 2 is the descriptor for STDERR, 1 for STDOUT and you are basically appending 2 on 1.
Thus you would rewrite the first script to:
#!/bin/bash
{
echo "hello world!"
ls -la
} >> script.log 2>&1
Be carefull with that, though. Some tools use colors and carriage return to display such nice formatted progress bars. This will look ugly in the log file.
I was wondering how I could execute a command through PHP into a screen running on my virtual private server.
i.e.
if(submit){
do "say This is a minecraft server." in screen.
}
Thanks in advance,
If you are running the script under screen, then just echo or print the data you want to display.
Otherwise you can:
make a system call to write or wall
write to a file that you are tail -fing in the screen (or some other system by which the screen polls something for new messages)
run a service in the screen that you can connect to over the network
Still unclear on exactly what you mean, but if this is what you're looking for, you can execute linux commands by using the backtick operator, such as
echo `ls`;
to print the contents of the directory where the script is located. So if you wanted to print commands to your server, you could do it that way.
i have been making a text writer where u can create a file and then u can also store it into a folder of d web server, like wase a cloud computing system.
but i have a prob that how to save the file when the menu is clicked. i am trying it with exec() function of php, to run a scritp but not getting how to invoke the script on click event. plz hepl out, ur input will be highly appriciated.
and also is there option to open a saved file on click into the text writer dynamically.
thanks!!
Why can't you just save it with fwrite() function?
I mean, it's a usual method of saving files – fopen() it for writing, fwrite() data and finally fclose() the file. You either use general approach or tell us more about your specific needs.
If you still need to run unix/linux commands from PHP, the option to debug what's happening is to log in to command line as a user PHP is running under – e.g. www-data or nobody. You can find it out by running this in your script (see the backtick execution operator):
print `whoami`;