Programming / Scripting
I currently Program in Borland Delphi 7, and a combination of PHP / MySQL. Here I will post some cool stuff that I have done and worked out how to do, Might even include some nice little scripts / applications.
Thanks for reading,
Ben Harris
PHP scripting
Recently I started using a script that required open_basedir similar to Server Status 2k script on one of my websites. Due to the restrictions of open_basedir I was unable to run it properly, so I set out to achieve a method of using the script withouth using open_basedir. Open_basedir is a restriction that stops users 'getting out' of their own files (e.g. When a script tries to open a file with, for example, fopen() or gzopen(), the location of the file is checked. When the file is outside the specified directory-tree, PHP will refuse to open it).
The method I used for the script was of a few parts. The script previously required access to /proc/cpuinfo and /proc/meminfo and aditionally, access to the OS release/version file (found at /etc/fedora-release in my case)
Firstly, to get around the requirement of open_basedir for the Operating System version/release file, we simply use the following lines of PHP
Code:
The output of the above in my case is: Fedora Core release 6 (Zod)
That is the same output that would be produced by accessing /etc/fedora-release. Depending on your operating system, your release/version file will be in a diferent place.
The following are a list of the major OSes file locations:
/etc/debian_release
/etc/debian_version
/etc/SuSE-release
/etc/UnitedLinux-release
/etc/mandrake-release
/etc/gentoo-release
/etc/redhat_version
/etc/redhat-release
/etc/fedora-release
/etc/slackware-release
/etc/slackware-version
/etc/trustix-release
/etc/trustix-version
/etc/eos-version
/etc/gentoo-release
/etc/cobalt-release
/etc/lfs-release
/etc/rubix-version
Secondly to get around the requirement of open_basedir for /proc/cpuinfo. (there are two ways)
Firstly,
we simply use the following lines of PHP. They can show as much or as little information as you want, I have just chosen the main info.
code:
This outputs the following:
vendor_id : GenuineIntel
model name : Intel(R) Core(TM)2 CPU T5500 @ 1.66GHz
cpu cores : 2
cpu MHz : 1000.000
cache size : 2048 KB
bogomips : 3319.95
A limitation of this is that it only shows the info on the first CPU. Can be a problem if you have more than one CPU.
My next method is better if your CPU's never change (e.g. by changing server). If you have SSH access, run the following command
cat /proc/cpuinfo
and save the contence into a file named cpuinfo then you can use the file() command on it. So say, before you find file(/proc/cpuinfo) you could then change it to file(/home/user/public_html/cpuinfo) to get the same outcome. If you dont have SSH access, ask your host to run the above command and send you the output.
Finally, we get onto the /proc/meminfo bit. This one is a little harder than the others especially if you only want the figure (for example RAM used, which changes constantly).
code:
The script then outputs the following (however in real usage, you would be using the number not showing it in most cases)
2066096
95036
4192956
4192908
182628
0
Ok, The above code looks a little daunting. Lets work through it bit by bit.
1. $total_mem_full = exec('cat /proc/meminfo | grep "MemTotal"');
2. $total_mem_min = split(':', $total_mem_full);
3. $total_mem = substr($total_mem_min[1], 0, -3);
The first line gets the line that shows the Total memory (RAM) available at the time of script execution. If you echoed that. It would show the following: MemTotal: 2066096 kB. Ok, thats alright, but we cant use the number (for example for stats).
So next, line 2 says split the String $total_mem_full where there is a colon (:), then put it into an array.
Finally, line 3 says take $total_mem_min[1] (thats the bit after the colon, and looks like 2066096 kB), and then take the last 3 characters from the right hand side. This takes away the ' kB' (thats a space, and kB). We are now left with 2066096 which we can generate stats and stuff from :D
I hope this was handy to someone!
Regards
Ben Harris
PHP scripting
How to show PHP source. Sometimes you may want to show your lovely PHP script source to the world (like in my above entry). To do this, you get your script, and rename it from .php/.php3/.php4/.php5 etc.. to .phps Then, In the above examples (and the one below as a demo) I am just using an iFrame
Code:
The iFrame code I used is as follows:
<iframe frameborder="0" width="100%" height="120" scrolling="No" src="demo/fedora-demo.phps">You can add something here for if the users browser doesnt support iframes! (this wont be displayed unless they cant) </iframe>
Feel free to take a look at the source for my mini-script for showing the servers OS here!
Regards
Ben Harris
SSH (Secure Shell)
Like many people, I use Secure Shell, or SSH for short quite a lot with the linux shell. The following are some commonly used commands, and what they do. To use SSH, most people use a small standalone application called PuTTY, which can be found here.
ls : Lists files and directories within the current directory
ls -l : Lists files, permissions, owner, date modified and filesize
cd : Change directory.
Used as cd /home/username/public_html/ (apsolute path) or
Used ad cd public_html (relative path)
cd
~ : Takes you to your home directory (on cpanel servers this is usually /home/username)
cd ../ : Takes you to the directory above you.
cat filename : shows the contents of filename.
chmod : Change file access permissions for USER - GROUP - EVERYONE.
0 = --- No permission
1 = --X Execute only
2 = -W- Write only
3 = -WX Write and execute
4 = R-- Read only
5 = R-X Read and execute
6 = RW- Read and write
7 = RWX Read, write and execute
Example Usage:
chmod 777 filename
This gives the USER, GROUP and EVERYONE Read, Write and Execute permissions for the file named filename.
Common Usage
chmod 000
: No-one can access the file
chmod 644 : Default for most files
chmod 755 : Used for CGI scripts and directories
chmod 777 : Usually used if you want a script to write to a file
pico : Easy to use file editor.
Used as follows: pico filename
grep : Looks for patterns within files.
Used as follows: grep "what your looking for" /file/location or
Used as: cat /proc/cpuinfo | grep "vendor_id" this would show the line that shows the vendor_id within the file /proc/cpuinfo.
last : shows the last users that logged in. Can also use last -20 -a which would show the last 20 logins with hostnames
w : shows users that are logged in with where they are logged in from
who : Shows who is on the server in a shell
users : Shows users logged into shell
netstat : Shows all current network connections
top : Shows all live system processes, uptime, memory stats etc.. (The processes are show in a style similar to the Windows Task Manager).
touch : Creates an empty file.
Used as follows: touch /path/to/where/you/want/the/file/filename.html
du -sh : Shows disk usage in a readable format.
cp filename filename.copy : Would copy the file filename to filename.copy So there would now be 2 files called filename and filename.copy.
mv filename newdir/filename : Would move filename to the the directory named newdir. You can also change the destination filename to something diferent, so for example mv filename newdir/filename2 would move filename into newdir, and rename it filename2.
rm filename : Remove/Delete file called filename.
tar : Creating and Extracting tar and tar.gz files
Usage:
tar -zxvf file.tar.gz : Extracts the file file.tar.gz (Gzipped)
tar -xvf file.tar : Extracts the file file.tar (Not Gzipped)
tar -cf archives.tar directory/ : Takes everything from directory/ and puts it into archives.tar
unzip filename.zip : Extracts filename.zip into the current directory.
zip filename.zip filename : Zip filename into a zip file called filename.zip
logout : closes the session.
exit : exits the session (basically the same as logout)
This covers the major commands.
Regards
Ben Harris
