Php exec on linux machine is not working - javascript

So I am writing a simple little panel so that my friends can restart their server without having to go through terminal to do so. I am currently running into this problem though. When I click on one of the buttons it does not execute the linux command on the local machine. If anyone could help me that would be great, also sorry if the code is all over the place.
<!DOCTYPE html>
<?php
if (isset($_POST['button']))
{
$command = '/etc/init.d/darkrp start'
$executecommand = shell_exec($command);
}
if (isset($_POST['button1']))
{
$command = '/etc/init.d/darkrp stop'
$executecommand = shell_exec($command);
}
if (isset($_POST['button2']))
{
$command = '/etc/init.d/darkrp restart'
$executecommand = shell_exec($command);
}
?>
<html>
<body>
<button name="button" onclick="myFunction()">Start</button>
<button name="button1" onclick="myFunction1()">Stop</button>
<button name="button2" onclick="myFunction1()">Restart</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Server started!";
}
function myFunction1() {
document.getElementById("demo").innerHTML = "Server stopped!";
}
function myFunction2() {
document.getElementById("demo").innerHTML = "Server restarted!";
}
</script>
</body>
</html>

Related

Chrome extension - send message from content script to the pop-up

I just wanted to ask you if it's possible to send a message from content script to the popup script. It works the other way around - from popup to content:
popup.js
let counter = 0;
let userInput = document.getElementById("userInput").addEventListener("click", () => {
counter++
let params = {
active: true,
currentWindow: true
}
chrome.tabs.query(params, gotTabs);
function gotTabs(tabs) {
chrome.tabs.sendMessage(tabs[0].id, counter)
}
});
content.js
chrome.runtime.onMessage.addListener(gotMessage);
function gotMessage(message) {
console.log(message)
}
index.html
<!DOCTYPE html>
<html>
<body>
<h2>Hello there!</h2>
<button id="userInput">Click me!</button>
</body>
<script src="popup.js"></script>
</html>
My question is, how can I send a message from content script to the popup script, if its possible, because when I switch those files, i got errors like:
content.js:14 Uncaught TypeError: Cannot read property 'query' of undefined
at HTMLDocument.myClickHandler
Thanks for your help.

My code not working on server but working on localhost

Code not working on server want to find geo-location and store into database
code not working on server want to find geo-location and store into a database it working properly on localhost its w3school code.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
window.location = "position/?latitude="+position.coords.latitude+"&longitude="+position.coords.longitude;
}
</script>
</body>
</html>
There is no geolocation library on the server. navigator.geolocation only exists in the browwer.

Update page content from live PHP and Python output using Ajax

