Search This Blog

Wednesday, June 24, 2009

To Forward all Traffic from one machine to another IPTABLES

Following Rules are Required to Forward all Traffic From __YOURMACHINE__ to __NEWGATEWAY__
    /sbin/iptables -t filter -I FORWARD -s __YOURMACHINE__ -i eth0 -p tcp -j ACCEPT
    /sbin/iptables -t nat -I PREROUTING -s __YOURMACHINE__ -i eth0 -p tcp -j ACCEPT
    /sbin/iptables -t mangle -I PREROUTING -s __YOURMACHINE__ -i eth0 -j ROUTE --oif eth0 --gw __NEWGATEWAY__

Wednesday, June 17, 2009

JavaScript Object Notation (JSON)

Abstract

JavaScript Object Notation (JSON) is a lightweight, text-based,
language-independent data interchange format.  It was derived from
the ECMAScript Programming Language Standard.  JSON defines a small
set of formatting rules for the portable representation of structured
data.

1. Introduction

JavaScript Object Notation (JSON) is a text format for the serialization of structured data. It is derived from the object literals of JavaScript, as defined in the ECMAScript Programming Language Standard, Third Edition [ECMA]. JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays). A string is a sequence of zero or more Unicode characters [UNICODE]. An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array. An array is an ordered sequence of zero or more values. The terms "object" and "array" come from the conventions of JavaScript. JSON's design goals were for it to be minimal, portable, textual, and a subset of JavaScript.

Why JSON?

The benefit of JSON is that it is recognized natively by JavaScript. No need for parsing an XML document to extract the data and get it throught the net.

JSON and XML

Benefits of JSON: - The easiness of reading. - The easiness of using. Benefits of XML: - XML is extensible. - It is widely used and recognized by almost all programming languages. Unfortunally, both XML and JSON are enable to integrate a large amount of data in binary form.

The syntax of JSON

The components of JSON: - An object: contains objets or attributes. - A scalar variable: Number, String, Boolean. - An array. - Literal values: null, true, false, "string of characters", and numerical values.

Object

It contains a member or a list of members, and each member has the form:

"name" : "value"

The syntax of the object is:

{ member, member, .... }

Array

A collection of values, separated by commas. [ value, value, ....]

Values

A value may be: an object, an array, a litteral (string, number, true, false, null).

Nothing more is required to create a JSON file!

Example of JSON file

A simple example, designing a menu: It is an object made of members that are an attribute and an array that holds other objects, the rows of the menu.


{
"menu": "File",
"commands": [
    {
        "title": "New",
        "action":"CreateDoc"
    },
    {
        "title": "Open",
        "action": "OpenDoc"
    },
    {
        "title": "Close",
        "action": "CloseDoc"
    }
 ]
}
The XML equivalent:
<?xml version="1.0" ?>
<root>
<menu>File</menu>

<commands>
   <item>
       <title>New</value>
       <action>CreateDoc</action>
   </item>

   <item>
       <title>Open</value>
       <action>OpenDoc</action>
   </item>
   <item>

       <title>Close</value>
       <action>CloseDoc</action>
   </item>
</commands>
</root>

How to use the format

The JSON file allows to load data from the server or to send data to it, in this format. For example, storing the content of a form, just filled by an user. This involves three steps: the browser processing, the server processing, and the data exchange between them.

Client side (browser)

This is rather easy, as JSON is a part of the JavaScript definition. The content of a file, or the definition of the data is assigned to a variable, and this variable becomes an object of the program.

Server side

JSON file are used by various programming languages, including PHP and Java thanks to parsers that allow to get the content and that may even convert it into classes and attributes of the language. The json.org includes a C parser and a list of parsers in other languages.

Data exchange

Loading a file may be accomplished from JavaScript in several ways: - direct including of the file into the HTML page, as a JavaScript .js external file. - loading by a JavaScript command. - using XMLHttpRequest. The JSON file is parsed by the eval() JavaScript function. Sending the file to the server may be accomplished by XMLHttpRequest. The file is sent as a text file and processed by the parser of the programming language that uses it.

Example

The XMLHttpRequest code:

var req = new XMLHttpRequest();
req.open("GET", "file.json", true);
req.onreadystatechange = myCode;   // the handler
req.send(null); 

The JavaScript handler:

function myCode()
{
 if (req.readyState == 4)
 {
      var doc = eval('(' + req.responseText + ')');
 }
}
Using the data:
var menuName = document.getElementById('jsmenu');   // finding a field
menuName.value = doc.menu.value;           // assigning a value to the field
How to access data:
doc.commands[0].title      // read value of the "title" field in the array
doc.commands[0].action     // read value of the "action" field in the array

For a Demo click here:

Tuesday, June 16, 2009

To add a partition which stores data in RAM of Linux

__REQUIREDPATH__ means the path of the temporary partition which you want
  1. echo "tmpfs __REQUIREDPATH__ tmpfs rw,size=10M" >> /etc/fstab
  2. mkdir __REQUIREDPATH__
  3. Change ownership appropiately
  4. mount -t tmpfs -o size=10m tmpfs __REQUIREDPATH__
Note: You will have to do the above steps again whenever your machine is rebooted. or add the above steps in /etc/rc.local For Eg: __REQUIRESPATH__ = "/var/tempdata/"

Wednesday, June 10, 2009

Superuser password Lost ?? & Single user not working ??

If "single user" mode doesn't let you in, then
  • Boot a "live distro" CD, like Ubuntu
  • Obtain a commandline (run xterm or terminal or something)
  • Get root; with "live boot" distros, this is usually dead simple, as the root password is usually provided as part of the documentation
  • Mount your real root directory onto an unused directory of the live boot environment (i.e. onto /mnt). Make certain you mount it rw
  • cd to your newly-mounted root directory
  • Enter the following command: "chroot . /bin/bash" to obtain a command prompt that uses your newly-mounted root directory and /it's/ files (including it's etc/passwd and etc/shadow)
  • Enter the passwd command to change roots password. When you are done
  • Enter "exit" to quit the chroot shell
  • cd to the real /
  • Umount your installation's root directory
  • Shutdown and Reboot
What this does is permit the password program from /your/ installation to work on the password files from /your/ installation, by running everything in an environment that starts with your installation as the root filesystem. Up to the point where you chroot, you are just establishing a running environment, but when you chroot, you have effectively re-established /your/ installation, with access as root.

Tuesday, June 9, 2009

VLAN: Virtual Local Area Network and IEEE 802.1Q

Definition of: virtual LAN Also called a "VLAN," it is a logical subgroup within a local area network that is created via software rather than manually moving cables in the wiring closet. It combines user stations and network devices into a single unit regardless of the physical LAN segment they are attached to and allows traffic to flow more efficiently within populations of mutual interest. VLANs are implemented in port switching hubs and LAN switches and generally offer proprietary solutions. VLANs reduce the time it takes to implement moves, adds and changes. VLANs function at layer 2. Since their purpose is to isolate traffic within the VLAN, in order to bridge from one VLAN to another, a router is required. The router works at the higher layer 3 network protocol, which requires that network layer segments are identified and coordinated with the VLANs. This is a complicated job, and VLANs tend to break down as networks expand and more routers are encountered. The industry is working towards "virtual routing" solutions, which allows the network manager to view the entire network as a single routed entity.

Monday, June 8, 2009

Understanding /etc/modules.conf

Loading modules for the hardware

All modules and settings are controlled in /etc/modules.conf for mandrake and /etc/conf.modules for redhat. Other methods are using the lilo.conf and /boot/grub/menu.lst.

By using the lilo.conf the modules are load as part of the kernel (built in), but the correct way of doing this is using the modules.conf file.

Before I go to the actual configuration, you may want to try some of these commands related to modules:

Commands Description
/sbin/lsmod Lists all configured modules on your system
/sbin/modprobe -l Lists all available modules
/sbin/modprobe -c Lists all configured aliases
/sbin/modprobe -r [module] Removes a loaded module rmmod
/sbin/modprobe [module] Loads a module same as insmod
man modprobe Loads the documentation for modprobe

Note: For all new modules downloaded or compiled must be moved to /lib/modules/[kernel version]/ for the system to be able to use it.

