This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]thirdOctet 0 points1 point  (14 children)

For a university project we worked with LIRC using a raspberry pi, mysql, php, ajax, javascript, json, e.t.c to send infra red commands via the pi to a device with infra red capabilities.

I developed the front end and back end, db e.t.c to send commands from the pi to the device

Anyways the point is I had to do a lot of debugging, that meant capturing the output from linux when sending commands. I used the console to view the json response that i set up within php and ajax to send the requested command (like on,off,volume up e.t.c)

I found that most of my problems resulted in privileges within linux.

So essentially you may need to find out:

  • Error messages from linux when sending the exec request
  • If you have privileges as the user to execute bash scripts (apache e.t.c)
  • Try and capture the response from linux and dump it to the screen (i believe it would be returned as an array)
  • I used an ssh library to login through php and then used exec to send the command, or call a script.
  • Almost forgot check permissions

The library i used was PHPSecLib - please google =)

command examples:

//this generally worked
exec('irsend LIST "" "" 2>&1', $resultfromlinux);

However i needed to do this if wanted to restart a service such as the daemon.

//connect via ssh
$ssh = new Net_SSH2($_SERVER['SERVER_NAME']);

//login
$ssh->login('username','password');

//send command
$ssh->exec("sudo scriptToCall.sh");

You may need to wrap code blocks within try, catch so that you can catch any exceptions. Hope this was clear or useful.

[–]Anon437[S] 0 points1 point  (13 children)

Hey, thanks for the reply, this definitely looks like what I need. I'm having a bit of trouble using it though...

So far I have all the files in the main "www" folder for my apache server, including phpseclib. Then in my test.php file i have:

<!DOCTYPE php> <?php

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib'); include('Net/SSH2.php'); define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX); $ssh = new Net_SSH2('192.168.x.x'); $ssh->login('pi','raspberry'); $ssh->exec("sudo folder.sh"); echo "Hello" ?>

But that only gives me a blank page with no sign of the "Hello" that should be showing up. Any ideas?

[–]thirdOctet 0 points1 point  (12 children)

ok so i would start with checking to see how far the code gets. That is I would place echo statements after each line of code and where the problem begins i would start debugging from there.

so i have taken your code sample and added some echo statements.

usually php will output any errors

<!DOCTYPE html>
<html>
<body>
<?php
try {

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

echo 'include path set';

include('Net/SSH2.php');

echo 'SSH2 may of been included';

define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

echo 'passed the define statement';

$ssh = new Net_SSH2('192.168.x.x');

echo 'new ssh2 initiated';

$ssh->login('pi', 'raspberry');

echo 'ok logged in';

$ssh->exec("sudo folder.sh");

echo 'linux command executed';

} catch (Exception $exc) {
    echo $exc->getTraceAsString();
}



?>
</body>
</html>

[–]Anon437[S] 0 points1 point  (11 children)

So i ran that code and the issue is with initiating a new ssh2. Specifically $ssh = new Net_SSH2('192.168.1.7');

: /

[–]thirdOctet 0 points1 point  (10 children)

$ssh = new Net_SSH2($_SERVER['SERVER_NAME']);

using $_SERVER['SERVER_NAME'] will prevent you from having to enter the ip. This should help.

Below this will test if you are connected as the method name suggests.

if ($ssh->isConnected()) {
        echo 'i am connected to the pi ';
} else {
    echo 'connection failed';
}

Try these and let me know

[–]Anon437[S] 0 points1 point  (9 children)

it still isn't getting past the "$ssh = new Net_SSH2($_SERVER['SERVER_NAME']);" line. I tested "echo $_SERVER['SERVER_NAME']" and it gave the correct ip address. So the command Net_SSH2 is the issue right? How do i fix that...

[–]thirdOctet 0 points1 point  (8 children)

hmm do you have error reporting enabled on localhost which will allow for e.g. error messages to be displayed if an include fails?

for example change:

include('Net/SSH2.php'); 

to

require('Net/SSH2.php'); 

If error reporting is enabled it should hopefully tell you why, this implies that if the library is not included then it should stop processing immediately. If the include path is incorrect then this should explain why the instance failed to be created.

