Thursday, April 10, 2014

HTTP High-availability Load-balancer

Wednesday, April 9, 2014

WiFi Tethering in Linux / Convert your laptop in to an access point

1. install hostapd and dnsmasq to your system

2. Configure hostapd
Sample configuration given below. Change interface, ssid and wpa_passphrase according to your requirement.
$ cat /etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
ssid=
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
ap_max_inactivity=2400

3. Create a start/stop script
This script will start, stop, restart  the required services and set the required iptables rules. Please change the IP address and interface name if required. 
$ cat /opt/ap.sh 
#!/bin/bash
function start(){
   echo "Starting hostapd"
   hostapd -B /etc/hostapd/hostapd.conf
   sleep 1
   echo "Setting 10.42.0.1 to wlan0"
   ifconfig wlan0 10.42.0.1 netmask 255.255.255.0
   echo "Starting dnsmasq..."
   dnsmasq --conf-file --no-hosts --keep-in-foreground --bind-interfaces --except-interface=lo --clear-on-reload --strict-order --listen-address=10.42.0.1 --dhcp-range=10.42.0.10,10.42.0.100,60m --dhcp-option=option:router,10.42.0.1 --dhcp-lease-max=50 --pid-file=/var/run/nm-dnsmasq-wlan0.pid&
   iptables -A FORWARD -d 10.42.0.0/24 -o wlan0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
   iptables -A FORWARD -s 10.42.0.0/24 -i wlan0 -j ACCEPT
   iptables -A FORWARD -i wlan0 -o wlan0 -j ACCEPT
   iptables -t nat -A POSTROUTING -s 10.42.0.0/24 ! -d 10.42.0.0/24 -j MASQUERADE
   echo 1 > /proc/sys/net/ipv4/ip_forward
}

function stop(){
   echo "Stopping hostapd..."
   pkill hostapd
   echo "Turn down wlan0..."
   ifconfig wlan0 down
   echo "stoping dnsmasq..."
   pkill -f dnsmasq.*10.42.0.1*
   iptables -D FORWARD -d 10.42.0.0/24 -o wlan0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
   iptables -D FORWARD -s 10.42.0.0/24 -i wlan0 -j ACCEPT
   iptables -D FORWARD -i wlan0 -o wlan0 -j ACCEPT
   iptables -t nat -D POSTROUTING -s 10.42.0.0/24 ! -d 10.42.0.0/24 -j MASQUERADE
   echo 0 > /proc/sys/net/ipv4/ip_forward
}

function status(){
   if [[ $(pgrep -f dnsmasq.*10.42.0.1*) != '' ]]
   then
      echo "dnsmasq running..."
   else
      echo "dnsmasq not running..."
   fi
   if [[ $(pgrep hostapd) != '' ]]
   then
      echo "hostapd running..."
   else
      echo "hostapd not running..."
   fi

   echo "ip_forward is set to:"$( cat /proc/sys/net/ipv4/ip_forward)
   echo "Related iptable rules:"
   sudo iptables -nL -t nat | grep 10.42;
   sudo iptables -nL | grep 10.42
}

case "$1" in
   start)
      if [[ $(pgrep -f dnsmasq.*10.42.0.1*) == '' &&  $(pgrep hostapd) == '' ]] 
      then
         start
      else
         status
      fi
      ;;
   stop)
      stop
      ;;
   restart)
      stop
      start
      ;;
   status)
      status
      ;;
   *)
      echo "Usage: $0" \
         "{start|stop|status}"
      exit 1
      ;;
esac

4. That's all. Now you can start, stop and check the status of the access point using this script.
/opt/ap.sh start
/opt/ap.sh stop
/opt/ap.sh restart
/opt/ap.sh status


Friday, January 31, 2014

