Php script prints values but not seen in browser


 
Thread Tools Search this Thread
Top Forums Web Development Php script prints values but not seen in browser
# 1  
Old 01-21-2016
Php script prints values but not seen in browser

greetings,

i'm a php and html novice and i figured i'd learn by diving in. i've written a php script that i've placed in the /var/www/html directory. it's supposed to give some relative info about the system when you point a browser at it. if i run the script using "php -q index.php" all the retrieved values are there, however when you point a browser at it the Hardware, Serial Number and Processor(s) values are blank. any suggestions(hints also appreciated) as to why the values are absent in the browser but present when run manually would be appreciated. i used `` rather than shell_exec to save on typing. while i'm sure there are other ways to do this and possibly smarter, you have to start somewhere:

Code:
<?php
$hostname = `hostname`;
$manufacturer = `dmidecode | grep Manufacturer | head -1 | cut -d: -f2`;
$model = `dmidecode | grep 'Product Name' | head -1 | cut -d: -f2`;
$serialno = `dmidecode | grep 'Serial Number' | head -1 | cut -d: -f2`;
$location = `cat /etc/snmp/snmpd.conf | grep syslocation | awk '{for (i=2; i<NF; i++) printf \$i " "; print \$NF}'`;
$contact = `echo "Contact Information: me at 734-555-5555"`;
$kernelarch = `uname -srvm`;
$processor = `dmidecode -s processor-version | head -1`;
$opsys = `cat /etc/redhat-release`;
$numprocessor = `cat /proc/cpuinfo | grep -c processor`;
$memtotal = `cat /proc/meminfo | grep MemTotal | awk '{print $2}'`;
$memvaluetotal = `expr $memtotal / 1000`; //i'm sure php can do math but that's for later
$memfree = `cat /proc/meminfo | grep MemFree | awk '{print $2}'`;
$memfreetotal = `expr $memfree / 1000`; //i'm sure php can do math but that's for later
$uptime = `uptime | awk '{print $3, $4}' | tr -d ,`;
?>

<p><b><u><big><?php echo $hostname;?></big></u></b><br/ >
<b>Hardware:</b><?php echo $manufacturer,$model;?><br/ >
<b>Serial Number:</b><?php echo $serialno;?><br/ ><p/ >

<p><b>Location: </b><?php echo $location;?><br/ >
<b>Contact Information: </b><?php echo $contact;?><p/ >

<p><b>Kernel and architecture: </b><?php echo $kernelarch;?><br/ >
<b>Processor(s): </b><?php echo $processor;?><br/ >
<b>Operating system: </b><?php echo $opsys;?><p/ >

<p><b>Processor total: </b><?php echo $numprocessor;?><br/ >
<b>Total memory: </b><?php echo $memvaluetotal;?>Mb<br/ >
<b>Free memory: </b><?php echo $memfreetotal;?>Mb<p/ >

<p><b>UpTime: </b><?php echo $uptime;?><br/ ><p/ >

the browser looks something like this:

Code:
t70cfd101 
Hardware:
Serial Number:

Location: York Cab #C205 
Contact Information: me at 734-555-5555

Kernel and architecture: Linux 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 
Processor(s): 
Operating system: Red Hat Enterprise Linux Server release 5.4 (Tikanga)

Processor total: 8 
Total memory: 49456556 Mb
Free memory: 4154896 Mb

UpTime: 459 days

and running the php script on the command line looks like:

Code:
sh: line 1: /: is a directory
sh: line 1: /: is a directory

<p><b><u><big>t70cfd101
</big></u></b><br/ >
<b>Hardware:</b> HP
 ProLiant BL280c G6
<br/ >
<b>Serial Number:</b> USE0293K8C
<br/ ><p/ >

<p><b>Location: </b>York Cab #C205
<br/ >
<b>Contact Information: </b>me at 734-555-5555<p/ >

<p><b>Kernel and architecture: </b>Linux 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64
<br/ >
<b>Processor(s): </b>Intel(R) Xeon(R) CPU X5570 @ 2.93GHz
<br/ >
<b>Operating system: </b>Red Hat Enterprise Linux Server release 5.4 (Tikanga)
<p/ >

<p><b>Processor total: </b>8
<br/ >
<b>Total memory: </b>49456556
Mb<br/ >
<b>Free memory: </b>4154888
Mb<p/ >

<p><b>UpTime: </b>459 days
<br/ ><p/ >