Long-time user, first-time asker. I've learned so much from the community and I love this site.
So here is what I'm shooting for. I want to have a web interface that runs ping commands on the backend. I ideally want a website that has a text input that allows you to enter an IP address or domain, a button that runs the command and a python script that runs from PHP to actually run the ping command. The tricky part for was to get the output to print to the website live as it is outputted on the command line. I want to do it this way as a way to future-proof the concept and eventually use different iperf parameters.
I built a little PHP page that "technically" gets the job done but I can't figure out how to only call the PHP script when the button is clicked. Since it's a PHP page, it runs whenever the page is loaded. So after some research, I figure ajax jquery is what I'm looking for. I've spent about 2 days trying different things that get me really close but it seems that I'm dancing around my solution.
From what I've learned about ajax, I essentially need a button that runs an ajax function that is linked to my working php script. I can get it to run the script but I can't get it to update the page content in a live/continuous manner. Only when the command is finished running.
Here is my php page that does what it needs to do but does it everytime the page is loaded/reloaded. Not ideal. I want the script to only run when the button is pressed.
liveping.php:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="liveping.php" id="ping" method="post" name="ping">
Domain/IP Address: <input name="domain" type="text"> <input name="ping" type="submit" value="Ping">
</form><?php
if (isset($_POST['ping'])) {
function liveExecuteCommand($cmd)
{
while (# ob_end_flush()); // end all output buffers if any
$proc = popen("$cmd 2>&1", 'r');
$live_output = "";
$complete_output = "";
while (!feof($proc))
{
$live_output = fread($proc, 4096);
$complete_output = $complete_output . $live_output;
echo "<pre>$live_output</pre>";
# flush();
}
pclose($proc);
}
}
$domain = $_POST['domain'];
$pingCmd = "python /var/www/html/ping.py ".$domain;
if (isset($_POST['ping'])) {
liveExecuteCommand($pingCmd);
}
?>
</body>
</html>
ping.py:
#!/usr/bin/python
import cgi
import os
import sys
ping = "ping -c 5 -W 2 "+sys.argv[1]
os.system(ping)
Some things I've tried:
<html>
<head>
<script>
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = setInterval(function() {
if (ajax.readyState == 4) {
document.getElementById('content').innerHTML = ajax.responseText;
}
},100);
function updateText() {
ajax.open('GET', 'ajax.php');
ajax.send();
}
</script>
</head>
<body>
<button onclick="updateText()">Click Me</button>
<div id="content">Nothing here yet.</div>
</body>
</html>
OR
<!DOCTYPE html>
<html>
<body>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_tweets').load('ajax.php').fadeIn("slow");
}, 1000); // refresh every 10000 milliseconds
</script>
</head>
<div id="load_tweets"> </div>
</body>
</html>
WITH ajax.php
<?php
while (# ob_end_flush()); // end all output buffers if any
$proc = popen("ping -c 5 -W 2 google.com", 'r');
$live_output = "";
$complete_output = "";
while (!feof($proc))
{
$live_output = fread($proc, 4096);
$complete_output = $complete_output . $live_output;
echo "<pre>$live_output</pre>";
# flush();
}
pclose($proc);
?>
Thanks for any help!
You do not need python for showing ping results. Just two PHP files will be enough.
index.php will have the AJAX functionalities along with the form.
ajax.php will have the code to ping specified domain address.
I afraid that using jQuery you might not able to catch the live feed. Because it doesn't have any onreadystatechange. So, you might need to use vanilla JavaScript in this case. Here is a working demonstration:
index.php:
<!DOCTYPE html>
<html>
<head>
<title>Ping AJAX</title>
</head>
<body>
<div>
Domain/IP Address: <input id="domain" type="text">
<input id="ping" type="button" value="Ping">
</div>
<div id="result"></div>
<script>
function updateText(domain) {
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (this.readyState == 3) {
var old_value = document.getElementById("result").innerHTML;
document.getElementById("result").innerHTML = this.responseText;
}
};
var url = 'ajax.php?domain='+domain;
ajax.open('GET', url,true);
ajax.send();
}
document.getElementById("ping").onclick = function(){
domain = document.getElementById("domain").value;
updateText(domain);
}
</script>
</body>
</html>
ajax.php:
<?php
if (isset($_GET['domain'])) {
function liveExecuteCommand($cmd)
{
while (# ob_end_flush()); // end all output buffers if any
$proc = popen($cmd, 'r');
$live_output = "";
$complete_output = "";
while (!feof($proc))
{
$live_output = fread($proc, 4096);
$complete_output = $complete_output . $live_output;
echo "<pre>$live_output</pre>";
# flush();
}
pclose($proc);
}
$domain = $_GET['domain'];
$pingCmd = "ping ".$domain;
liveExecuteCommand($pingCmd);
}
else{
echo "No post request";
}
?>
Output:
Declaimer:
The ping command is changed as I am currently using Windows operating system. Update it according to your operating system.
As a first time questioner, you have described the problem neatly and also showed your efforts to solve the problem. I really appreciate it.
ajax.readyState == 4
essentially means, script on the other side has finished ... 3 is partial.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState
You just have to take all ajax script into the function
example:
function updateText() {
$.ajax({
type: 'GET', // can be POST, too
url: "ajax.php",
crossDomain: true,
data: {
firstvar: firstvar,
secondvar: secondvar
},
cache: false,
success: function(data) {
if($.trim(data) == "false") {
alert("Fail to recived data");
}
else {
// Success getting data
// Do some jobs
}
}
});
}
If you want to cancel submit to not refesh, U can use
return false; // At the end of the function above
Hope it helps.

JavaScript to Html

Given a web page with JavaScript code, I would like to generate a resulting html automatically (either via CLI tool OR using some library in some language)
For example, given test.html
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
</body>
</html>
I would like to get as a result
<html>
<body>
<p id="demo">Hello JavaScript!</p>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
</body>
</html>
After a quick search, it looks like watin will do what you want.
It's aimed at automated testing, but when it hits a page it will execute all js as well as ajax calls etc. Looks like you can grab the resulting html from it too.
The answer is based on the comment of #torazaburo
In fact, the phantomjs is capable of evaluating javascript and producing html.
Here is how it could look like, executing phantomjs load_page.js path_to/test.html
var page = require('webpage').create(),
system = require('system'),
page_address;
var fs = require('fs');
if (system.args.length === 1){
console.log('Usage: phantomjs ' + system.args[0] + ' <page_to_load:http://www.google.com>');
phantom.exit();
}
page_address = system.args[1]
page.open(page_address, function(status){
console.log('Status:' + status);
if (status === 'success' ){
fs.write('phantom_result.html', page.content, 'w')
}
phantom.exit();
});

Uncaught SyntaxError: Unexpected end of input on popup.html file in Chrome Extension

I'm making a Chrome Extension for the first time. When I right click the extension icon and inspect the popup this is the error I'm getting.
Uncaught SyntaxError: Unexpected end of input myextension/popup.html
And the fact that it's failing on an HTML file is making me confused. I looked through about 10 other similar questions and most of the time it's apparently a syntax error in the javascript file. I've linted mine with a couple different linters and still nothing is showing up. Could somebody help me spot a mistake here? It's 62 lines:
function updatePopup(){
listofbad = JSON.parse(localStorage["badguys"]);
$('#dumpster').empty();
var i = 0;
for(i = 0; i < listofbad.length; i = i+1){
$('#dumpster').append( listofbad[i] + "<br>");
}
}
$(document).ready(function(){
if(localStorage["badguys"] != undefined){
var namesthe = JSON.parse(localStorage["badguys"]);
updatePopup();
chrome.tabs.query({
active: true,
currentWindow: true
},
function(tabs) {
/* ...and send a request for the DOM info... */
console.log("Sending message from popup to content");
chrome.tabs.sendMessage(
tabs[0].id,
{from: 'popup', subject: 'DOMInfo', newnames: namesthe}
);
}
);
}
});
function save_options() {
var thenames = [];
if( localStorage["badguys"] != undefined ){
thenames = JSON.parse(localStorage["badguys"]);
}
console.log("JSONParse: " + thenames.toString());
//thenames = ["Aidenator", "Moobot", "Nightbot", "Teamevilmaster"];
var elname = document.getElementById("namebox").value;
if( $.inArray(elname, thenames) > -1 ){
return false;
}
thenames.push(elname);
localStorage["badguys"] = JSON.stringify(thenames);
updatePopup();
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
/* ...and send a request for the DOM info... */
console.log("Sending message from popup to content");
chrome.tabs.sendMessage(
tabs[0].id,
{from: 'popup', subject: 'DOMInfo', newnames: thenames}
);
});
}
document.getElementById('namebutton').addEventListener('click', save_options);
function clearlist() {
localStorage["badguys"] = [];
updatePopup();
}
document.getElementById('clearbutton').addEventListener('click', clearlist );
Here's the small HTML that goes along with it too:
<!doctype html>
<head>
<title>Extension Options</title>
<script src="jquery.js" type="text/javascript"></script>
</head>
<body>
<div id='status'></div>
Annoying person's name:
<input type='text' id="namebox"></input>
<button id="namebutton">Add</button>
<button id="clearbutton">Clear List</button>
<hr>
<center>
<i>List of Removed People:</i>
</center>
<div id="dumpster"></div>
<hr>
<center>
<img src="carlton.jpg" width="50px">
</center>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>
Maybe I've been staring at a screen too long to spot any typos, but if somebody could help me out here, that'd be great. This Chrome Extension business has been quite frustrating.

Categories

Resources