Untethered Jailbreak IPhone 3GS iOS 6.1.3



  1. Backup the iPhone using iTunes
  2. Try this method. 
  3. If that didn't work, then try the following steps.
  4. Upgrade your iPhone 3GS to 6.1.3 using iTunes 
  5. Download Redsn0w 0.9.15b3 from here
  6. Download iOS 6.0.0 IPSW from here
  7. Open up Redsn0w, select Extras then Select IPSW
  8. Select the downloaded iOS 6.0.0 IPSW
  9. Now, from the main screen in Redsn0w just click Jailbreak and make sure you follow the instructions that will appear on the screen. Select install Cydia.
  10. If Cydia is not installed then go to extras hit select IPSW again and click the same 6.0 and then click just boot
  11. After booting connect to the internet through WiFi
  12. From Cydia install p0sixspwn and ultrasn0w.
  13. p0sixspwn will untether the jailbreak. 
  14. ultrasn0w will unlock the provider and allow you to use your SIM.


REF: http://www.jailbreakmodo.com/jailbreak-ios-6-1-3-using-redsn0w-tethered.html
http://www.iphonehacks.com/2014/01/jailbreak-ios-6-1-5-6-1-4-6-1-3-p0sixspwn.html

Tuesday, May 14, 2013

RSA SecureID in Android

install RSA SecureID Software Token
https://play.google.com/store/apps/details?id=com.rsa.securidapp&hl=en

You should have .sdtid file in your Linux box.
Download ftp://ftp.rsasecurity.com/pub/agents/TokenConverter261.tar.gz

./TokenConverter  -iphone
com.rsa.securid.iphone://ctf?ctfData=1034701932740192347...

change this as;
http://127.0.0.1/securid/ctf?ctfData=1034701932740192347...

Transfer this URL into your android device in a file. Open it with an text editor and copy it in the import text box of RSA SecureID Software Token.

Saturday, February 9, 2013

Clean Uninstaller for Mac OS X packages

This will automate the uninstallation process of an installed Mac OS X packages [.pkg]. It utilizes the functionalities provided by pkgutil.

#!/bin/bash
# Copyright (C) 2008 Nayanajit Mahendra Laxaman mail: nmlaxaman@gmail.com

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
LOG=/var/log/uninstall.log