After moving the modules to the appropriate directory, run depmod -a to let the system know about the new module, to test the module run this: modprobe [module name] if the module is loaded without error that means everything is OK. If you get any error messages, that means the module is wrong, or maybe the device is already running with the appropriate driver. Believe me it happens.

Modules loaded in the /etc/modules.conf, are loaded as alias of drivers. For example:

          Alias          Device          Driver
         alias          eth0            8139too 

Many of the modules require further configurations, like I/O addresses and IRQ numbers. The following is an example of a parallel port assigned in the file modules.conf

        alias parport_lowlevel parport_pc
       options parport_pc io=0x378 irq = 7 

It is very easy to identify, io=0x378 and irq = 7, is the address and irq assigned to the first onboard parallel port in your system.

When you install additional hardware in your system, you have to manually edit the modules.conf, unless hard drake allows you to configure it.

Step 2

From the assumption of our next two-port installation we continue…

The modules.conf already contains some information; edit it to add the new information.

This port1 (integrated on the motherboard):

       alias parport_lowlevel parport_pc
      options parport_pc io=0x378 irq=7 

You could just add the parameters required in the same line or create it separately for each port. Combination of the ports 1,2,3:

alias parport_lowlevel parport_pc
options parport_pc io=0x378, 0x6400, 0x6500 irq=7, 5, auto 

As you can see in the io=0x378, 0x6400, 0x6500 are the IO address for each parallel port1, port2 port3. The port2 and port3 belongs to the dual ports B&C.

The IRQs= 7, 5, auto are assigned to the port1, port2 and port3. You can either set the IRQ number manually or set it to get first available IRQ automatically.

As I have already stated, you could also separate the settings as in the following:

Separation of ports by line

       alias parport_lowlevel parport_pc
      options parport_pc io=0x378 irq=7
 
      alias parport_lowlevel parport_pc
      options parport_pc io=0x6400 irq=5

      alias parport_lowlevel parport_pc
      options parport_pc io=0x6500 irq=auto 

If you exit the editor and save the file, restart the system. The parallel ports lp0, lp1 and lp2 should be enabled now and is ready to use. Other hardware may require special procedures, read the documentations or browse the web for help. You could also load modules or special configuration in other way such as search for it and execute it at boot time, this could be accomplished by creating a bash file. Call it whatever you want, makes it +x executable and add it in /etc/rc.d/ Before you attempt the rc.d, try adding it in /etc/modules this file is used to load from the kernel; as far as the documentation concerns. Note I am not referring to modules.conf, which is in the same directory.

IMPORTANT

Possible explanation to modules.conf alias

The modules.conf mystery puzzled me. I decided to read the documentation
in the kernel sources. Here's what I found

>From /usr/src/linux/Documentation/modules.txt
---------------------------------------------
Whenever a program wants the kernel to use a feature that is only
available as a loadable module, and if the kernel hasn't got the
module installed yet, the kernel will ask the kerneld daemon to take
care of the situation and make the best of it.

This is what happens:

       - The kernel notices that a feature is requested that is not
         resident in the kernel.
       - The kernel sends a message to kerneld, with a symbolic
         description of the requested feature.
       - The kerneld daemon asks e.g. modprobe to load a module that
         fits this symbolic description.
--------
Here's the important point that I think solves the mystery:
--------
       - modprobe looks into its internal "alias" translation table
         to see if there is a match.  This table can be reconfigured
         and expanded by having "alias" lines in "/etc/modules.conf".
--------

So, as I see it, the ppp aliases *should* already be in modprobe's
internal alias translation table. People with older versions of modutils
might not have it in their modprobe's alias tables, and so would need
the alias lines in /etc/modules.conf .

(NOTE: The following documentation file is a bit old, but it is still
relevant. There is no longer a kerneld daemon, IIRC, but I might be
mistaken.)

Important Note was added by
Jan Michael Ibanez
Student, University of Asia & the Pacific

Wednesday, June 3, 2009

Perl (Practical Report and Extraction Language.)

Today if have found a good link to learn Perl Script. Click on the title to start Learning Perl