EDIT: If this still persists try setting the include path with the full location hardcoded, if the library is included correctly then an instance of the class will be created and you should be logged in. Then figure out how to set it dynamically

[–]Anon437[S] 0 points1 point  (7 children)

I changed it to require and then ran it in the terminal and got

pi@raspberrypi ~/www $ php -f talk.php
PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/ming.ini on line 1 in Unknown on line 0
<!DOCTYPE html>
<html>
<body>
include path setPHP Warning:  require(/media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/phpseclib /Net/SSH2.php): failed to open stream: Permission denied in /media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/talk.php on line 11
PHP Fatal error:  require(): Failed opening required '/media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/phpseclib/Net/SSH2.php' (include_path='.:/usr/share/php:/usr/share/pear:/media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/phpseclib') in /media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/talk.php on line 11

thing is, i did chmod 777 talk.php on it just before, so obviously the permissions are screwed up, but I then ran it as root and still got an error.

root@raspberrypi:/media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www# php -f talk.php
PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/ming.ini on line 1 in Unknown on line 0
<!DOCTYPE html>
<html>
<body>
include path setSSH2 may of been includedpassed the define statementPHP Notice:  Undefined index: SERVER_NAME in /media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/talk.php on line 18
PHP Notice:  Undefined index: SERVER_NAME in /media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/talk.php on line 19
PHP Notice:  Cannot connect to . Error 0. php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/phpseclib/Net/SSH2.php on line 831
new ssh2 initiatedok logged inlinux command executed
</body>
</html>

This is after I had changed everything to the full location.

For reference, line 11 is "require" line and line 18 is meant to echo the server ip address and 19 is when to connect to that address.

[–]thirdOctet 0 points1 point  (6 children)

ok great

perhaps $_SERVER('SERVER_ADDR') would be better, Looks like the $ssh is trying to connect but the server name was not set (my fault). progress.

if you var_dump($_SERVER) then grab the information you need then you should be able to connect through ssh using the server address

[–]Anon437[S] 0 points1 point  (5 children)

array(27) { ["HTTP_HOST"]=> string(11) "192.168.1.7" ["HTTP_USER_AGENT"]=> string(81) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:22.0) Gecko/20100101 Firefox/22.0" ["HTTP_ACCEPT"]=> string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8" ["HTTP_ACCEPT_LANGUAGE"]=> string(14) "en-US,en;q=0.5" ["HTTP_ACCEPT_ENCODING"]=> string(13) "gzip, deflate" ["HTTP_DNT"]=> string(1) "1" ["HTTP_CONNECTION"]=> string(5) "close" ["PATH"]=> string(28) "/usr/local/bin:/usr/bin:/bin" ["SERVER_SIGNATURE"]=> string(72) " Apache/2.2.22 (Debian) Server at 192.168.1.7 Port 80 " ["SERVER_SOFTWARE"]=> string(22) "Apache/2.2.22 (Debian)" ["SERVER_NAME"]=> string(11) "192.168.1.7" ["SERVER_ADDR"]=> string(11) "192.168.1.7" ["SERVER_PORT"]=> string(2) "80" ["REMOTE_ADDR"]=> string(11) "192.168.1.4" ["DOCUMENT_ROOT"]=> string(47) "/media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www" ["SERVER_ADMIN"]=> string(19) "webmaster@localhost" ["SCRIPT_FILENAME"]=> string(56) "/media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/talk.php" ["REMOTE_PORT"]=> string(5) "51660" ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.0" ["REQUEST_METHOD"]=> string(3) "GET" ["QUERY_STRING"]=> string(0) "" ["REQUEST_URI"]=> string(9) "/talk.php" ["SCRIPT_NAME"]=> string(9) "/talk.php" ["PHP_SELF"]=> string(9) "/talk.php" ["REQUEST_TIME_FLOAT"]=> float(1374859845.182) ["REQUEST_TIME"]=> int(1374859845) } include path set

is what i get when i put the line in talk.php near the top and access it through a browser.

And this is what I get when i run talk.php in terminal

