2014年12月29日 星期一

[Ubuntu] Recommended package for arp-scan

arp-scan is a good tool to perform arp-scanning which can

  1. Do troubleshooting for IP confliction
  2. IP<>MAC mapping
  3. Get production manufacturer
Just install with: sudo apt-get install arp-scan

Common cmd used:
  • Specify a list of IP addresses as arguments: sudo arp-scan -I eth0 192.168.1.1 192.168.1.2 192.168.1.3
  • Specify network/bits: sudo arp-scan -I eth0 192.168.1.0/24
  • Specify network:netmask: sudo arp-scan -I eth0 192.168.1.0:255.255.255.0
  • Specify address range: sudo arp-scan -I eth0 192.168.1.3-192.168.1.27
  • Read a list of IP addresses from a file: sudo arp-scan -I eth0 --file=ip-address-list.txt

Openwrt - Modifying the repositories

Sometimes the Openwrt's repository is not always correct, please modify using the following.


  1. SSH to the AP and navigate to /etc/opkg.conf
  2. Modify the URL to the one you want
    src/gz barrier_breaker http://downloads.openwrt.org/snapshots/trunk/ar71xx/packages/packages/  >>You can change this URL to whatever you want
  3. Update the opkg list
    opkg update
  4. Install whatever package you want using opkg install.

2014年12月27日 星期六

[Django] Human readable text in admin panel

In Django, when we are dealing with the admin panel, we can specify human readable text for the column through different methods


  1. Make it when building the corresponding model
    e.g.1: question_text = models.CharField('question', max_length=200)
    e.g.2: pub_date = models.DateTimeField('date published')

  2. Django makes the first parameter as the field show in admin UI, in this case "Date published" and "Question" will be shown.

  3. Or, you can make it through the admin.py, Django's build-in admin model

    short_description 
  4. e.g.: was_published_recently.short_description = 'Published recently?'

    The corresponding column will be shown with readable text

  5. Or you let Django do the rest of the job using class option 'list_display'

    list_display = ('first_name', 'last_name')

    Django will set column name as First name and  Last name where they are both 'database fields', underscores will be evaluated as spaces

[Python] printf in Python

We usually make use of the following syntax for printf

print ("I am a %s %s", %('good', 'boy'))

[Python] Basic data types

There are several data structures in Python, the following are some key notes for reference


  1. List
    • Example: a=[1, 2, 3, 4, 5]
    • Mutable (support modification of objects like a.append(6)
    • Support stack operations like "pop", "append"
    • looping (1 expression only) squares = [x**2 for x in range(10)]
    • looping using traditional for
      for x in [1,2,3]:
          x = x**2
  2. Tuple
    • Example: tup = (1, 2, 3, 'wahaha') (Always enclosed with parentheses!)
    • Immutable -> tup[0] = 100 -> Not allowed!!
    • Can contain lists
      tup = (['a', 'b'], [1, 2])
    • Tuple with single element should add trailing comma
      tupSingle = (a,)
  3. Sets
    • Example: a = set([1, 2, 3])
    • No element duplicate
    • Example: 1 in a , Output: True (Fast testing)
    • Mutable
  4. Dictionary
    • Pretty similar to associative arrays in PHP
    • {} -> Empty dict
    • Mutable
    • Example: cord = {'x' : 123.4, 'y' : 278.5}
    • Get keys -> cord.keys() -> return list ['x', 'y']
    • Fast Testing -> 'in'

[Python] __main__ and __name__

A module in python (usually in form of .py) will be executed by Python interpreter, we normally face the following situations when running python code.


  1. Running the .py directly, like python manage.py
  2. Imported from another module, like from admin import manage.py

So how do python know whether the module is directly run as a main program or it is just a part of another module? The special variable __name__ will do the job!


Take a look at this piece of code

if __name__ == '__main__':
    print 'I am run by itself'
else:
    print 'I am imported as module'

So if the .py is being run as a main program, the __name__ will have value __main__ being set, otherwise, the __name__ 's value will become the module name itself. We can therefore deal with different situations depending on the program's executing behaviour.

2014年12月23日 星期二

[jQuery] Find selected option with "this"

Usually, when we want to get the selected option's text using jQuery, we will use the following method.

var selVal = $("selObj#selID option:selected").val()

What if the target object is "this"? We will instead use the following

var selVal = $("option:selected", this).prop("value");

2014年12月22日 星期一

[requireJS] Simple Dependecies

In requirejs, we can make use of the following codes to control the order of how js can be loaded.

var require = {
    shim : {
        "bootstrap" : { "deps" :['jquery'] }
    },
    paths: {
        "jquery" : "//code.jquery.com/jquery-2.1.1.min",
        "bootstrap" :  "//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min"
    }
};

paths: Self-define js path as variables
shim: Define dependencies, in this case bootstrap REQUIRES jQuery

Remember, after defining this require variable, we still need to define/require the js files or the variable's definition is useless.

Reference:http://getfishtank.ca/blog/how-to-use-bootstrap-3-with-requirejs

2014年12月18日 星期四

[jQuery] About .data() and $.data()

$.data >> Old school method, data may not be retrieved successfully after assigning data to the DOM.

Use el.data() instead to ensure greatest compatibility

2014年12月15日 星期一

Customize unity sidebar using shell command in Ubuntu

Environment
OS: Ubuntu 12.04.5 (32 bit edition)


Steps
1) Navigate to /usr/share/applications, it shows all the *.desktop files

**Make a .desktop file with the following format if you want to add your custom scripts / softwares to sidebar**

[Desktop Entry]
Type=Application
Terminal=true
Name=unmount-mount
Icon=/path/to/icon/icon.svg
Exec=/path/to/file/mount-unmount.sh

2) Add/remove the designated desktop files to reflect the unity sidebar

3) Run setsid unity to refresh the UI for sidebar changes