Accessing shell script on remote server - php

So first of all I have 2 Servers:
Raspberry pi
Linux Debian server
The pi has a script that displays the current Temperature in a room.
This is the script :
#!/bin/bash
i2cset -y 1 0x48 0xEE
dte=$(date +%Y-%m-%d)
tme=$(date +%H:%M:%S)
hexraw=$(i2cget -y 1 0x48 0xAA w)
msb=$(echo ${hexraw:4:2})
lsb=$(echo ${hexraw:2:1})
dec=$(printf "%d\n" "0x$msb$lsb")
temp=$(echo "scale=1; $dec/16" | bc)
echo $temp
exit
Displaying the temperature on the pi's website works fine.
But then I have another script on the linux server that takes the value of the pi's script:
#!/bin/bash
x=$(ssh pi#172.16.248.210 sudo /home/pi/CurrTemp;)
echo $x
This works too. But when I want to display the Temp on my Linux server website(with shell_exec()) it doesn't work. The var_dump is empty.
PHP:
I've tried to write it with a "sudo".
I've changed the script owner to root.

ssh authentication does not work since the web server can't (and should) access your ssh keys.
You have to options (where the latter is definitely preferred!):
Create another ssh key for the web server on the Debian server and add to allowed_keys on the PI. You may restrict the key to allow only that command.
Create a little web service on the PI and output the temperature in plain text, xml, json or whatever. The use file_get_contents() to obtain the file over HTTP, like this using a plain text file:
temperature.php on raspberry pi:
<?php
echo shell_exec('/home/pi/CurrTemp');
temperature.php on Debian:
<?php
$temperature = file_get_contents('http://172.16.248.210/temperature.php');
echo $temperature;

Related

Accessing X Display through Apache

I am trying to send keypresses to a Raspberry Pi running Raspdbian from from a web page hosted on an Apache server at /var/www.
I'm using xdotool, which can send keypresses using commands like this:
xdotool key C
I made a python script xdotool.py that sends a few keypresses like that:
#!/usr/bin/env python
import os
print 'sending HI'
os.system("export DISPLAY=:0")
os.system("xdotool key H")
os.system("xdotool key I")
I am SSH'd into the Raspberry as user Pi, and running this script from terminal works fine and sends the keystrokes "HI" to the Raspberry.
However, I want to be able to do this through a web page. I've made a php-script that runs python scripts and displays the terminal output in the browser, but it does not work with the xdotool.py script. I have given the script permission to be viewed/changed/executed by all users.
I've also changed the Apache settings in /etc/apache2/envvars and changed the user from www-data to pi. The user pi should have all necessary permissions.
The script does run, and "sending HI" is displayed in the browser. But it doesn't send the keystrokes. The error I get from error.log is this:
Error: Can't open display: (null)
Failed creating new xdo instance
Any ideas how I can accomplish this? If there's a way to give everyone permission to do everything, that wouldn't be a problem seeing that this server will only run on a local network with no internet access.

Run the remote script in linux without login

I am having one php script in remote linux server. I need to run that php from another linux machine without any kind of login.
In Linux Machine 1:
$] /home/user/myScript.php
In Linux Machine 2:
$] runRemoteCmd linuxMachine1:/home/regress/myScript.php
...
myScript.php outputs
...
I need something like this.
You need to have the script on the remote server, and if you have ssh key authentication you can do the following
ssh user#thatServer.toruncommand.com 'command to run'

Running imagesnap in PHP with Mac Server

I am trying to develop a small site to take snapshots of my house using a webcam connected to to my mac mini at home. The mac mini is running OS X Server. The Webcam is a Logitech HD Pro Webcam C920 (USB connected). It is based on PHP and HTML5 (no other code besides CSS).
My development machine is a MacBook Pro using the internal iSight camera. It is not running OS X server; I manually enabled apache and PHP. While developing the site on my laptop, everything worked flawlessly. When I installed it on my mac mini the imagesnap program turns on the camera but never finishes; I have to manually kill the process, and the snapshot is never created.
The following error is logged:
imagesnap[2363]: *** QTCaptureSession warning: Session received the following error while decompressing video: Error Domain=NSOSStatusErrorDomain Code=-12903 "The operation couldn’t be completed. (OSStatus error -12903.)". Make sure that the formats of all video outputs are properly configured.
This is only when running it from the site directly. If I run the same program from the command-line, it works just fine... even running it as the _www user!
sudo -u _www bin/imagesnap images/snapshot_$DATE.jpg
This is the code that does the call in PHP:
<?php
if(isset($_POST['submit'])) {
system('bin/run_imagesnap.sh', $return_value);
if ($return_value) {
echo "The command failed.";
}
}
?>
I was unable to figure out how to write the date to the output filename (I'm a noob with PHP), so I used a bash script to do it for me (I'm better with BASH):
#!/bin/bash
DATE=`date +"%m-%d-%y_%T"`
bin/imagesnap -q -w 3 images/snapshot_$DATE.jpg
I've also tried exec(), passthru(), and shell_exec() all with the same results.
Finally, I've also tried the following: Create test.php, put code:
<?php
system('bin/run_imagesnap.sh', $return_value);
if ($return_value) {
echo "The command failed.";
}
?>
and ran it on the command-line as the _www user using the php-cli:
sudo -u _www php test.php
and it also worked just fine (created the snapshot file and did not give error).
I'm thinking maybe it has something to do with the OS X server app, but google searches return nothing.
I'm stumped. Why does it break when I run the same exact code on the browser but works fine on the command-line using the same exact tools and user?
Unknown if this is the correct way, but I ended up using sudo to run the script.
visudo
and then give the _www user passwordless root access to the script:
_www ALL=NOPASSWD: /path/to/script
and now it works. There's always more than one way to skin a cat..

