Pages

Saturday, December 15, 2012

How to make a bootable Windows USB under any LINUX version

There are a lots of softwares available for making bootable and live USB's but they all fail at one distro or another. So here is a magic bullet that fits all. All you need is a bootable OR live distro's iso image file. If you have a live distro you'd get a live USB, if you only have bootable iso image you would get a bootable USB only. First locate your USB
sudo fdisk -l

Then run this, replace path-to-iso with the path to where the iso file is on your disk and the ? with drive letter for your USB
dd if=/path-to-iso of=/dev/sd?
sync

Dont forget the sync command at the end to flush the usb-cache. Example
dd if=/home/me/ubu.iso of=/dev/sdc
sync
Note: Do not include the numeric number e.g. its /dev/sdc and NOT /dev/sdc1!

Sunday, October 7, 2012

Generating random numbers in c++


#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;

int main()
{
 int c;
 srand(time(NULL));
        while(1)
        {
       cout<<rand();
        }
 return 0;
}

Lets have an in depth look at the code. The actual random numbers are generated by the rand() function. The numbers that it generates are not random but are pseudorandom i.e. they appear to be random but are not. In actual the rand() function generates a predictable series of numbers. The series however can be initialized with a seed value which in the above code is being done using the srand() function. The srand() needs only be called once during a typical program. However given a same seed value the program will generate the same random numbers every time the program is run. In order to avoid this repetition across program executions. We have passed the time(NULL) as an argument to the srand() function. Which will initialize the srand with a new value every time the function is called, hence now the numbers generated by rand() would appear to be random.

Saturday, October 6, 2012

Using the arrow keys in c++ using scan codes

As a programmer you may have been tempted to use the arrow keys to take input from the user, did you get frustrated and gave up because it was not as trivial as it should be?
Well it is a piece of cake but to eat it you have to earn it.
Wait no further....
For those in a hurry here is the code

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
 int c;
 while (1)
 {
  c = getch(); // first call gets the 0xE0 byte or decimal 224
  switch(c)    // lets just waste the value
  {
  case 0:
  case 224:
   switch (getch()) //Here the actual code for the key is got
   {
   case 72: cout << "up arrow\n"; break;
   case 75: cout << "left arrow\n"; break;
   case 77: cout << "right arrow\n"; break;
   case 80: cout << "down arrow\n"; break;
   default: cout << "extended key " << c << "\n";
   }
  }
 }
 return 0;
}


Lets De-magicify

And here is the treat for the geeks.
Lets examine the code step by step. Before diving into details lets have a look at the background. Any key pressed from the key board generates a unique code (think of it as a number stored as a HEX value), this code is known as Scancode Generally each code is one byte in size but some keys (keys with special functions) generate two or more bytes and this code is referred to as extended Scancodes

The following keys fall under the extended category
  • Arrow keypad
  • Home keypad
  • Right Alt
  • Right Ctrl
  • keyboards with the arrow pad, home pad and number pad
Since the arrow keys fall under the extended scancode category. Lets see what code is generated when an arrow key is pressed.

If an arrow key is pressed it generates two bytes, one the code for the key pressed prefixed with an 0xE0 or decimal 224 (scan code for left shift release) on most systems. The corresponding scan codes for the keys are
  • Up 72
  • Down 80
  • Left 75
  • Right 77

Scancodes c++ and the getch function

The getch function gets a single character from the console, since extended scan codes, especially the arrow keys generate two bytes, you can already judge why we'd need two calls to getch(), the first call gets the code 224 and the next one gets the actual code for the key pressed. The rest is plain c++ switch statements. Enjoy your programming :)

Thursday, July 5, 2012

How to configure a LAMP server on Ubuntu 10.04

A LAMP server refers to Linux, Apache, MySQL, PHP or Python. Assuming you have already got a Ubuntu 10.04 (LINUX) machine up and running lets first install an Apache server.
sudo apt-get install apache2

Before we proceed with setting up apache lets set up the hosts file, this file holds ip mappings to machine names.
sudo gedit /etc/hosts

You will see something like this
127.0.0.1	localhost.localdomain	localhost

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts


Add the line 127.0.0.1 test-site-name.local to make it look something like this, i have used the name "ccl" in this example
127.0.0.1	localhost.localdomain	localhost
127.0.0.1 	ccl.local		z3d-laptop

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts


Now when you are done setting up the hosts file, we'll now configure virtual hosting so that we can host multiple domains (or subdomains) with the server.
cd /etc/apache2/sites-available

Once in the directory lets make setup our virtual host, for this we'll use the template provided by apache. This file is named default. Create a copy of the file first.
sudo cp default your-site-name

Now open the file you just created (by copying) for editing
sudo gedit your-site-name

This should open something similar to this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

 

In this file add the line ServerName test-site-name.local just above the DocumentRoot directive (in front of line 4). Edit DocumentRoot /var/www path on line 4 and set it to /path-to-the-test-site-WITHOUT-trailing-slash. It should look something like this
DocumentRoot /path-to-the-test-site-WITHOUT-trailing-slash

