Coding comments/suggestions - first python script - sshd/ftpd blocking

Discussion in 'Python' started by avinashc@yahoo.com, May 8, 2005.

  1. Guest

    If anyone is interested in a /etc/hosts.deny automatic update script
    (Unix only) based on sshd/vsftpd attacks, here's a python script:
    http://www.aczoom.com/tools/blockhosts/

    This is a beta release, and my first attempt at Python coding.
    Any comments, suggestions, pointers on using more common Python idioms
    or example coding snippets, etc, welcome!

    Thanks!
     
    , May 8, 2005
    #1
    1. Advertising

  2. Re: Coding comments/suggestions - first python script - sshd/ftpdblocking

    wrote:
    > If anyone is interested in a /etc/hosts.deny automatic update script
    > (Unix only) based on sshd/vsftpd attacks, here's a python script:
    > http://www.aczoom.com/tools/blockhosts/
    >
    > This is a beta release, and my first attempt at Python coding.
    > Any comments, suggestions, pointers on using more common Python idioms
    > or example coding snippets, etc, welcome!


    First thing: I had *many* indentation errors (emacs + python_mode on a
    linux-box). *Please* take care of this.

    I just gave a quick glance, not even pretending to really understand
    what the code do, so what follow are just some general advices:

    ***
    def die(msg, ex=None):
    print msg
    if ex: print ex
    sys.exit(1)

    - errors messages (including usage) should be written to stderr (stdout
    is for normal output)
    - you may want to use positional arguments (*args) instead of 'ex'

    def die(msg, *args):
    print >> sys.stderr, msg
    for ex in args:
    print >> sys.stderr, ex
    sys.exit(1)

    ***
    class LockFile:
    (...)
    def lock(self):
    try:
    try:
    self.fp = open(self.path, "r+")
    # r+ prevents trashing the file!
    except Exception, e :

    You should use IOError instead of Exception here.
    *Always* use the most specific exception class possible.

    if e.errno == errno.ENOENT: # no such file

    Here if you have anything else than an IOError (well, anything that
    doesn't have a 'errno' attribute), you'll get an AttributeError...
    (...)

    if DEBUG: print " ... first r+ lock file open failed, so opened
    with w+ mode"

    You may want to define a 'debug_trace' function (or use an existing
    trace/log lib) that encapsulate the test...

    ***
    class BlockHosts:
    (...)
    def load_hosts_deny(self, logoffsets):
    self.__remaining_lines = []

    if self.__verbose: print " ... hosts.deny: loading from ", self.__denyfile

    Same as for DEBUG : you may want to encapsulate the test in a method.

    HTH
    Bruno
    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno modulix, May 10, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendra Gunawan
    Replies:
    1
    Views:
    13,062
    Allan Herriman
    Apr 8, 2004
  2. Kenneth McDonald
    Replies:
    2
    Views:
    789
  3. Kenneth Xie

    any ftpd written in python?

    Kenneth Xie, Feb 16, 2006, in forum: Python
    Replies:
    4
    Views:
    403
    Magnus Lycka
    Feb 20, 2006
  4. Gilles Ganault

    Monitoring SSHd and web servers?

    Gilles Ganault, Mar 14, 2008, in forum: Python
    Replies:
    3
    Views:
    308
    Pacman
    Mar 14, 2008
  5. News123

    sshd in python for windows 7

    News123, Feb 7, 2010, in forum: Python
    Replies:
    4
    Views:
    1,096
    News123
    Feb 7, 2010
Loading...

Share This Page