Thursday, November 27, 2008

Removing Duplicated File Automatically

It's annoying to have two or more of the same file laying around in your hard drive. Out of curiosity, I devised a small script to automate the process of finding and deleting duplicated file in two different directories. This is it.

#!/bin/bash
#
# This script checks whether there is a file duplication
# in two different directrories and deletes the one in
# ${SRC_DIR} if it found the same file
#

SRC_DIR="/home/darmawan/download"
DST_DIR="/home/sources"
TMP_FILENAMES="__filenames.txt"
CUR_FILE=""
DST_FILE=""
RESULT=""

find ${SRC_DIR} -type f > ${TMP_FILENAMES}

while read LINE
do
unset RESULT
unset CUR_FILE

CUR_FILE=$(basename "${LINE}")
#find ${DST_DIR} -type f -name "${CUR_FILE}" -exec diff -q "${LINE}" '{}' ';'
RESULT=$(find ${DST_DIR} -type f -name "${CUR_FILE}" -print)

if [ "${RESULT}" != "" ] ; then
echo "File of the same name found at ${LINE} and ${RESULT}" ;
echo "Diffing.."
diff -q ${LINE} ${RESULT}

# If the file differ (diff return value _is_not_0_ ),
# then print it out, otherwise delete the file in ${SRC_DIR}
if [ $? -eq 0 ]; then
rm -vf ${LINE}
fi
fi

done < ${TMP_FILENAMES}

unset LINE
unset FILENAME
unset CUR_FILE
unset DST_FILE
unset RESULT

rm -v ${TMP_FILENAMES}

This is a very rough script. Don't expect robustness out of it.

Thursday, November 20, 2008

Koneksi IM2 Broom di Linux

Hari ini gw nyobain Indosat Broom untuk pertama kali. Gw pikir awalnya bakal sedikit susah buat aktivasi, ternyata cuma sedikit lebih kompleks daripada bikin account email gratisan.

Konfigurasi sistem:
1. Linux Slamd64 12.1
2. Wvdial 1.60
3. AMD Turion64
4. RAM 1GB
5. Nokia E61.

Step garis besarnya:

a. Insert USIM dari starter-pack broom ke handphone/modem yang akan digunakan.

b. Dial ke situs registrasi broom menggunakan username dan password universal buat registrasi. Ini bisa dilakukan langsung. Jadi, pada dasarnya begitu dapat USIM (a.k.a SIM CARD) langsung bisa terhubung ke network registrasi Indosat M2.

c. Isi form registrasi dengan benar, kemudian di submit. Ntar bakal muncul jawaban aktivasi telah selesai.

d. Konfigurasi ulang username dan password sesuai dengan form registrasi.

e. Silahkan browsing, ftp, blogging, rss-feed, etc. sesuai kemauan anda ke semua destinasi.

Ok. Sekarang detailnya.

Step a: Ga perlu diilustrasikan coz sangat gampang.

Step b: Berhubung gw pake wvdial + Linux Slamd64, ini dia wvdial.conf yang gw pake

[Dialer im2_reg]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+IFC=2,2;+CVHU=1
Init4 = ATS7=60+DS=3,0;&K3
Init5 = AT+CGDCONT=1,"IP","indosatm2"
Init6 = ATS0=0
Phone = *99#
Username = indosatm2
Password = prabayar
Modem Type = USB Modem
Baud = 115200
New PPPD = yes
Dial Command = ATDT
Modem = /dev/rfcomm0
ISDN = 0

Pake wvdial.conf ini gw dial ke situs registrasi im2

root@opunaga # wvdial im2_reg


Step c: Buat registrasi, pake browser gw buka http://www.indosatm2.com/aktivasi. Ini situs registrasinya, trus isi form registrasi. Di form ini ada bagian untuk ngisi username + password yang akan kita pake browsing dan tipe servis Broom yang mau kita pake. Gw milih yg unlimited. Lumayan 100rb sebulan unlimited.

Step d: Tinggal modifikasi username ama password di wvdial.conf yang ada di step c, selesai deh.

Step e: Pake browser kalo pengen browsing, etc.

Sampai di sini. Tinggal dipake aja koneksinya.

Btw, di daerah Mega Kuningan lumayan kenceng koneksinya.

Thursday, November 6, 2008

Combined find, xargs and chown Trick

Sometimes, you want to change the ownership of files (including symlinks) in directories recursively. However, "malformed" directory and file names can become huge stumbling blocks. This is how to do that correctly:

root@copy_cabana# find . -user old_user -group old_group -print0 | xargs -0 chown -v -h new_user:new_group

A few notes:


  • The -print0 parameter tells find to append the "null" character after each matching filename instead of the usual "newline" character. This is handy to "fight" against malformed filename and directory name that contain white spaces, punctuation, etc.


  • The -0 parameter tells xargs that the delimiter in its input is not "newline" but a "null" character.


  • The -h parameter tells chmod to change the ownership of the symlink instead of the file/directory the symlink refers to.




