Saturday, November 24, 2012

Howto: Configure Linux Virtual Local Area Network (VLAN)

VLAN is an acronym for Virtual Local Area Network. Several VLANs can co-exist on a single physical switch, which are configured via software (Linux commands and configuration files) and not through hardware interface (you still need to configure switch).
Hubs or switch connects all nodes in a LAN and node can communicate without a router. For example, all nodes in LAN A can communicate with each other without the need for a router. If a node from LAN A wants to communicate with LAN B node, you need to use a router. Therefore, each LAN (A, B, C and so on) are separated using a router.
VLAN as a name suggest combine multiple LANs at once. But what are the advantages of VLAN?
  • Performance
  • Ease of management
  • Security
  • Trunks
  • You don't have to configure any hardware device, when physically moving server computer to another location etc.
VLAN concepts and fundamental discussion is beyond the scope of this article. I am reading following textbooks. I found these textbooks extremely useful and highly recommended:
  • Cisco CNNA ICND books (part I and II)
  • Andrew S. Tanenbaum, Computer Networks book

Linux VLAN Configuration Issue

I am lucky enough to get couple of hints from our internal wiki docs :D.
  • Not all network drivers support VLAN. You may need to patch your driver.
  • MTU may be another problem. It works by tagging each frame i.e. an Ethernet header extension that enlarges the header from 14 to 18 bytes. The VLAN tag contains the VLAN ID and priority. See Linux VLAN site for patches and other information.
  • Do not use VLAN ID 1 as it may be used for admin purpose.

Linux VLAN How To

My VLAN ID is 5. So I need to copy file /etc/sysconfig/network-scripts/ifcfg-eth0 to /etc/sysconfig/network-scripts/ifcfg-eth0.5
# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.5
Now, I've one network card (eth0) and it needs to use tagged network traffic for VLAN ID 5.
  • eth0 - Your regular network interface
  • eth0.5 - Your virtual interface that use untagged frames
Do not modify /etc/sysconfig/network-scripts/ifcfg-eth0 file. Now open file /etc/sysconfig/network-scripts/ifcfg-eth0.5 using vi text editor:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0.5
Find DEVICE=ifcfg-eth0line and replace with:
DEVICE=ifcfg-eth0.5
Append line:
VLAN=yes
Also make sure you assign correct IP address using DHCP or static IP. Save the file. Remove gateway entry from all other network config files. Only add gateway to /etc/sysconfig/network file. Save and close the file. Restart network:
# /etc/init.d/network restart
Please note that if you need to configure for VLAN ID 2 then copy the copy file /etc/sysconfig/network-scripts/ifcfg-eth0 to /etc/sysconfig/network-scripts/ifcfg-eth0.2 and do the above procedure again.

Using vconfig command

Above method is perfect and works with Red hat Enterprise Linux / CentOS / Fedora Linux without any problem. However, you will notice that there is a command called vconfig. The vconfig program allows you to create and remove vlan-devices on a vlan enabled kernel. Vlan-devices are virtual ethernet devices which represents the virtual lans on the physical lan.
Please note that this is yet another method of configuring VLAN. If you are happy with above method no need to read below.
Add VLAN ID 5 with follwing command for eth0:
# vconfig add eth0 5
The vconfig add command creates a vlan-device on eth0 which result into eth0.5 interface. You can use normal ifconfig command to see device information:
# ifconfig eth0.5
Use ifconfig to assign IP address to vlan interfere :
# ifconfig eth0.5 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255 up
Get detailed information about VLAN interface:
# cat /proc/net/vlan/eth0.5
If you wish to delete VLAN interface delete command:
# ifconfig eth0.5 down
# vconfig rem eth0.5

What is a VLAN? How to Setup a VLAN on a Cisco Switch

Have you ever wondered what a Virtual LAN (or VLAN) is or been unclear as to why you would want one? If so, I have been in your place at one time too. Since then, I have learned a lot about what a VLAN is and how it can help me. In this article, I will share that knowledge with you.

