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 “.$account_type.” is $”.$new_bal.”
“;
}
}
//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
PHP
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!
Linux
sudo tcpdump -n -i eth0 -s 0 -w output.txt src or dst port 80
Networking, Uncategorized
-Firefox > Preferences > Manual Proxy configuration > Socks Host: localhost Port: 9999, socksv5.

-Next open linux terminal and setup up tunneling.
$ssh -CND 9999 user@remoterserver.com
*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!
Uncategorized
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'])
Uncategorized
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!
Uncategorized
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')
Uncategorized
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'])
Uncategorized
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
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
Uncategorized
dd if=/dev/zero of=file_to-create bs=1k count=4000000
*the above will create a 4gb file called ‘file_to-create”
Uncategorized
Using urllib…
#!/usr/bin/python
import urllib
c=urllib.urlopen('http://nike.com')
print c.getcode()
200
or try with httplib…
#!/usr/bin/python
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1.status
200
Networking, Uncategorized
#!/usr/bin/python
items=['klm','klm','pia','nwa','klm','al','al','al','ri','pia','klm']
#get unique items from list
get_unique=[]
for a in items:
if a not in get_unique:
get_unique.append(a)
#dict for storage of key,occurence
dict={}
for x in items:
for y in get_unique:
if x in y:
if x in dict:
val=dict[x]
val+=1
dict[x]=val
else:
dict[x]=1
print dict
</code>
Uncategorized