PHP run BASH script to add printers locally (OS X)

Thanks in advance for helping. I am trying to set up a public website for my organization which consist of all the organization's printers and the accompanying configurations (printer name, location details, SMB path and driver to use which is already provided by the OS X). From this web site a user will search for a printer and add/install the printer to their OS X workstation by clicking on a link.
I am looking at setting up a PHP site and the printer installation action facilitated by BASH script. Below are the codes:
This is the webSite.php
<?PHP
exec("bash ./InstallPrinter.sh");
?>
and this is the InstallPrinter.sh BASH script
#!/bin/sh
lpadmin -p ToshibaPrinterName -L Location/Building/Level 1 -v smb://prin-server.AD.ORG/ToshibaEStudio4520C-A -P"/Library/Printers/PPDs/Contents/Resources/TOSHIBA_eS4520CSeries.gz"
cupsenable ToshibaPrinterName
/usr/sbin/accept ToshibaPrinterName
The printer will install but not locally on the workstation but on the server hosting the PHP and BASH script. I see the printer turn up in the OS X System Preferences, which is not the expected result. Is there a way to make the BASH script run it's command on the local workstation?
Thanks
No. You will have to provide the shell script as a download and instruct the user on how to run it.
You cant execute bash script on a local machine (for obvious reasons) and you also cant offer a bash script to download without having to change the CHMOD permissions to executable. I would suggest making applescript to add the printer and offering that to download.

Php : running ssh from Windows to login to a Linux and run a script

Here's my goal :
I have a Windows XP PC with all the source code in it and a development database.
Let's call it "pc.dev.XP".
I have a destination computer that runs Linux.
Let's call it "pc.demo.Linux".
Here's what I've done on "pc.dev.XP" (just so you get the context) :
installed all cygwin stuff
created a valid rsa key and put it on the dest
backup computer so that ssh doesn't
ask for a password
rsync works pretty well this way
If i try to do this on "pc.dev.XP" via a command line :
cd \cygwin\bin
ssh Fred#pc.demo.Linux "cd /var/www && ls -al"
this works perfectly without asking a password
Now here's what I want to do on the "pc.dev.XP":
launch a php script that extract the dev. database into a sql file
zip this file
transfer it via ftp to the "pc.demo.Linux"
log to the "pc.demo.Linux" and execute "unzip then mysql -e "source unzipped file"
if I run on "pc.dev.XP" manually :
putty -load "myconf" -l Fred -pw XXX -m script.file.that.unzip.and.integrates.sql
this works perfectly.
Same for :
cd \cygwin\bin
ssh Fred#dest "cd /var/www && ls -al"
If I try to exec() in php (wamp installed on "pc.dev.XP") those scripts they hangs. I'm pretty sure this is because the user is "SYSTEM" and not "Fred", and putty or ssh ask for a password but maybe I'm wrong.
Anyway I'm looking for a way to automate those 4 tasks I've described and I'm stuck because exec() hangs. There's no problem with safe_exec_mode or safe_exec_dir directives, they're disabled on the development machine, thus exec() works pretty well if I try some basic stuff like exec("dir")
Any idea what I could do / check / correct ?
I'm not sure if this is what you need, but I typically use a construct like this to sync databases across machines:
php extractFromDb.php | ssh user#remote.com "mysql remoteDatabaseName"
This executes the PHP script locally, and pipes the SQL commands the script prints out through SSH straigt into the remote mysql process which executes them in the remote database.
If you need compression, you can either use SSH's -C switch, or integrate the use of your compression program of choice like this:
php extractFromDb.php | gzip -9 | ssh user#remote.com "gunzip | mysql remoteDatabaseName"
You want to do this from PHP running under apache, as in I go to http://myWebserver.com/crazyScript.php and all this happens? Or you just want to write your scripts in PHP and invoke them via cmd line?
If you want the first solution, try running your apache/iss under a different user that has credentials to perform all those tasks.
"if I run on the development PC manually this works perfectly.".
Why not do it like that? When you run that script, I assume you're connecting to the local SSH server on the dev machine. When you do this, you are using the credentials Fred, so everything works. When you run the PHP script, you are right that it is probably running as SYSTEM.
Try either changing the user that apache is running as or use php to connect to the local ssh thereby using alternate credentials.
Here's what I did :
a batch file that :
Calls a php file via "php.exe my_extract_then_compress_then_ftp.php"
Calls rsync to synchronize the source folder
Calls putty -l user -pw password -m file_with_ssh_commands_to_execute
It works like a charm.

Categories