Want to open a vm through php code using virsh command - php

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.

Related

Sending data from a client to a server by means of a http post

I'm trying to send data from the client to the server. The client runs a simple python script that uses the 'request' library. The server side consists of another simple php script using the $_POST.
I need the webpage to update depending on the data that is given through the client program.
Here is the python script:
import requests
url = "http://xxxxxxx.com/php_files/text_data.php"
d = {'test': 'It works!'}
r = requests.post(url, data = d)
print r.status_code, r.reason
print r.text
And here is the php code:
<!DOCTYPE = html>
<html>
<head>
<h1>
<?php
$txt = $_POST['test'];
echo $txt;
?>
</h1>
</head>
</html>
I need the php page to display 'It works!' on h1 as this is the value that is being passed.
But for some reason, it does not display anything
r.text prints the required format with 'It works!' in the < h1 > tags, but the same does not get displayed in the actual website.
I've also tried var_dump($txt). It gives me a NULL value.
Any help would be gladly appreciated.
It seems to me that you are asking a separate instance to update your current instance. The PHP that you are accessing in your browser knows nothing about the python script. It doesn't call the python script at all. In the second session the python script calls the PHP and receives the correct response.
These are two different sessions, the browser window will see nothing from the python script unless it calls it.
Here is what is happening:
Session 1
Run Python script on local machine
Python calls PHP on server
PHP returns output to local machine
Python prints result
Session 2
Open web browser on local machine
Web browser calls PHP on server
PHP returns results to web browser
Web browser displays results
There is no persistence in the first session to save the information for the second session. They are two completely separate actions. A more typical way would be to set up a database (or just quick and dirty a text file) on the server to save the information. You need to create a second PHP file to save the information to a database or text file on the server. You then need to modify your previous PHP file to read the information from the database or the text file. The sessions would then be set up the following way.
Session 1
Run Python script on local machine
Python calls PHP (new file) on server
PHP writes information from python script to database (or text file)
PHP returns status message to local machine
Python prints status
Session 2
Open web browser on local machine
Web browser calls PHP (original file) on server
PHP reads desired information from database (or text file)
PHP displays information read from server on web browser
Web browser displays results
If you really want to use the results from the python script in the PHP without a database or text file, you will need to upload the python script to your server, and use one of the methods suggested in Calling Python in PHP

How to run a .jar inside browser?

I tried to execute a .jar using HTML or PHP.
In the first case I wrote the code below:
<applet code=Diagnostica.class
archive="Diagnostica/Diagnostica.jar"
width="120" height="120">
</applet>
This way doesn't work cause the file Diagnostica.jar need to contain an applet I suppose.
So I tried the second option in PHP:
exec("java -jar Diagnostica/Diagnostica.jar");
This way it works. Diagnostica.jar starts outside the browser, but I want to start it inside the browser.
How can I do?
If that file does not contain an Applet, I do not think you will be able to start it in the browser (have a look here).
Executing it via exec should work like executing it via the command line - but as you already noticed, it will be run on the server - not the client.
If it should run on the client, you'll either have to use a Java Applet or let the user download the jar-executable and execute it via command line etc.

Can Applescript send get or post requests?

I use Hazel to organize downloaded files on my Mac. I see i can make it run applescripts every time it triggers a rule.
I was wondering if it is possible to make it send something like:
http://my.server.com?type=Show&name=Big+Bang+Theory
Then i could create a small page that logs all downloads.
Sure, the command line utility curl is one way. Applescripts can use command line utilities with the "do shell script" command. So try this...
set theURL to "http://my.server.com?type=Show&name=Big+Bang+Theory"
do shell script "curl " & quoted form of theURL
The above will return the page results to applescript. Maybe you'd rather just open the link in your default browser...
set theURL to "http://my.server.com?type=Show&name=Big+Bang+Theory"
open location theURL
So take your pick!

How to execute an interactive shell program from a PHP webpage

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

How to capture x screen using PHP, shell_exe and scrot

I'm building a web page screen capture application for an internal R&D project.
Environment: Ubuntu 9.04 (default desktop install), Apache, PHP.
So far I've got a bash script that takes one parameter (URL), fires up firefox, grabs the screen and saves it as a PNG. I've tried running this from terminal and it works fine.
Here's the Bash script:
#!/bin/bash
firefox $1 # Start firefox and go to the passed in URL
scrot -d 5 test.png # Take screen grab with 5 second delay
Next I created a simple PHP page that uses shell_exec to run the script:
<?
// Sample URL
$url = 'http://www.google.com';
// Run the script
shell_exec('sh script.sh ' . $url);
// Out put HTML to display image
echo '<img src="test.png" />';
?>
However, when the PHP page is called the screen is not captured.
A quick look in the apache error logs show the following message:
Error: no display specified
giblib error: Can't open X display. It *is* running, yeah
I'm guessing this is because apache is running as a different user and hasn't got access to my X display.
So, can anyone shed any light on what I'm doing wrong or how I can capture the current user display.
Thanks.
Launching firefox from PHP running under Apache seems to me like a bad idea (it definitly feels wrong).
The way I would do that :
a PHP webpage (which runs under Apache) that receives the URL ; something like a form, for instance
that page inserts the URL in a database-like system, that will be used as a queue
this URL is marked as "to process"
a PHP (or another language) script, totally independant from Apache ; for instance, launched by the crontab
this scripts selects an URL from the queue in the database (least recent one, for instance), and marks it as "processing"
it then lauches your shell-script, which launches firefox and does the screenshot
one the screenshot is done, the URL in the queue is marked as "done", and the screenshot's path is associated to the URL
this will work, as it is independant from Apache
another web page displays the queue, and the status of each URL ("to process", "processing", "done + path to the screenshot"
you can even imagine having an association betwen a user and an URL, to not display every URL+screenshot to everyone.
With this system, several advantages :
php+apache for the webpages
php outside of apache for the "system" parts
you can have the webpages on one server
and you can have several machines (linux, windows, mac -- maybe using virtual machines) to make the screenshots
allow you to get screenshots from different OSes
scales way better ^^
It's not really an answer to the question, but I think it's a better way... Hope this helps !
Here is a guide to do screen-capturing using firefox and xvfb. The advantage with this approach is that there will be no firefox windows opening and closing on your main X server. It will also solve your problem with permissions.
Can't you run your bash and firefox as the same user as apache?

Categories