Entries from August 2007 ↓

Another script.

This script will get list all processes affliated to a given user. Better than ps and can specify processes per user :)

#!/usr/bin/python
#adminscript.py
import pwd
from subprocess import *
import pwd
a = Popen(["ps", "axo", "euid"], stdout=PIPE, stderr=PIPE)
output = a.communicate()
uid_list = output[0]
error_list = output[1]
for uid in uid_list.split('n')[1:-1]:
    uid1 = int(uid)
    user_list = pwd.getpwuid(uid1)[0]
    print user_list
    full_name_list = pwd.getpwuid(uid1)[4]
    for user in user_list:
        b = "ps"
        c = "au" + user
        process = Popen([b, c], stdout=PIPE, stderr=PIPE)
        output2 = process.communicate()
        list_of_processes = output2[0]
        list_of_errors = output2[1]
        print user + list_of_processes

The download script I said I would add.

Here is the 1st script which pertains to downloads. It will download all pdf files from a given url. I wrote this for a reason. Have a look at the huge amount of repetition. I was talking to this guy on IRC who told me the importance of neatly written code. I will put up the better looking version sometime later.

#!/usr/bin/python
#newproj.py
import urllib
from urlparse import urlparse
from BeautifulSoup import BeautifulSoup
url = raw_input("URL you want to download things from: ")
html = urllib.urlopen(url)
print repr(html)
a = BeautifulSoup(html)

for link in a.findAll('a'):
    if url.endswith('.html') == True and urlparse(link['href']).hostname:
       b = link['href']
       print b
       if b.endswith('.pdf') == True:
           c=urlparse(b).path.split('/')[-1]
           urllib.urlretrieve(b,c)
    elif url.endswith('.html') == True and not urlparse(link['href']).hostname:
        b = '/'.join(url.rsplit('/', 1), link['href'])
        print b
        if link['href'].endswith('.pdf') == True:
            c=urlparse(link['href']).path.split('/')[-1]
            urllib.urlretrieve(b)
    elif url.endswith('.html') == False and not urlparse(link['href']).hostname:
        b = url + "/" + link['href']
        print b
        if b.endswith('.pdf') == True:
            c=urlparse(link['href']).path.split('/')[-1]
            urllib.urlretrieve(b, c)
print " I have downloaded all available pdfs."