Thursday, February 21, 2019

Mounting Remote SSH Filesystem

This is how to mount remote SSH (Secure Sockets Host) filesystem on your local directory (e.g., /home/clemrasul/remoteserv) on Linux.  Mounting remote directories in this way allows you to do server side programming with ease since source code can be edited using your favorite GUI text editor.  The text editor need not have the feature of being able to log on to an SSH server.  Here are the steps:

Step 1:  Install the sshfs software

On Ubuntu, this is done using the command apt-get.

# apt-get install sshfs


Step 2:  Place it in the Linux module and give permissions

# modprobe fuse
# adduser clemrasul fuse
# chown root:fuse /dev/fuse
# chmod +x /dev/fuse

This assumes a username of "clemrasul" that is added to the "fuse" group.


Step 3:  Create the directory where the remove SSH filesystem will be mounted

# cd /home/clemrasul
# mkdir ~/remoteserv

This assumes a remove directory name of "remoteserv".


Step 4:  Mount the remote SSH directory as follows:

# sshfs {fully qualified SSH username}:{default directory}  {local directory mount point}

Example:

# sshfs clemrasul@taocoven.com:/home/clemrasul/html ~/remoteserv

You will be prompted by the remote SSH server for password.  Once authenticated, the remote directory can be seen in the local directory as:

# cd /home/clemrasul/remoteserv/





Tuesday, November 28, 2017

Simple Sample Size Formula

I have often been asked how to determine sample size in Statistics, while I always referred to Cochran [1963:1] for a complete explanation and Yamane [1967:2] for a simpler version of the formula, I discovered (an eureka moment!) that the existing published formula for sample size can still be further simplified.  Here is my simple sample size formula (Rasul Formula):

          Sample Size (n) = 1 / e^2

          where e is the margin of error (sampling error)

This simplified formula assumes an infinite population (or universe) and slightly greater than 95% confidence level.  The input requirement is only the margin of error expressed as a percentage.  If we assume further a 5% (.05) margin of error, the formula will give us a magic value of 400 (Rasul Sample), which can be interpreted as the sample size given infinite population at slightly more than 95% confidence level with 5% margin of error.

If you do not want to compute and was asked as to what sample size of proportion, the answer is 400 samples.

This simple formula also yields a simpler way of calculating the margin of error (e) given an actual sample number as follows (Rasul Margin of Error Formula):

          Margin of Error (e) = (1/n)^(1/2)

          or

          Margin of Error (e) is the square root of 1/n.

Let us say, you were able to get only 300 samples and you want to know what is your margin of error (e), applying the derived formula above will give you:

          e = (1/300)^(0.5)

          e = (0.003333)^(0.5)

          e = 0.057773 (or 6% rounded off to the nearest integer percent)

This tells us that even if we plan for a 5% margin of error, in real life situation, actual field condition (like peace and order situation) may not allow us to get the required sample size.  This particular formula can easily aid us in calculating the margin of error given the actual sample gathered.



Derivation

For those interested on how the formula was derived, here it is:

a)  Cochran's sample formula for proportions given infinite population (universe) is stated as,

          Sample Size (n) = ( Z^2 * p *q ) / e^2

          where:

                   Z is the abscissa of area of the normal curve
                   p is estimated proportion of an attribute that is present in the population
                   q is (1 - p )
                   e is the margin of error

b)  Remember that the Z score is the number of standard deviations a given proportion is away from the mean.  Given that, the desired confidence level and Z score relationship are as follows:

          Confidence Level          Z score

                     90%                         1.65
                     95%                         1.96
                     99%                         2.58


c)  Assuming the more widely acceptable confidence level of 95% and borrowing from the idea of Taro Yamane of approximating the Z score of 95% confidence by rounding it off to 2 instead of using 1.96, and

d)  Assuming further a more conservative proportion (p) of 50% (This is often the assumed value if we do not know the given proportion, which is most likely).  Therefore, p * (1 - p ) = 0.5 * (1-0.5) yields the value of 0.25,

e)  The formula can now can be stated as:

          n = [ 2^2 * (0.25) ]/e^2

          n = [ 4 * 0.25 ]/e^2


     Thereby producing the more simpler formula of:

          n = 1/ e^2

       








--------------------
References

[1]  Cochran, W.G. 1963. Sampling Techniques, 2nd Edition, New York.  John Wiley and Sons, Inc.

[2]  Yamane, Taro. 1967.  Statistics: An Introductory Analysis, 2nd Edition,  New York.  Harper and Row.


Wednesday, September 16, 2015

How to extract the monthly report of an Apache log file

To extract the monthly report of an Apache log file, use the Linux sed command.  For example, to get the logs from July 1, 2015 to July 31, 2015, issue the command:

# sed -n '/1\/Jul\/2015/,/31\/Jul\/2015/ p' /opt/lamp/logs/access.log > Jul2015_log


Installing Webalizer on Ubuntu 14.04

