Intro to Php Objects.

I wrote up this quick little banking script to illustrate how to do simple object oriented programming in php5 using classes. Functions within a class are known as methods.

-JJ

class account_transact
{
    var $balance;
    var $account_type;
    var $amount;

    function get_bal($account_type)
    {
        if($account_type=='checking')
        {
            #query for sql to look up account
            $balance=100;
        }
        elseif($account_type=='savings')
        {
            #query for sql to look up account
            $balance=25;
        }
        return $balance;
    }
    function verify_account($account_type)
    {
        if($account_type=="checking" || $account_type=="savings")
        {
            #account verified
            return $account_type;
        }
        else
        {
            echo $account_type." is not a valid account";
            exit();
        }
    }
    function deposit($account_type, $amount)
    {
        $cur_bal=$this->get_bal($account_type);
        $new_bal=$cur_bal+$amount;
        print "Balance for <b>".$account_type."</b> is <b>$".$new_bal."</b><BR>";
    }
}

//create new class instance
$transaction=new account_transact();

//checking deposit
$account=$transaction->verify_account("checking");
$transaction->deposit($account,"5");

//savings
$account=$transaction->verify_account("savings");
$transaction->deposit($account,"-10");
?>

More: http://www.php-editors.com/articles/simple_php_classes.php

Killing processes w/ 'pkill'

Today i found a better way to kill processes in linux. Use pkill. Now instead of killing items w/ the process id, you can kill items matching a certain pattern, plus lots of extras…

For example, lets say i have a bunch of stagnate processes all spawned from the same script.

root 27623 27622 0 Jun25 ? 00:00:00 /bin/sh -c python /etc/nagios2/scripts/notify_failover.py
root 27625 27623 0 Jun25 ? 00:00:00 python /etc/nagios2/scripts/notify_failover.py
root 27920 27919 0 Jun25 ? 00:00:00 /bin/sh -c python /etc/nagios2/scripts/notify_failover.py
root 27922 27920 0 Jun25 ? 00:00:00 python /etc/nagios2/scripts/notify_failover.py
root 28206 28205 0 Jun25 ? 00:00:00 /bin/sh -c python /etc/nagios2/scripts/notify_failover.py
root 28208 28206 0 Jun25 ? 00:00:00 python /etc/nagios2/scripts/notify_failover.py
root 28730 28729 0 Jun25 ? 00:00:00 /bin/sh -c python /etc/nagios2/scripts/notify_failover.py
root 28732 28730 0 Jun25 ? 00:00:00 python /etc/nagios2/scripts/notify_failover.py
root 29061 29060 0 Jun25 ? 00:00:00 /bin/sh -c python /etc/nagios2/scripts/notify_failover.py

Now, I could kill them by doing this….

kill 27623 27625, etc, etc…

However, the more efficient way to do this would be to do the following…

pkill -f “python /etc/nagios2/scripts/notify_failover.py”

Whamo! All process gone!

Record all traffice on port 80

sudo tcpdump -n -i eth0 -s 0 -w output.txt src or dst port 80

Tunnel / Encrypt web traffic

-Firefox > Preferences > Manual Proxy configuration > Socks Host: localhost Port: 9999, socksv5.

LINUX:

Next open linux terminal and setup up tunneling.

$ssh -CND 9999 user@remoterserver.com

WINDOWS:

Follow these screen shots…

*Leave terminal window open.

To verify web traffic is coming from the host go to www.whatismyip.com and the server ip should be listed from where data is being tunnelled through!

Python: Unions of lists and sets.

a=set(['apple','apple','orange','apple','pear','berry'])
b=set(['pear','coconut','strawberry'])
a | b

set([‘strawberry’, ‘coconut’, ‘apple’, ‘orange’, ‘pear’, ‘berry’])

a.union(b)

set([‘strawberry’, ‘coconut’, ‘apple’, ‘orange’, ‘pear’, ‘berry’])

Python: Reading and Writing to a Word (.doc) file

Need to install ‘antiword’ debian package then read output from CLI.

import commands
output=commands.getoutput('antiword /home/jesterj/Desktop/test.doc')
print output

Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.

Write to word…

Pretty easy here. Just write to a file and append with .doc.

f=open("newfile.doc","wr")
f.write(output)
f.close()

Badow!

Python: Reading & Writing to Excel

install ‘xlrd’ for reading…
Download: http://www.lexicon.net/sjmachin/xlrd.htm

root@donkey:~# sudo apt-cache search xlrd
python-xlrd – extract data from MS Excel spreadsheet files

import xlrd
wb=xlrd.open_workbook('/home/jesterj/Desktop/JJ-MonthlyNumbersNew-6.18.09.xls')
wb.sheet_names()


[u'Sheet1', u'Sheet2', u'Sheet3']
sh=wb.sheet_by_index(0)
for rownum in range(sh.nrows):
    print sh.row_values(rownum)

Ref: http://scienceoss.com/read-excel-files-from-python/

Install 'xlwt' for writing excel files...
Download: http://pypi.python.org/pypi/xlwt


import xlwt
from datetime import datetime

font0 = xlwt.Font()
font0.name = 'Times New Roman'
font0.colour_index = 2
font0.bold = True

style0 = xlwt.XFStyle()
style0.font = font0

style1 = xlwt.XFStyle()
style1.num_format_str = 'D-MMM-YY'

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')

ws.write(0, 0, 'Test', style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))

wb.save('example.xls')

Computing a Symmetrical Difference in sets and lists.

Symmetrical difference calculates the difference of not whats in the just one list. It calculates the items that are not common in both lists!

list1=['donkey','balls']
list2=['donkey','scrotom']
set1=set(list1)
set2=set(list2)
print set1

set([‘donkey’, ‘balls’])

print set2

set([‘donkey’, ‘scrotom’])

symdiff = set1 ^ set2
symdiff

set([‘balls’, ‘scrotom’])

Return diff in list or set.

There is no way to do a function diff on a list. However you can do a loop.

list1 = [1, 2, 3, 5, 7,  11]
list2 = [1, 2, 4, 8, 16, 32]
diff_list = []
for item in list1:
    if item not in list2:
        diff_list.append(item)

print diff_list
[3, 5, 7, 11]

Or use sets….

from sets import Set
set1=Set(['a','b','c'])
set2=Set(['d','b','c'])
diff=set1-set2
print diff
 

Set(['a'])

for x in diff:
    print x

a

Or conver list to a set…

list1=['a','b']
list2=['b','c']

set1=set(list1)
set2=set(list2)

diff=set1-set2

Python argv & getopts

def main():
cases=['auth','transact','dns','network','ssl']
print sys.argv
try:
if sys.argv[1]=='-t':
if sys.argv[2] not in cases or len(sys.argv[2])==0:
print "*Must specify a valid case: %s " % cases
exit()
else:
case=sys.argv[2]

if case=='auth':
test_authentication()
if case=='transact':
test_transaction_problem(transact_url)
if case=='dns':
test_dns(hosts)
if case=='network':
test_network(hosts)
if case=='ssl':
test_ssl(hosts)
else:
print "You must specify a valid test %s" % cases
except:
print "You must specify option -t followed by test option %s" % cases