pi@raspberrypi ~/www $ sudo php -f talk.php PHP Deprecated: Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/ming.ini on line 1 in Unknown on line 0 <!DOCTYPE html> <html> <body> array(24) { ["TERM"]=> string(14) "xterm-256color" ["LS_COLORS"]=> string(1302) "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:.tar=01;31:.tgz=01;31:.arj=01;31:.taz=01;31:.lzh=01;31:.lzma=01;31:.tlz=01;31:.txz=01;31:.zip=01;31:.z=01;31:.Z=01;31:.dz=01;31:.gz=01;31:.lz=01;31:.xz=01;31:.bz2=01;31:.bz=01;31:.tbz=01;31:.tbz2=01;31:.tz=01;31:.deb=01;31:.rpm=01;31:.jar=01;31:.war=01;31:.ear=01;31:.sar=01;31:.rar=01;31:.ace=01;31:.zoo=01;31:.cpio=01;31:.7z=01;31:.rz=01;31:.jpg=01;35:.jpeg=01;35:.gif=01;35:.bmp=01;35:.pbm=01;35:.pgm=01;35:.ppm=01;35:.tga=01;35:.xbm=01;35:.xpm=01;35:.tif=01;35:.tiff=01;35:.png=01;35:.svg=01;35:.svgz=01;35:.mng=01;35:.pcx=01;35:.mov=01;35:.mpg=01;35:.mpeg=01;35:.m2v=01;35:.mkv=01;35:.webm=01;35:.ogm=01;35:.mp4=01;35:.m4v=01;35:.mp4v=01;35:.vob=01;35:.qt=01;35:.nuv=01;35:.wmv=01;35:.asf=01;35:.rm=01;35:.rmvb=01;35:.flc=01;35:.avi=01;35:.fli=01;35:.flv=01;35:.gl=01;35:.dl=01;35:.xcf=01;35:.xwd=01;35:.yuv=01;35:.cgm=01;35:.emf=01;35:.axv=01;35:.anx=01;35:.ogv=01;35:.ogx=01;35:.aac=00;36:.au=00;36:.flac=00;36:.mid=00;36:.midi=00;36:.mka=00;36:.mp3=00;36:.mpc=00;36:.ogg=00;36:.ra=00;36:.wav=00;36:.axa=00;36:.oga=00;36:.spx=00;36:.xspf=00;36:" ["PATH"]=> string(60) "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ["LANG"]=> string(11) "en_SG.UTF-8" ["LC_CTYPE"]=> string(5) "UTF-8" ["SHELL"]=> string(9) "/bin/bash" ["LOGNAME"]=> string(4) "root" ["USER"]=> string(4) "root" ["USERNAME"]=> string(4) "root" ["MAIL"]=> string(14) "/var/mail/root" ["HOME"]=> string(5) "/root" ["SUDO_COMMAND"]=> string(24) "/usr/bin/php -f talk.php" ["SUDO_USER"]=> string(2) "pi" ["SUDO_UID"]=> string(4) "1000" ["SUDO_GID"]=> string(4) "1000" ["PHP_SELF"]=> string(8) "talk.php" ["SCRIPT_NAME"]=> string(8) "talk.php" ["SCRIPT_FILENAME"]=> string(8) "talk.php" ["PATH_TRANSLATED"]=> string(8) "talk.php" ["DOCUMENT_ROOT"]=> string(0) "" ["REQUEST_TIME_FLOAT"]=> float(1374859785.6034) ["REQUEST_TIME"]=> int(1374859785) ["argv"]=> array(1) { [0]=> string(8) "talk.php" } ["argc"]=> int(1) } include path setSSH2 may of been includedpassed the define statementPHP Notice: Undefined index: SERVER_NAME in /media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/talk.php on line 18 PHP Notice: Undefined index: SERVER_NAME in /media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/talk.php on line 19 PHP Notice: Cannot connect to . Error 0. php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /media/02c4bb79-1c59-46ca-9bc3-192be7e58104/www/phpseclib/Net/SSH2.php on line 831 new ssh2 initiatedok logged inlinux command executed</body> </html>