Webalizer is a web-based log file viewer.  To install the software on Ubuntu 14.04, type:

# apt-get install webalizer 

This will install the software in default location. The configuration file is in /etc/webalizer and the default web pages will be created in /var/www/webalizer directory.

The command of the webalizer is:

#/usr/bin/webalizer

Edit the webalizer.conf in the /etc/webalizer directory to point to the Apache log file location (e.g., /var/log/apache2/access_log) and issue the command #/usr/bin/webalizer to create the log file HTML in /var/www/webalizer directory.

To execute the webalizer command at regular intervals, use the Linux's crontab command.

Friday, October 18, 2013

Authentication System Design Pattern

The following are PHP scripts that can serve as design pattern for an application authentication system.

This is the "login.php" file that serves as the HTML login form.


<?php
echo "
<center>
<form method='post' action='authenticate.php'>
<fieldset>
<legend> Login </legend>
<table border='0'>
<tr>
<td rowspan='3'><img src='../img/emblem-keys.png'></td>
<td>Username</td><td><input type='text' name='username'></td>
</tr>
<tr>
<td>Password</td><td><input type='password' name='wordpass'></td>
</tr>
<tr>
<td>   </td><td><input type='submit' value='Login'><input type='reset' value='Reset'</td>
</tr>
</table>
</fieldset>
</form>
";
?>


This is the "authenticate.php" file that is executed after the HTML login form.


<?php
session_start();
include_once('db_postgresql.inc.php');

$sql1 = "SELECT COUNT(*) AS numfound FROM webappuser WHERE username='{$_POST['username']}' AND wordpass='{$_POST['wordpass']}' AND access_level='1' AND record_state='1'";
$result1 = $con->query(''.$sql1.'');

// Login Logic
$my_array1 = $result1->fetch(PDO::FETCH_ASSOC);
if ( $my_array1 ['numfound'] < 1 ) // Login Failed
	{
	$delay = 0;
	$url = "unauthorized.php";
	echo "<meta http-equiv='REFRESH' content='$delay;url=$url'>";
	exit;
	}

// Get the login information from the user	
$sql2 = "SELECT username, wordpass, access_leve, record_state FROM webappuser WHERE username='{$_POST['username']}' AND wordpass='{$_POST['wordpass']}' AND access_level='1' AND record_state='1'";
$result2 = $con->query(''.$sql2.'');
$my_array2 = $result2->fetch(PDO::FETCH_ASSOC);

// Register authentication information in the session
$username = $my_array2['username'];
$wordpass = $my_array2['wordpass'];
$access_level = $my_array2['access_level'];
$_SESSION["u_id"] = $u_id;
$_SESSION["username"] = $username;
$_SESSION["wordpass"] = $wordpass; 
$_SESSION["access_level"] = $access_level;

// Go to the welcome page
$delay = 0;
$url = "welcome.php";
echo "<meta http-equiv='REFRESH' content='$delay;url=$url'>";
?>


This is the "unauthorized.php" file that is called by the authentication system if username and password does not match.


<?php
session_destroy();
echo "
<center>
<form method='post' action='authenticate.php'>
<fieldset>
<legend> Login </legend>
<table border='0'>
<tr>
<td rowspan='3'><img src='../img/emblem-keys.png'></td>
<td>Username</td><td><input type='text' name='username'></td>
</tr>
<tr>
<td>Password</td><td><input type='password' name='wordpass'></td>
</tr>
<tr>
<td>   </td><td><input type='submit' value='Login'><input type='reset' value='Reset'</td>
</tr>
</table>
</fieldset>
</form>
<br>
<font color='red'><blink><b>Unauthorized ...</b></blink></font>
</center>
";
?>


This is the "logout.php" file that is called when the user clicks on a logout button.


<?php
session_destroy();
echo "
<center>
<form method='post' action='authenticate.php'>
<fieldset>
<legend> Login </legend>
<table border='0'>
<tr>
<td rowspan='3'><img src='../img/emblem-keys.png'></td>
<td>Username</td><td><input type='text' name='username'></td>
</tr>
<tr>
<td>Password</td><td><input type='password' name='wordpass'></td>
</tr>
<tr>
<td>   </td><td><input type='submit' value='Login'><input type='reset' value='Reset'</td>
</tr>
</table>
</fieldset>
</form>
<br>
<font color='red'><blink><b>You have been logged out...</b></blink></font>
</center>
";
?>


This is the "db_postgresql.inc.php" database connection file that is included in PHP page whenever it needs to connect to the database. The assumption here is the application is connected to a PostgreSQL database system. You can change this to whatever database brands you prefer. Furthermore, the database named "mydb" will have a table named "webappuser" with a minimum of four fields, namely: (i) username, (ii) wordpass, (iii) access_level, (iv) record_state. In like manner, you can modify this to suit your needs.


