10th Kyu
Strikes: Seiken Morote-Tsuki Jodan/Chudan/Gedan, Seiken Oi-Tsuki j-c-g
Blocks: Seiken Jodan Uke, Mae-Gedan-Baria Kicks: Hiza ganmen geri & Kin geri
Renraku 1: Jodan Tsuki-Jodan uke-kin geri-jodan uke
Renraku 2: Chudan tsuki, Mae gedan barai, hiza ganmen geri, mae gedan barai
Kata: Taikyoku sono ichi Exercises: 10 push-ups & 20 sit-ups
Read the rest of this entry »
I wanted the system to send me a mail every month the updates it has for the packages, i tried using subprocess.Popen, thought to redirect the output to stdin and write it to a file, but it was terrible…something which i could do easily in bash
apt-get -V upgrade > upgrade.log
#!/usr/bin/env python
import subprocess
import smtplib
from datetime import datetime
from datetime import timedelta
import os
import apt
from apt.package import Version
cache=apt.Cache()
cache.update()
cache.upgrade()
for pkg in cache.get_changes():
print pkg.name, pkg.summary
fileHandle = open(‘/tmp/upgrade.log’, ‘a’)
fileHandle.write(pkg.name + ” – ” + pkg.summary + “\n”)
now = datetime.now()
t = timedelta(7)
v = now + t
datestr = str(v.date())
SERVER = “localhost”
FROM = “server@server.localhost”
TO = ["alpha@krisindigitalage.com"] # must be a list
body = open(“/tmp/upgrade.log”)
TEXT = body.read()
SUBJECT = “Hello!”
CUSTOM = “dear admin\n \t\tThe following upgrades are available for your server \n\n”
# Prepare actual message
END = “\n\nThe updates will be scheduled on ” + datestr + ” . Let me know by 5pm on that date if you wish to make any changes \n\n Kind regards\n Your Server”
TEXT1 = CUSTOM + TEXT + END
message = “”"\
From: %s
To: %s
Subject: %s
%s
“”" % (FROM, “, “.join(TO), SUBJECT, TEXT1)
# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()
os.remove(‘/tmp/upgrade.log’)

#!/usr/bin/env python
import sys
def generate_log_report(logfile):
for line in logfile:
line_split = line.split()
list = [line_split[0], line_split[1], line_split[2], line_split[4]]
if “named” in list[3]:
l = [line_split[0], line_split[1], line_split[2], line_split[4]]
print l
else:
li = [line_split[0], line_split[1], line_split[2], line_split[4]]
# return lif __name__ == “__main__”:
if not len(sys.argv) > 1:
print __doc__
sys.exit(1)
infile_name = sys.argv[1]
try:
infile = open(infile_name, “r”)
except IOError:
print “you must specify a valid file”
print __doc__
sys.exit(1)
log_report = generate_log_report(infile)
print log_report
infile.close()

generating graphs using cairo was relatively very easy, i ran this on a debian box. First you need to install “bazaar”
root@server:/usr/src/python# apt-get install bzryou might need python-cairo and other dependencies..
root@server:/usr/src/python# apt-get install python-cairo python-cairo-dev python-cairo-dbg python-pycha python-gasp python-dockyRead the rest of this entry »
An example on how to use python’s subprocess.Popen function:
#!/usr/bin/env pythonimport subprocess
import os
#import pdb; pdb.set_trace()def find():
y = raw_input(“Enter a text you want to search on the system!”)
s = y.rstrip(“\n”)
print “string”, s
# command = “find /bin -name ” + str(y)
# arg = ['/usr/bin/find', '/bin', '-name', s '-print']
# print “command”, command
find = subprocess.Popen([r"/usr/bin/find", "/", "-name", y, "-print"], stdout=subprocess.PIPE)
#find_stdout = find.communicate()[0]
#print (find_stdout)
for line in find.stdout.readlines():
print “line”, line
l = line.rstrip(“\n”)
list = subprocess.Popen([r"ls", "-l", l], stdout=subprocess.PIPE)
list_stdout = list.communicate()[0]
print list_stdout
file = subprocess.Popen([r"file", l], stdout=subprocess.PIPE)
file_stdout = file.communicate()[0]
print file_stdout
def main():
find()main()

#!/usr/bin/env python
from subprocess import Popen, PIPE, STDOUT
import subprocess
import os
import popen2def memory():
print “gathering memory info…”
subprocess.call(“free”, shell=True)def cpu():
print “gathering system uptime info… “
subprocess.call(“uptime”, shell=True)def pci():
print “gathering pci devices..”
subprocess.call(“lspci”, shell=True)def apache():
print “calculating apache mem use..”
sum = 0.0
command = os.popen(“ps aux | grep apache | awk ‘{print $4}’”)
for i in command.readlines():
l = i.rstrip(“\n”)
d = float(l)
sum = d + sum
print sumdef exim():
print “calculating exim mem use…”
esum = 0.0
ecommand = os.popen(“ps aux | grep -v grep | grep exim | awk ‘{print $4}’”)
for e in ecommand.readlines():
f = e.rstrip(“\n”)
g = float(f)
esum = g + esum
print esum
def spam():
print “calculating spamd mem use..”
ssum = 0.0
spamd = os.popen(“ps aux | grep spamd | awk ‘{print $4}’”)
for s in spamd.readlines():
t = s.rstrip(“\n”)
u = float(t)
ssum = u + ssum
print ssumdef log():
print “logged in users…”
subprocess.call(“last| grep ‘still’”, shell=True)def main():
memory()
cpu()
# pci()
apache()
spam()
exim()
log()
main()
Listening to Martin Archer on KISS 100 live on KISS KUBE mobile. Check out http://www.totalkiss.com