What is a LAN?

Okay, most of you already know what a LAN is but let’s give it a definition to make sure. We have to do this because, if you don’t know what a LAN is, you can’t understand what a VLAN is.
A LAN is a local area network and is defined as all devices in the same broadcast domain. If you remember, routers stop broadcasts, switches just forward them.

What is a VLAN?

As I said, a VLAN is a virtual LAN. In technical terms, a VLAN is a broadcast domain created by switches. Normally, it is a router creating that broadcast domain. With VLAN’s, a switch can create the broadcast domain.
This works by, you, the administrator, putting some switch ports in a VLAN other than 1, the default VLAN. All ports in a single VLAN are in a single broadcast domain.
Because switches can talk to each other, some ports on switch A can be in VLAN 10 and other ports on switch B can be in VLAN 10. Broadcasts between these devices will not be seen on any other port in any other VLAN, other than 10. However, these devices can all communicate because they are on the same VLAN. Without additional configuration, they would not be able to communicate with any other devices, not in their VLAN.

Are VLANs required?

It is important to point out that you don’t have to configure a VLAN until your network gets so large and has so much traffic that you need one. Many times, people are simply using VLAN’s because the network they are working on was already using them.
Another important fact is that, on a Cisco switch, VLAN’s are enabled by default and ALL devices are already in a VLAN. The VLAN that all devices are already in is VLAN 1. So, by default, you can just use all the ports on a switch and all devices will be able to talk to one another.

When do I need a VLAN?

You need to consider using VLAN’s in any of the following situations:
  • You have more than 200 devices on your LAN
  • You have a lot of broadcast traffic on your LAN
  • Groups of users need more security or are being slowed down by too many broadcasts?
  • Groups of users need to be on the same broadcast domain because they are running the same applications. An example would be a company that has VoIP phones. The users using the phone could be on a different VLAN, not with the regular users.
  • Or, just to make a single switch into multiple virtual switches.

Why not just subnet my network?

A common question is why not just subnet the network instead of using VLAN’s? Each VLAN should be in its own subnet. The benefit that a VLAN provides over a subnetted network is that devices in different physical locations, not going back to the same router, can be on the same network. The limitation of subnetting a network with a router is that all devices on that subnet must be connected to the same switch and that switch must be connected to a port on the router.

Quick-Tip: SSH Tunneling Made Easy

I was surprised at how long it took me to find a good HOWTO on setting up a simple SSH tunnel that I wanted to write up this Quick-Tip.
Using OpenSSH on a Linux/Unix system you can tunnel all of the traffic from your local box to a remote box that you have an account on.
For example I tunnel all of my outbound E-mail traffic back to my personal server to avoid having to change SMTP servers, use SMTP-AUTH, etc. when I am behind firewalls. I find that hotel firewalls, wireless access points, and the other various NATing devices you end up behind while traveling often do not play nice.
To do this I use the following:
ssh -f user@personal-server.com -L 2000:personal-server.com:25 -N
The -f tells ssh to go into the background just before it executes the command. This is followed by the username and server you are logging into. The -L 2000:personal-server.com:25 is in the form of -L local-port:host:remote-port. Finally the -N instructs OpenSSH to not execute a command on the remote system.
This essentially forwards the local port 2000 to port 25 on personal-server.com over, with nice benefit of being encrypted. I then simply point my E-mail client to use localhost:2000 as the SMTP server and we're off to the races.
Another useful feature of port forwarding is for getting around pesky firewall restrictions. For example, a firewall I was behind recently did not allow outbound Jabber protocol traffic to talk.google.com. With this command:
ssh -f -L 3000:talk.google.com:5222 home -N
I was able to send my Google Talk traffic encrypted through the firewall back to my server at home and then out to Google. 'home' here is just an SSH alias to my server at home. All I had to do was reconfigure my Jabber client to use localhost as the server and the port 3000 that I had configured.