#Verify the arguments
if [[ $# -ne 1 ]]
then
 echo "Usage:"
 echo "$0 "
 echo "NOTE:The PKGID should be as in 'pkgutile --pkgs' list."
 echo "Eg:"
 echo "$0 com.apple.pkg.JavaForMacOSX107"
 exit 1
fi

#Check the availability of the package
if [[ $( pkgutil --pkgs | grep "$1" ) == ''  ]]
then
 echo "Package $1 not installed!"
 exit 1
else
 echo "Following package will be uninstalled:"
 pkgutil --info $1
 sudo pkgutil --info $1 >> $LOG
 echo "The files removed will be archived at /tmp/$1.tar"
fi

#Confirm UNINSTALLTION
echo -n "Proceed with UNINSTALLTION [y/N]:"
read ans
if [[ $ans == 'y' || $ans == 'Y' ]]
then
 echo "Uninstalling $1"
else
 echo "Uninstalltion aborted!"
 exit 0;
fi

#Do it from root
cd /

#Generating the list of files
FILES=$(pkgutil --only-files --files $1 | tr '\n' ' ' )
sudo echo $FILES >> $LOG

#Generating the list of directories
DIRS=$(pkgutil --only-dirs --files $1 | tr '\n' ' ')
sudo echo $DIRS >> $LOG

#Archive  all the files
echo "Creating archive at /tmp/$1.tar ..."
tar -cf /tmp/$1.tar  $FILES

echo "Deleting files ..."
for file in $FILES
do
 if [[ $(pkgutil --file-info $file | grep pkgid | wc -l) -ne 1 ]]
 then
  echo "$file is used by another package and not removing!"
 else
  sudo rm -f $file
 fi
done

echo "Deleting dirs ..."
for dir in $DIRS
do
 if [[ $(pkgutil --file-info $dir | grep pkgid | wc -l) -ne 1 ]]
 then
  echo "$dir is used by another package and mot removing!"
 else
  sudo rm -fr $dir
 fi
done

#Forgetting the package from pkg database
echo "Forgetting the package $1..."
sudo pkgutil --forget $1

Friday, September 28, 2012

Easy read out loud in linux

1. Install espeak and xclip
2. Create the following script
vim /opt/myapps/read_out_loud.sh

#!/bin/bash
pidof espeak
if [[ $? == 0 ]]
then
   pkill -9 espeak
else
   TEXT=$( xclip -o | sed s/\"/\'/g )
   espeak -ven+f2 -s180 "$TEXT"
fi


3. Add keyboard short-cut to the script  (eg: Ctrl+Shift+r)
4. To read any text, first copy the text (eg: Select and Press Ctrl+c) and the press Ctrl+Shift+r. This will read the text in the clipboard.

Saturday, August 18, 2012

PHP array_walk any function

Built-in array_walk  function of PHP require a specially defined functions to work with. So any built-in functions or your other functions can not walk on array. Below code provide a functionality to overcome this issue and walk any function on your array. 
$my_arr=array("a"=>"apple","b"=>"banana","c"=>'cashew');

//walker function holder array
$GLOBALS['walker']="";

/**
 * walker walks a function over an array
 */
function array_walk_(&$array,$function,$var=array()){
   $GLOBALS['walker']=$function;
   array_walk($array,'walk_helper',$var);
}

/**
 * Helper function for the walker
 */
function walk_helper(&$item,$key,$var=null){
   $var[]=$item;
   $item=call_user_func_array($GLOBALS['walker'],$var);
}

array_walk_($my_arr,'strtoupper');
print_r($my_arr);

array_walk_($my_arr,'strtolower');
print_r($my_arr);            


Result of the execution:

Array ( [a] => APPLE [b] => BANANA [c] => CASHEW ) Array ( [a] => apple [b] => banana [c] => cashew )

Saturday, July 21, 2012

ZTE MF190B on OpenSUSE 12.1

Create following two files in given paths  and that will do 

1) vim /etc/udev/rules.d/40-usb_modeswitch.rules
# ZTE MF190B 19d2:0146#
#disable cdrom
ACTION=="add", SUBSYSTEMS=="scsi", DRIVERS=="sr", ATTRS{manufacturer}=="ZTE", RUN:=""
#disable handeled by network manager
ATTRS{idVendor}=="19d2", ENV{ID_MM_DEVICE_IGNORE}="1"
#change usb mode
SUBSYSTEM=="usb", ATTRS{idVendor}=="19d2", ATTRS{idProduct}=="0146", RUN+="/lib/udev/usb_modeswitch '%b/%k'"
#starting wvdial if you are using wvdial else comment this line
SUBSYSTEM=="usb", ATTRS{idVendor}=="19d2", ATTRS{idProduct}=="0143", RUN+="/usr/bin/wvdial'"

2) vim /usr/share/usb_modeswitch/19d2:0146

#ZTE MF190B 19d2:0146#

DefaultVendor = 0x19d2
DefaultProduct = 0x0146

TargetVendor = 0x19d2
TargetProduct = 0x0143
MessageContent="5553424312345678000000000000061b000000020000000000000000000000"
CheckSuccess=20

3)This step is if you are using wvdial: sample /etc/wvdial.conf (for mobitel Sri Lanka)
[Dialer Defaults]
Init1 = ATZ
Stupid Mode = 1
Password = null
Phone = *99#
Modem Type = USB Modem
Check DNS = off
Dial Command = ATDT
Modem = /dev/ttyACM0
Init = AT+CGDCONT=1,"IP","mobitel3g"
Init1 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
ISDN = 0
Username = null
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Auto Reconnect = off
Baud = 460800
Auto DNS = Off
dns1 = 8.8.8.8
Check Def Route = 1