Edit path on line 9 and set it to /path-to-the-test-site-WITH-trailing-slash/. It should look something like this
 Directory /path-to-the-test-site-WITH-trailing-slash/ 

Please Note: Be careful regarding where there is a trailing slash where there is none. Also change
AllowOverride None

to
AllowOverride All

for the first two directory nodes (the / one and the one with the path to your site). That will allow all the .htaccess files to work properly and allow redirection.
Now we are almost done, we just need to enable the site
sudo a2ensite test-site-name 
sudo /etc/init.d/apache2 restart

In the next step lets configure and install MySQL
apt-get install mysql-server 

when prompted for a password choose one and save it for future reference Note the configuration file for MySQL is located at /etc/mysql/my.cnf we will not be modifying it but you should know it for future reference. Running the next command is recommended for securing your server, especially if it is a production server. When you will run the command you will be prompted for many things answering them with a yes is recommended.
mysql_secure_installation 

Next lets create a database and grant control to a user
mysql -u root -p 
create database test-db;
grant all on test-db.* to 'zed' identified by 'password';
flush privileges;
quit

Note: The semicolons are important and identify end of MySQL commands. In the above case a database with the name of test-db has been created and the user zed has been granted full control over it. The password for the user is password. You can keep any password that you like. With Apache and MySQL installed we are now ready to move on to installing PHP. Install php
apt-get install php5 php-pear

If you are not experienced with PHP the following configurations are useful for more detailed logging and better performance
sudo gedit /etc/php5/apache2/php.ini

Change the fields as shown below
max_execution_time = 30
memory_limit = 64M
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
display_errors = Off
log_errors = On
error_log = /var/log/php.log
register_globals = Off

Now restart Apache
sudo /etc/init.d/apache2 restart

if you need MySQL support in PHP then install the following package
apt-get install php5-mysql
apt-get install php5-suhosin

The second package is a recommended package for security, it provided additionally security for PHP Restart Apache to make sure everything is correct
sudo /etc/init.d/apache2 restart

Wola! Your lamp server is now configured and good to go! :)

Thursday, May 24, 2012

Creating a Bootable USB the hassle free way

There are a lots of softwares available for making bootable and live USB's but they all fail at one distro or another. So here is a magic bullet that fits all. All you need is a bootable OR live distro's iso image file. If you have a live distro you'd get a live USB, if you only have bootable iso image you would get a bootable USB only. First locate your USB
sudo fdisk -l

Then run this, replace path-to-iso with the path to where the iso file is on your disk and the ? with drive letter for your USB
dd if=/path-to-iso of=/dev/sd?

Example
dd if=/home/me/ubu.iso of=/dev/sdc
Note: Do not include the numeric number e.g. its /dev/sdc and NOT /dev/sdc1!

Monday, February 13, 2012

Installing ns-2.35 on Ubuntu 10.04

A lot of people dread having to install ns2. Well here is a simple tutorial to get you through in just a single go. Mostly people fail at installing ns2 in the first go because of the missing libraries and dependencies.

Here is what you do to install ns2 in the first go.

1. Download ns-allinone-2.35

2. cd into the directory where you have downloaded the ns-allinone-2.35.tar.gz file.
tar -xvzf ns-allinone-2.35.tar.gz

3. Next you need to install the required libraries

sudo apt-get install autoconf automake gcc g++ build-essential libxmu-dev libtool libxt-dev 

6. Now cd into the ns-allinone-2.35 directory and do ./install , hopefully everything will go smoothly and you ns-2 will be installed. But before you can proceed you need to set the environment variables.

gedit ~/.bashrc
copy the following lines at the end of the file, be sure to change /your/path to the path where ns is installed on your system.

# LD_LIBRARY_PATH
OTCL_LIB=/your/path/ns-allinone-2.35/otcl-1.14
NS2_LIB=/your/path/ns-allinone-2.35/lib
X11_LIB=/usr/X11R6/lib
USR_LOCAL_LIB=/usr/local/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OTCL_LIB:$NS2_LIB:$X11_LIB:$USR_LOCAL_LIB

# TCL_LIBRARY
TCL_LIB=/your/path/ns-allinone-2.35/tcl8.5.10/library
USR_LIB=/usr/lib
export TCL_LIBRARY=$TCL_LIB:$USR_LIB

# PATH
XGRAPH=/your/path/ns-allinone-2.35/bin:/your/path/ns-allinone-2.35/tcl8.5.10/unix:/your/path/ns-allinone-2.35/tk8.5.10/unix
NS=/your/path/ns-allinone-2.35/ns-2.35/
NAM=/your/path/ns-allinone-2.35/nam-1.15/
PATH=$PATH:$XGRAPH:$NS:$NAM

for instance on my system i would replace /your/path with /home/z3d

9. source ~/.bashrc to have the changes take effect or you can simply reopen terminal.

10. Now cd into the folder /ns-allinone-2.35/ns-2.35/ and type the command ./validate
it will take a considerably longer time than it did to install and in almost an hour you should get a message all tests successful.

11. Now you can type ns and begin doing your work :)

Monday, January 9, 2012