Creating an IP Tunnel using GRE on Linux

IP Tunelling

We will do IPv4 tunneling using GRE. GRE is a tunneling protocol that was originally developed by Cisco, and it can do a few more things than IP-in-IP tunneling. For example, you can also transport multicast traffic and IPv6 through a GRE tunnel.
We are using Debian with linux kernel 2.4.26. In Linux, you'll need the ip_gre.o module.

Starting Configuration

We have 2 routers X and Y, and intermediate network C (or let's say, Internet).
router X
Router X is connected to the Internet on interface eth0 and network A on eth1.
interface eth0 :: address 169.229.255.134 on the Internet (or network C)
interface eth1 :: address 10.0.2.1, network 10.0.2.0/24 (network A)

router Y
Router Y is connected to the Internet on interface eth0, network B on eth1 and network C on eth2.
interface eth0 :: address 207.241.237.37 on the Internet (or network C)
interface eth1 :: address 10.0.3.1, network 10.0.3.0/24 (network B)
interface eth2 :: address 10.0.4.1, network 10.0.4.0/24 (network C)
As far as network C is concerned, we assume that it will pass any packet sent from X to Y and vice versa. How and why, we do not care.

Tunnelling Objective

Create a tunnel between router X and Y, such that we can route traffic from network A (connected to X) to networks B and C (connected to Y). This tunnel will look just like a wire between the two routers with its own subnet (10.0.201.0/24)

Create Tunnels

On router X, commands are
iptunnel add tunX mode gre remote 207.241.237.37  local 169.229.255.134 ttl 225
ifconfig tunX 10.0.201.1/24
ifconfig tunX up
ifconfig tunX pointopoint 10.0.201.2
ifconfig tunX multicast
In line 1, we added a tunnel device, and called it tunX. Furthermore we told it to use the GRE protocol (mode gre), that the remote address is 207.241.237.37 (the router Y at the other end), that our tunneling packets should originate from 169.229.255.134 (which allows your router to have several interfaces and choose which one to use for tunneling) and that the TTL field of the packet should be set to 255 (ttl 255).
Line 2 gives the newly born interface tunY the address 10.0.201.1.
Line 3 enables the device.
Line 4 is necessary to set the IP address of the peer. Need when using dynamic routing with RIP/OSPF with Zebra. Refer to Routing HOWTO for more details.
Line 5 is necessary to enable multicast - so that routing with Zebra works (they normally multicast routing updates).
One router Y, commands are
iptunnel add tunY mode gre local 207.241.237.37 remote 169.229.255.134 ttl 225
ifconfig tunY 10.0.201.2/24
ifconfig tunY up
ifconfig tunY pointopoint 10.0.201.1
ifconfig tunY multicast
Tunnel X<->Y Now we created a tunnel on the 10.0.201.0/24 network from router X to Y and vice versa.
routerX ----------------tunnel-----------------routerY 
        10.0.201.1                   10.0.201.2
         (tunX)                      (tunY)
We can send packets on the 10.0.201.0/24 network from router X to Y and vice versa. So we can ping router X from Y on the tunnel interface.
routerX# ping 10.0.201.2
routerY# ping 10.0.201.1

Additional Routes

However, if we to send packets to network B or C from router X, we need to add routes so that traffic for these networks is sent on the tunnelling interface.
On router X:
route add -net 10.0.3.1/24 gw 10.0.201.1 dev tunX
route add -net 10.0.4.1/24 gw 10.0.201.1 dev tunX
Similarily, to send packets to network A from router Y, we need to add a route.
On router Y:
route add -net 10.0.2.1/24 gw 10.0.201.2 dev tunY


Delete Tunnels

On router X:
ifconfig tunX down
iptunnel del tunX

Network Diagram

 (network A)   
 10.0.2.1, eth1
    |
 ___|_________
|  Router X   |
|_____________|
    | 169.229.255.134 (eth0)
    | (Internet or network C)
    |
    |
   | |  10.0.201.1 (tunX)
   | |
   | |
   | |  (gre tunnel: 169.229.255.134 <-> 207.241.237.37)
   | |
   | |
   | |  10.0.201.2 (tunY)
    |
    | (Internet or network C)
    | 207.241.237.37 (eth0)
 ___|___________
| Router Y      |
|_______________|
  |           |
  |           |
10.0.3.1      10.0.4.1 
eth1          eth2
(network B)   (network C)


Debian Configuration

  • router X: /etc/network/interfaces
auto tun0
iface tun0 inet static
       address 10.0.201.1
       netmask 255.255.255.0
       broadcast 10.0.201.255
       up ifconfig tun0 multicast
       pre-up iptunnel add tun0 mode gre remote 207.241.237.37 local 169.229.255.134 ttl 255
       pointopoint 10.0.201.2
       post-down iptunnel del tun0
  • router Y: /etc/network/interfaces
auto tun0
iface tun0 inet static
       address 10.0.201.2
       netmask 255.255.255.0
       broadcast 10.0.201.255
       up ifconfig tun0 multicast
       pre-up iptunnel add tun0 mode gre local 207.241.237.37 remote 169.229.255.134 ttl 255
       pointopoint 10.0.201.1
       post-down iptunnel del tun0

Tuesday, November 6, 2012

php function get age from birthday

birthday format is YYYY-MM-DD

function age($birthDay) {
    list($y, $m, $d) = explode('-', $birthDay);
    if (($m = (date('m') - $m)) < 0) {
        $y++;
    } elseif ($m == 0 && date('d') - $d < 0) {
        $y++;
    }
  
    return date('Y') - $y;
}

10 Best jQuery Popup Plugins



Open Source TinyBox2 JavaScript Modal Windows


Apprise: Attractive Alert and Dialog Box for jQuery


noty


Reveal: jQuery Modals Made Easy


A Simple Lightweight Modal Windows jQuery Plugin


Fancy Javascript Popup Library with jQuery UI


PageSlide


Lightbox_me – A Really Simple Lightboxing for Programmer


Growl for Rails



15+ Useful jQuery Datepicker Roundup

jQuery Datepicker Scripts

jdPicker – the ultimate jQuery date picker

image

jdPicker is a free non obstructive jQuery plugin that enables you to simply add a folding calendar to the HTML input you want your users to select a date in.
In other words it’s a fully customizable jQuery date picker (hence the name) which already embeds a many options and will continue to progress that way.
MORE INFO / DEMO

Date Range Picker using jQuery UI 1.7 and jQuery UI CSS Framework

date-range-picker
Really cool jQuery datepicker script with date range picking features.
MORE INFO / DEMO

Calendar – date picker – date field

Calendar - date picker - date field
A field that opens a calendar where you can pick a date. The date that you picked will be entered in the inputbox. Works with jQuery and php class. It is possible to update the layout and text in the popup.
MORE INFO / DEMO by CodeCanyon (premium plugin)

jQuery Datepicker

jquery datepicker
A datepicker can easily be added as a popup to a text field or inline in a division or span with appropriate default settings. The popup shows when the field gains focus and is closed by clicking on the Close link or clicking anywhere else on the page. You can also remove the datepicker widget if it is no longer required.
MORE INFO / DEMO

jQuery DatePicker

image
Simple jQuery UI date picker extension to allow user to choose date ranges. When user chooses a date, a hidden form is submitted. Uses jQuery 1.6 and jQuery UI 1 .8. Tested in and compatible with IE 6 -9, Firefox, Safari, Chrome, and Opera.
MORE INFO / DEMO by CodeCanyon (premium plugin)

Date Picker jQuery Plugin

jquery-datepicker[3]
Date Picker component with a lot of options and easy to fit in your web application. Flat mode – as element in page, Multiple calendars in the component, Allows single, multiple or range selection, Mark dates as special, weekends, special days, Easy to customize the look by changing CSS etc.
MORE INFO / DEMO

Timeline calendar

Timeline calendar

Timeline is simple JavaScript driven calendar, which is written on top of jQuery JavaScript framework. Timeline is a horizontal representation of days in month. It can be used to display unlimited number of events with their descriptions.
MORE INFO / DEMO by CodeCanyon (premium plugin)

Zebra_Datepicker, a lightweight datepicker jQuery plugin

zebra-datepicker
Zebra_Datepicker is a small, compact and highly configurable datepicker jQuery plugin, meant to enrich forms by adding the datepicker functionality to them. This jQuery plugin will automatically add a calendar icon to the indicated input fields which, when clicked, will open the attached datepicker. Users can easily jump between months and years due to the datepicker’s intuitive interface. The selected date will be entered in the input field using the date format of choice, configurable in the datepicker’s options.
MORE INFO / DEMO

Timeframe, (JavaScript Datepicker)

timeframe
Timeframe is an open source JavaScript date picker.
MORE INFO / DEMO

JSCalendar (JavaScript)

image

JSCalendar is a Javascript class that adds accessible and unobtrusive date-pickers to your form elements. By using it, you can set up the coolest calendar in the shortest amount of time.
MORE INFO / DEMO by CodeCanyon (premium plugin)

Datepicker Control, (JavaScript Datepicker)

blueshoes-datepicker
The DatePicker lets users easily select valid dates from a dropdown calendar. No need to worry about date formats, by picking dates it’s always correct.
MORE INFO / DEMO

Multiday Calendar Datepicker JQuery Plugin

multiday

Multi-day, multi-month animated datepicker jquery Calendar plugin that weighs in at 14KB with the uncompressed development version.
MORE INFO / DEMO

jQuery.calendar/datepicker

jquery calendar picker

This component is a light-weight jquery Calendar/datepicker. Some features worth mentioning could be: supports internationalization (supports do not necessary means it is implemented), supports changing current date, supports mouse wheel scrolling, supporting (deferred) callback on date selection,supports variable number of years, months and days, supports next/prev arrows
MORE INFO / DEMO

oClock – Analog Time Picker

oClock - Analog Time Picker
oClock is a shiny analog time picker that provides user select time easily. It converts your inputs into an analog clock like date pickers does.
MORE INFO / DEMO by CodeCanyon (premium plugin)

Simple JQuery Datepicker

image

Pretty neat jquery Calendar datepicker which works on one or more text input elements and is easy styled with CSS. It is very easy to use this datepicker – just choose your year and month first and then pick some day
MORE INFO / DEMO

jQuery UI Datepicker

UI datepicker

The jQuery UI Datepicker is a highly configurable plugin that adds datepicker functionality to your pages. You can customize the date format and language, restrict the selectable date ranges and add in buttons and other navigation options easily. By default, the datepicker calendar opens in a small overlay onFocus and closes automatically onBlur or when a date is selected. For an inline calendar, simply attach the datepicker to a div or span. You can use keyboard shortcuts to drive the datepicker
MORE INFO / DEMO

jQuery Calendar datepicker plug-in

jquery date picker
This jQuery datepicker allows you to easily add “date picker” calendars to you HTML forms. These calendars make it much quicker, easier and less error prone for people to input certain types of dates.
MORE INFO / DEMO

simple jQuery datepicker plugin

image
Simple plugin that works on one or more text input elements (<input type="text" … />)

Unobtrusive Datepicker Widget Update

unobtrusive date picker
A datePicker ( jquery Calendar) that is accessible using the keyboard, requires no embedded JavaScript blocks, uses no pop-up windows and is suitable for use within documents served as application/xhtml+xml.
MORE INFO / DEMO