thanks in advance.
# 2  
Old 01-21-2016
Add a statement to print/display the PATH variable.
In all likelihood the dmidecode program is not found when the script is executed by the web server.
# 3  
Old 01-22-2016
In short, if you run php script on command line, you will get something like this
Code:
[akshay@localhost tmp]$ php -r 'echo `whoami`;'
akshay

At the same time, If you're trying to trigger it from a web interface, you will receive output as
Code:
apache

You need to check following
  1. The user running the code (you vs apache)
  2. SELinux (disable SELinux)
  3. PATH
  4. If you are running some time consuming big programs, then max_execution_time or memory_limit in php.ini
    program executed by browser (with apache) and for cli (terminal - without apache) may have different values for max_execution_time or memory_limit - usual php.ini for cli has more memory and it could run for longer period of time
  5. Whether user apache has enough permission to read, write and execute programs/files
# 4  
Old 01-22-2016
thank you all for replying.

akshay,
it didn't go unnoticed that the issue was likely pointing to the execution of dmidecode. i had already disabled selinux and even tried executing /usr/sbin/dmidecode with no luck. it didn't occur to me the command was being executed by apache and not root. so that's the issue, since dmidecode requires access to /dev/mem. so now i need to find a secure way for the apache user to execute dmidecode. or perhaps using sudo. not being a web admin or a php expert i'll have to dig in a little deeper. i suppose the short answer would be to "chmod 4755 /usr/sbin/dmidecode".

thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script which telnets to a device, runs commands and prints output to a file

I am connecting to a device using telnet, I want my script to perform certain commands : ie- show device , show inventory..etc and write the output it sees from the terminal to a file. this is what I have got : #!/usr/bin/expect -- set running 1 spawn telnet <ip address> expect ... (1 Reply)
Discussion started by: samantha123
1 Replies

2. Shell Programming and Scripting

Script to compare 2 files and prints difference as output sidebyside

Hi All, Am trying script to compare 2 files and print the difference found from old file to new file on line by line basis on side by side display. Basically line by line comparision and files may contain blank line as well I know we have compare/diff commands but i don't how to make... (10 Replies)
Discussion started by: Optimus81
10 Replies

3. Shell Programming and Scripting

Shell script that check the argument passed to it and prints error if test condition is not met

I want to make a script that check for the argument passed to it and generates an error in case any character/string argument passed to it. I am using below code, but its not working. can anyone help. #!/bin/bash if ]; then echo 'An integer argument is passed to the script hence... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

4. Emergency UNIX and Linux Support

Script issue - Prints pages with jibberish...

Hi all, I have a script from a programmer, for which I need to analyze a problem. The script gathers audit info and prints out the results. Two different departments use it, on two different printers. In the script department the there are no issues. In the other department the same script... (10 Replies)
Discussion started by: zixzix01
10 Replies

5. Shell Programming and Scripting

Script that prints 2 messages to a screen session

Im trying to make a script that prints 2 messages to a screen session, one after the other. screen -x session44 -X stuff "`printf "Test 1\r"`" This works fine, but adding a second lien with a different message yields no results. Changed Subject: Please Follow Forum Rules Regarding... (1 Reply)
Discussion started by: kylecn
1 Replies

6. Shell Programming and Scripting

Execute shell script from browser through php

Hi all, I want to execute a shell script in which I call ssh to execute a remote command. Everything works fine from command line but when I try to call from PHP (with exec or whatever function) it fails. After some time I came to the root cause: when www-data execute the ssh command it asks... (4 Replies)
Discussion started by: Evan
4 Replies

7. Shell Programming and Scripting

echo prints nothing-shell script

could anyone tell me why when i execute the following script, echo returns blank set k = 1 echo $k (9 Replies)
Discussion started by: saman_glorious
9 Replies

8. Shell Programming and Scripting

Execute interactive bash menu script in browser with PHP

I have an interactive menu script written in bash and I would like use PHP to open the interactive bash menu in a browser. Is this possible? Using the sytem() function in php runs the script but it's all garbled. Seems like maybe a terminal window needs to be opened in php first? ... (1 Reply)
Discussion started by: nck
1 Replies

9. Shell Programming and Scripting

Multiple PHP sessions within the same browser instance

Dear all..... I am currently writing a Help-Desk / Knowledge Base application using PHP/PostGreSQL. I authenticate the user using a quite elaborate mechanism of cookies. The problem is that using cookies (I also have a version using sessions with the same problem), I can only seem to get one... (4 Replies)
Discussion started by: zazzybob
4 Replies
Login or Register to Ask a Question