NOTE: modify 'Phone' and 'Init' of wvdial.conf to match your providers configuration

Monday, July 16, 2012

Sunday, July 15, 2012

Ubuntu 12.04 install ffmpeg with amr support

1)Install the source package
apt-get source ffmpeg


2) install dependencies to build the package
apt-get build-dep ffmpeg


3) Install dependencies for custom options (confflags)
apt-get install libopencore-amrnb-dev libopencore-amrwb-dev

4) ffmpeg is on of the components of libav so apt-get source ffmpeg will create /usr/src/libav-0.8.3 with the source so cd to that location
cd /usr/src/libav-0.8.3

4) change confflags to enable some extra options for ffmpeg
vim debian/confflags
add following lines in '# Common configuration flags' section
confflags += --enable-libopencore-amrwb
confflags += --enable-libopencore-amrnb
confflags += --enable-version3

5) Build the package
dpkg-buildpackage -rfakeroot -uc -b

6) Install the resulting deb package(s)
cd /usr/src
dpkg -i ffmpeg_0.8.3-0ubuntu0.12.04.1_all.deb

7) Using ffmpeg to convert mp3 to amr
ffmpeg -y -vn -i in_file.mp3 -y -acodec libvo_amrwbenc -ac 1 -ar 16000 -ab 7.4k -f amr -map_metadata  -o out_file.amr

Friday, July 13, 2012

Generate wallpaper slideshow xml for gnome/Unity


#!/bin/bash

start='<background><starttime><year>2009</year><month>08</month><day>04</day><hour>00</hour><minute>00</minute><second>00</second></starttime>';
end='</background>';
static='<static><duration>1000.0</duration><file>%s</file></static>';
transition='<transition><duration>5.0</duration><from>%s</from><to>%s</to></transition>';

echo $start
echo
prev_file='';
ls | grep -i -e .jpg -e .png | while read line
do
   file=$PWD/$line;
   if [[ $prev_file == '' ]]
   then
      printf $static "$file";
      echo
   else
      printf $transition "$prev_file" "$file";
      echo
      printf $static "$file";
      echo
   fi
   prev_file=$file;
done
echo $end

1) In ubuntu precise the xml which provides the wallpaper slideshow is located here
/usr/share/backgrounds/contest/precise.xml

2) In order to create your own xml file you have to cd to the directory which contains the images and run this script and recirect the output to an xml file

3) After createing the xml file (eg: myshow.xml) backup the original file comes with precise and replace the newly created file to its place with the same name (precise.xml)

4) Go to system settings > appearance and select your slide show there

NOTE: make sure to keep your image filenames without spaces or use following script to replace spaces with underscores

#!/bin/bash

ls | grep '[a-zA-Z0-9]\ [a-zA-Z0-9]' | while read line
do
mv "$line" $( echo $line | sed s/' '/'_'/g )
done

Put wirless in to ad-hoc shell script

#!/bin/bash
rfkill unblock 0
rfkill unblock 1
ifconfig wlan0 down
sleep 1
iwconfig wlan0 essid
iwconfig wlan0 key s:
iwconfig wlan0 mode ad-hoc
ifconfig wlan0 up
ifconfig wlan0 10.16.90.12 netmask 255.255.240.0
route add default ppp0
sleep 1
iwconfig wlan0 essid

NOTE: place your essid and password in the script

Keyboard shortcukt to Send to back send to front a application window

1) Create this script as /opt/terminator_quake.sh
 
#!/bin/bash
TM_STATE=/tmp/tm_stat
WIN=$( wmctrl -lGx | grep -i terminator | awk '{print $1}' );
if [[ $WIN == "" ]]
then
   exit 0
   #terminator&
