Friday, September 14, 2012

Linux/Unix commands and utils


I will be putting all the simple unix helpful commands and utils over here. Starting with only couple of commands. Will be updating this on regular basis.
  • Calculating frequency of data in UNIX
    • Calculating frequency of some data in a plain text file. For example if the file has data in each line like as in this file. Then program should print frequency of numbers as output as given in this file.
    • awk '{x[$1] += 1}END{ for (entry in x) {print entry "-" x[entry]} }' 1.txt
    • Note that this can be applied to applied to any arbitrary pipe output also by appropriately select the data column number ($1 or $2 etc.)
  • Get range of lines from a file or output
    • The below command prints 2nd to 5th line of 1.txt file
      • awk 'NR>=2 && NR<=5 {print $1}' 1.txt
  • To know your IP from unix command line
    •  wget -qO- icanhazip.com

Saturday, September 1, 2012

Configure WPA wireless network interface in ubuntu through command line

We will go through steps to configure wireless network interface having WPA security through command line in ubuntu


  • Ensure that wpa_supplicant package is installed. If not try to get it by running
    •   sudo apt-get install wpasupplicant
  • Open your /etc/networking/interfaces (with sudo) file and add below two lines
    auto wlan0
    iface wlan0 inet dhcp
  • reboot the system (Dont worry if the startup says all network are not started. We will set it up)
  • Now type "wpa_supplicant -h" to get list of driver names. Observe the section "drivers:" in the output. Watch out for driver text like "Linux Wireless extension". Generally it is wext
  • Now we will create a configuration file for wpa. Create one in /etc/wpa_supplicant.conf with below content

    ctrl_interface=/var/run/wpa_supplicant
    eapol_version=1
    ap_scan=1
    fast_reauth=1
  • Now we will generate the text required for the WPA authentication. Type below command where NetworkSSID is your wireless network name
    • wpa_passphrase "NetworkSSID"
  • Enter your WPA passphrase when it prompts
  • Now it will generate a ouput similar to below

    network={
    ssid="NetworkSSID"
    #psk="YOUR_PSK"
    psk=lklksdjflwejroi209429083lkdfjlskdfjslkdfjlk
    }
  • Copy the above text to the end of /etc/wpa_supplicant.conf and also add scan_ssid, proto & key_mgmt keys as below (#psk can be removed as it's just a comment)
    • network={
      ssid="NetworkSSID"
      scan_ssid=1
      prot=WPAi
      key_mgmt=WPA-PSK
      psk=lklksdjflwejroi209429083lkdfjlskdfjslkdfjlk
      }
  • Now type below command to test all the configurations done till now. In below command wlan0 is the  interface name we defined in /etc/networking/interfaces and wext is your wireless driver name
    •   sudo wpa_supplicant -iwlan0 -c/etc/wpa_supplicant.conf -Dwext
  • You should see below text in output
    •  WPA: Key negotiation completed with 
  • The above output confirms that all steps are correct till now
  • Now we just need to tell ubuntu to use this wpa_supplicant configuration for this particular wireless device. For that, add two lines in /etc/networking/interfaces immediately after (iface wlan0 inet dhc) line which we added in the beginning. So it would look like (combined)
    • auto wlan0
      iface wlan0 inet dhcp
      wpa-driver wext
      wpa-conf /etc/wpa_supplicant.conf
  • Reboot the system

That's it. Now you should be able to access wifi from your ubuntu machine