<?php
$host = "localhost";
$user = "postgres";
$pw = "wordpass123";
$db = "mydb";
# Using pg PDO
# Connection Check
try {
    $con = new PDO('pgsql:host='.$host.';dbname='.$db.'', $user, $pw );
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    #echo "Database Status:  <font color='green'><blink><b>Connected...</b></blink></font><hr />";
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>



This is the "secure.php" file that can be added at the top of any of the PHP file that you want protected from unauthorized users.


<?php
ini_set( "display_errors", 0);
session_start();
include_once( 'db_postgresql.inc.php' );
$username = $_SESSION["username"];
$wordpass = $_SESSION["wordpass"];

$sql1 = "SELECT COUNT(*) AS numfound FROM webappuser WHERE username='$username' AND wordpass='$wordpass' AND access_level='1' AND record_state='1'";
$result1 = $con->query(''.$sql1.'');
$my_array1 = $result1->fetch(PDO::FETCH_ASSOC);

// Login Logic
if ( $my_array1 ['numfound'] < 1 ) // Login Failed
	{
	$delay = 0;
	$url = "unauthorized.php";
	echo "<meta http-equiv='REFRESH' content='$delay;url=$url'>";	
	exit;
	}
?>


SQL Query Form Design Pattern Using "pg" driver

The following is a PHP Script that can serve as a design pattern for HTML form handling with "pg" connection driver for the PostgreSQL database.


<html>
<head>
<title>Postgresql pg Query Interface</title>
<!--
Clement L. Rasul
copyright (c) 2013
-->
</head>
<body>

<?php
$sql = trim($_POST['sql']);
$host = "localhost";
$user = "postgres";
$pw = "wordpass123";
$db = "testdb";

# Query Form
echo "<h1>PostgreSQL pg Query Interface</h1>";
echo "<form action='postgresql_pg_query.php' method='post'><textarea cols='80' rows='5' name='sql'>";
echo "$sql";
echo "</textarea><br /><input type='submit'></form>";

# Connection Check
$con = pg_connect("host=$host port=5432 dbname=$db user=$user password=$pw") or 
  die ("Database Status: <font color='red'><blink><b>Connection Error!</b></blink></font>"); echo "Database Status: <font color='green'><blink><b>Connected...</b></blink></font><hr />"; # SQL Query Script $result = pg_query($con, $sql); $num_rows = pg_num_rows($result); $num_cols = pg_num_fields($result); echo "Rows: ".$num_rows."<br />"; echo "Columns: ".$num_cols."<hr />"; echo "<table border='1'>"; # Table Heading $f=0; while ( $col = pg_field_name($result, $f) ) { $fn = $col; echo "<th>".$fn."</th>"; $f=$f+1; } # Table Content while ( $row = pg_fetch_array($result) ) { echo "<tr>"; $c = 0; while ( $c < $num_cols ) { echo "<td>".$row[$c]."   </td.>"; $c = $c+1; } echo "</tr>"; } echo "</table>"; ?> </body> </html>

SQL Query Form Design Pattern Using "PostgreSQL PDO" driver

The following is a PHP Script that can serve as a design pattern for HTML form handling with "PostgreSQL PDO" connection driver for the PostgreSQL database.


<html>
<head>
<title>PostgreSQL PDO Query Interface</title>
<!--
Clement L. Rasul
copyright (c) 2013
-->
</head>
<body>

<?php
$sql = trim($_POST['sql']);
$host = "localhost";
$user = "postgres";
$pw = "wordpass123";
$db = "eis2013v2";

# Query Form
echo "<h1>PostgreSQL PDO Query Interface</h1>";
echo "<form action='postgresql_pdo_query.php' method='post'><textarea cols='80' rows='5' name='sql'>";
echo "$sql";
echo "</textarea><br /><input type='submit'></form>";

# Connection Check
try {
    $con = new PDO('pgsql:host='.$host.';dbname='.$db.'', $user, $pw );
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	echo "Database Status:  <font color='green'><blink><b>Connected...</b></blink></font><hr />";
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

# SQL Query Script
$result = $con->query(''.$sql.'');
$num_rows = $result->rowCount();
$num_cols = $result->columnCount();
echo "Rows: ".$num_rows."<br />";
echo "Columns: ".$num_cols."<hr />";
echo "<table border='1'>";

# Table Heading
for ($i = 0; $i < $result->columnCount(); $i++) {
    $col = $result->getColumnMeta($i);
    $columns[] = $col['name'];
}

$f=0;
while ( $f < $num_cols ) {
	$fn = $columns[$f];
	echo "<th>".$fn."</th>";
	$f=$f+1;
    	}

# Table Content
# PDO::FETCH_ASSOC
# PDO::FETCH_BOTH
while ( $row = $result->fetch(PDO::FETCH_NUM) ) {
	echo "<tr>";
	$c = 0;
	while ( $c < $num_cols ) {
		echo "<td>".$row[$c]."   </td.>";
		$c = $c+1;
		}
	echo "</tr>";
    	}
echo "</table>";

?>

</body>
</html>