else
   if [[ -e $TM_STATE ]]
   then
      wmctrl -i -r $WIN -b remove,below
      wmctrl -i -r $WIN -b add,above
      rm $TM_STATE
   else
      wmctrl -i -r $WIN -b remove,above
      wmctrl -i -r $WIN -b add,below
      touch $TM_STATE
   fi
fi

2) I'm using this in unity on ubunutu (ie. compiz) so open ccsm (compiz config setting manager)
3) Click 'commands' button in 'general' section
4) In 'commands' tab add /opt/terminator_quake.sh to 'command line 0'
5) Click 'key binding' tab and add shortcut to the 'Run command 0' entry.

Thats it now you can send to front and sent to back the window (eg: terminator in this case). This can be use as alternative to yakuake, guake and tilda.

Check real bandwidth usage in mobitel sri lanka

#!/bin/bash

if $(gconftool-2 -g /system/http_proxy/use_http_proxy)
then
   export http_proxy="http://$(gconftool-2 -g /system/http_proxy/host):3128"
else
   unset http_proxy
   unset https_proxy
fi

ping -c 1 www.google.com 2>/dev/null >/dev/null
if [[ $? != 0 ]]
then
   zenity --error --text="Error connecting to the server!"
   exit 0;
fi

USER=
PASSWORD=

ACC="username=$USER&password=$PASSWORD"
DATA="select=ing"
COOKEY="/tmp/cookey.$USER"
OUTPUT="/tmp/curl.out"
MYACC="http://selfcare.mobitel.lk/MyAccount"
LOGIN="$MYACC/SubmitLogin.jsp"
DATA_USAGE="$MYACC/DataUsage.jsp"

#echo "curl --head --cookie $COOKEY --cookie-jar $COOKEY --user-agent Mozilla/4.0 --data '$ACC&$DATA' $LOGIN 2>/dev/null >/dev/null"
curl --cookie $COOKEY --cookie-jar $COOKEY --user-agent Mozilla/4.0 --data "$ACC&$DATA" $LOGIN 2>/dev/null >/dev/null
curl --cookie $COOKEY --cookie-jar $COOKEY --user-agent Mozilla/4.0 --data "$ACC&$DATA" $DATA_USAGE -o $OUTPUT 2>/dev/null >/dev/null

UTILIZED="Utilized: $( cat $OUTPUT | grep Utilized -A1 | tail -1 | awk -F'>' '{print $2}' | awk -F'<' '{print $1}' )"
REMAINING="Remaining: $( cat $OUTPUT | grep Remaining -A1 | tail -1 | awk -F'>' '{print $2}' | awk -F'<' '{print $1}' )"
EXCESS="Excess: $( cat $OUTPUT | grep Excess -A1 | tail -1 | awk -F'>' '{print $2}' | awk -F'<' '{print $1}' )"

if [[ $1 == gui ]]
then
   zenity --title "3g Usage in Mb" --info --text=" $UTILIZED \n $REMAINING \n $EXCESS"
else
   echo  $UTILIZED
   echo  $REMAINING
   echo  $EXCESS
fi

Thursday, June 7, 2012

libreoffice macro basic full name in to name with initials format


1) Open macro organizer
Tools > Macros > Organize Macros > Libreoffice basic...


2) Select and edit Main located at
My Macros > Standard > Module1 > Main


3) Add following function to the end of the file
Function FullnameInitialed(fullname)
Dim out as String
Dim arr() As String
arr = Split(fullname, " ")
For lngPosition = LBound(arr) To UBound(arr)
word = arr(lngPosition)
If UBound(arr) = lngPosition Then
out = out & " " & word
Else
    out = out & " " & LEFT(word,1)
End If        
Next lngPosition
FullnameInitialed=out
End Function 


4) Save and close the macro editor and get back to spreadsheet and now you can use this function as any other function (like SUM function)


eg:
=FULLNAMEINITIALED("string here")
=FULLNAMEINITIALED(D22)