That's it. Now you're prepared for weird filenames.

Tuesday, October 7, 2008

Automated Login on Time-based Public Wireless Access Point

Public wireless access point sometimes employ a time-based logout-login/connect-disconnect cycles to "attract" users into their paid service counterpart in which the time-based logout-login/connect-disconnect cycles removed. Of course that kind of connect-disconnect cycles are annoying. However, with a little bit of bash script. The login-logout chores can be automated. I'll present an example script. You will need a machine that runs *nix and has curl installed. Mind you that in this scheme, it is assumed that the access-point recognize your WLAN card based on it's MAC address. The MAC address will be used as your user name used for login purposes. This is it.


#!/bin/bash
#
# Script to login and renew connection to FreeHotspot@somewhere
#
# Note: This script connects the INTERFACE to the access point and
# renew the connection automatically.
# Press CTRL+C to stop the script
#

INTERFACE=wlan2
ESSID="FreeHotSpot@somewhere"
SESSION_LENGTH=20m
WLAN_KEY=off
LOGOUT_URL="http://192.168.10.254/logout"
LOGIN_URL="http://192.168.10.254/login?username=T-00%3AAA%3CCC%3FFF%3EEE"

if [ $UID -ne 0 ]; then
echo "This script needs root privilege to run!"
exit 1;
fi

while [ 1 ];
do
# It seems that killing dhcpcd is the most reliable way to do now.
# Otherwise reconnecting is always problematic.
# Just removing dhcpcd PID file is not enough.
#
echo "Killing dhcpcd.."
killall dhcpcd
rm -vf /etc/dhcpc/dhcpcd-${INTERFACE}.pid

echo "Bringing the interface down.."
ifconfig ${INTERFACE} down

echo "Configuring ${INTERFACE}.."
iwconfig ${INTERFACE} essid ${ESSID}
iwconfig ${INTERFACE} key ${WLAN_KEY}

echo "Bringing the interface up.."
ifconfig ${INTERFACE} up

echo "Activating dhcpcd on ${INTERFACE}.."
dhcpcd ${INTERFACE}

if [ -e /etc/dhcpc/dhcpcd-${INTERFACE}.pid ]; then
#
# Always logout first to ensure that there is no "stall"
# session which will reject your subsequent DNS requests
#
echo "IP address obtained. Logging out with curl.."
curl ${LOGOUT_URL} > /dev/null
echo "Logging in with curl.."
curl ${LOGIN_URL} > /dev/null
sleep ${SESSION_LENGTH};
else
echo "Unable to obtain dhcp address. Please run the script again. Exiting.. "
exit 1
fi
done


The script above must be executed by someone with root privilege. The script above also assumes that to logout from the current session, you have to visit some special URL. The LOGOUT_URL variable contains this URL. You have to modify it to suit your need. You can obtain this URL from your browser by examining the HTML code of the page that shows the logout button. This page usually displayed right after you do a "manual" login in your browser, for example when you are using Firefox or Opera to login and use the access point to connect to the net. Moreover, the script also assumes that you have to visit some special URL to login into a new session. The LOGIN_URL variable contains this URL. You have to modify it to suit your need as well. The IP address in the LOGIN_URL and LOGOUT_URL seems to be owned by the access point. I'm not really sure about it, but in most cases it is. One thing that I want to note that you have to modify the SESSION_LENGTH to suit the logout interval to suit the public access point that you use. Its value must be less than the session expiration time in the public access point that you use. For example, if the session expires in 30 minutes in your particular public access point, then use 29m (or less) as the value of the SESSION_LENGTH variable. Note that 29m corresponds to 29 minutes, because this value is passed to the sleep bash built-in function.

There you have it, automated login script for "smooth" surfing on public access point. As a bonus, let's see how to do automated download as well. In this case, we assume that the download process will resume after some interval. Nonetheless the script is not bug free. You have to stop it downloading things when you think it's finished because it will keep trying downloading even if the download already completed. To stop the script, simply press CTRL+C. Here it is:


#!/bin/bash

# get the file
#
#

INTERVAL=15m
SRC="http://download.mystuff.com/source_code.zip"
DEST="source_code.zip"

# Install a trap (interrupt handler) to handle Ctrl+C
trap 'killall wget; exit 0' 2

while [ 1 ]; do
# run wget in the background
(wget -c ${SRC} -O ${DEST} &> /dev/null) &

# wait for few minutes
sleep ${INTERVAL}

# restart the download
# this is a rather brute force approach to stop current download
# TODO: fix it!

killall wget

done

exit 0


Remember to stop the script by using CTRL+C. It's advised to run wget and then terminate wget previous to running this script in order to know the size of the file that you are going to download. With that information you will know when you should stop the script.

That's it for the moment. Ciao