[PLUG] Question

Ross Brattain ross at principia.edu
Thu Apr 25 05:53:34 UTC 2002


Chris Emery <deathfox at moochercrew.org> writes:

> Sorry, have to chip in with the python way to do it, tad longer script,
> but Eh, I'm not as good as I would like to be, requires python2.0  Also
> assumes you want to use stdin (I can change it for python1.5 if you want
> *grins*
> 
> #!/usr/bin/env python2
> import sys
> 
> mdict = {}
> stdin = sys.stdin.readlines()
> 
> for line in stdin:
>     llist = line.split(None)
>     if llist[0] in mdict.keys():
>         if llist[1] in mdict[llist[0]].keys():
>             mdict[llist[0]][llist[1]] += 1
>         else :
>             mdict[llist[0]][llist[1]] = 1
>     else :
>         mdict[llist[0]] = {llist[1]: 1}
>         
> for a in mdict.keys():
>     sys.stdout.write(a + "\n")
>     for b in mdict[a].keys():
>         sys.stdout.write("".ljust(5) + b.ljust(30) + str(mdict[a][b]) +
> "\n")
> 
> This also handles if usera logs on from two different IPs, and record
> each under user a, and the attempts from each IP.
> 

A more tricky version:

#!/usr/bin/env python2
import sys

mdict = {}

while 1:
    line = sys.stdin.readline().strip()
    if not line: break
    llist = line.split()
    mdict.setdefault(llist[0], {llist[1]: 0}).setdefault(llist[1], 0)
    mdict[llist[0]][llist[1]] += 1
    
for (user, hosts) in mdict.items():
    for (host, count) in hosts.items():
        sys.stdout.write('\t'.join((user, host, "failed logins=%d" % count)) + "\n")


Mapping object methods are fun.

-ross






More information about the PLUG mailing list