Decode HTML Entities using Javascript

Tags: February 23, 2014 1:13 PM
0 comments

I came into situation where I need to pass some string to Pen Editor instance. The problem is the string is already encoded to HTML entities by PHP's htmlentities(). So, when I have value like This is <strong>strong</strong> element. The Pen editor instance convert it into:

This is <strong>strong</strong> element

Instead of this:

This is strong element

The Solution

The solution is pretty dead simple just inject the string into textarea element and call the value property to get the content instead of innerHTML.

function decodeHtml(html){
  var txt = document.createElement("textarea");
  txt.innerHTML = html;
  return txt.value;
}
If your string already came from textarea like in my case then you don't event need to create a single function.

References

Share on Facebook Twitter

How to Make Text Align Bottom using CSS

Tags: February 21, 2014 5:44 PM
0 comments

What I want to achieve is like picture below.

 

Getting The Job Done

The simple trick to make it works like expected is we need to treat the element as table element. Set display element to table-cell which has vertical-align, the property which what we want to align the text to the bottom. The example below makes H2 element align to the bottom.
h2.bottom {
   display: table-cell;
   vertical-align: bottom;
   height: 60px; /* it's better to make it static */
   line-height: 30px; 
   width: 400px;  /* only as example */
}

Share on Facebook Twitter

Sending On/Off Signal to USB devices in Linux

Tags: February 15, 2014 1:14 PM
0 comments

Some devices like USB modem get stuck when it failed to connect and just hang up forever when trying to dial a number. The solution is unplug and plug it again, weird but it works. But seriously, that is not "a geek way". Rather than plug/unplug we could do it via shell to send the device "on" or "off" signal.

Recognizing the USB Devices

These steps is used to get information about device that we want to send the signal to. A Tool used for this task is lsusb. Here's example of output in my system, make to sure run as root.
$ lsusb
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 04f2:b290 Chicony Electronics Co., Ltd 
Bus 001 Device 009: ID 0cf3:3005 Atheros Communications, Inc. AR3011 Bluetooth
Bus 002 Device 018: ID 201e:1022 <-- WE WANT TO SEND SIGNAL TO THIS DEVICE
Bus 002 Device 016: ID 03f0:ae07 Hewlett-Packard 
Bus 002 Device 012: ID 0458:00ee KYE Systems Corp. (Mouse Systems)

Share on Facebook Twitter

Faking Services using Netcat (For Testing Nagios)

Tags: February 3, 2014 4:41 AM
0 comments

Simulating the health of services can be pain if we should install and turn them on/off the real services such HTTP Server, SSH Server, FTP, etc one-by-one on each VM. In this experiment I just faking 2 services which are: HTTP and SSH. Based on this simple topology we would end up with:

Services which Monitored

  • VM 1
    • HTTP
    • PING
  • VM 2
    • HTTP (Simulate the Always 404 Error)
    • PING
  • VM 3
    • HTTP
    • PING
    • SSH

Share on Facebook Twitter

Connecting Two Subnet inside VMs in Linux

Tags: February 2, 2014 2:51 PM
0 comments

This should be applicable to real world (physical devices) not just virtualized environment. The topology is simple:

                 +----------------------------+
                 |          The Host          |
                 |          --------          |
                 |           Router           |
                 +----------------------------+
                       |               |
                    +-----+         +-----+
        192.168.1.1 | br0 |         | br1 | 192.168.1.9
                    +-----+         +-----+
                       |               |
  Mask: 192.168.1.0/29 |               | Mask: 192.168.1.8/29
        +--------------+               +----------+
        |              |                          |
        |              |                          |
 +--------------+      |                   +---------------+
 |     VM 1     |      +----+              |    VM 3       |
 | 192.168.1.2  |           |              | 192.168.1.10  |
 +--------------+           |              +---------------+
                    +--------------+
                    |     VM 2     |
                    | 192.168.1.3  |
                    +--------------+

The Details

The goal is to connecting VM in different subnet so they can talk to each other. Btw, I'm using Virtualbox for this playground. Here's the breakdown from the schema above:
  • The Host had two interface br0 and br1 (Both are virtual)
  • The schema using two subnet 192.168.1.0/29 and 192.168.1.8/29
  • The Host had two bridge interfaces and three TAP interfaces (for each VMs)
  • tap0 for VM 1, tap1 for VM 2 and tap2 for VM 3
  • tap0 and tap1 attached to br0 and tap2 attached to br1

Share on Facebook Twitter

Insert iptables Rule to Specified Position

Tags: 11:36 AM
0 comments

Suppose I already have rules for iptables like this:

$ iptables -L FORWARD -n -v --line-numbers
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      228 13794 ACCEPT     all  --  vmbr0  ppp0    192.168.1.10         0.0.0.0/0           
2      337 28308 DROP       all  --  vmbr0  ppp0    0.0.0.0/0            0.0.0.0/0
I want to insert my new rule between them (number 1 and 2). Here's how to do it:
$ iptables -I FORWARD 2 -i vmbr0 -o ppp0 -s 192.168.1.11 -j ACCEPT
iptables insertion rules sounds something like "Insert it before number {X}". So in my case I need to put it before number 2 (-I FORWARD 2). Take a look to the result.
$ iptables -L FORWARD -n -v --line-numbers
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      228 13794 ACCEPT     all  --  vmbr0  ppp0    192.168.1.10         0.0.0.0/0           
2        0     0 ACCEPT     all  --  vmbr0  ppp0    192.168.1.11         0.0.0.0/0           
3      342 28728 DROP       all  --  vmbr0  ppp0    0.0.0.0/0            0.0.0.0/0

Share on Facebook Twitter

How to Create Bridge Networking in Linux

Tags: February 1, 2014 1:47 PM
0 comments

Two user space tools used for creating bridge networking in linux are tunctl and brctl. tunctl used for simulating the network or link layer device and brctl for configuring them.

Create the TAP interface

Issue command below to create tap interface and then activate the tap.
tunctl -t vtap01 -u myuser
ip link set up dev vtap01
# or
ifconfig vtap01 up

Create the Bridge

Issue command below to configure the bridge for using the TAP interface which just created.
brctl addbr vbr01
brctl addif vbr01 vtap01

Assign IP and Routing to the Bridge

Make the bridge identified by others such as another computers or VMs (Assuming the network is 192.168.1.0/30).
ip link set up dev vbr01
ip addr add 192.168.1.1/30 dev vbr01
ip route add 192.168.1.0/30 dev vbr01
or it could be written as:
ifconfig vbr01 up
ifconfig vbr01 192.168.1.1 netmask 255.255.255.252
route add -net 192.168.1.0 netmask 255.255.255.252 dev vbr01
The host at 192.168.1.2 should be able to ping host 192.168.1.1 (via bridge).

References

Share on Facebook Twitter