~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gplugs/banner.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerplugs/banner.py
 
2
#
 
3
# Description: check is host:port is open, and supply the first line or 'banner'
 
4
# Author: Wijnand 'tehmaze' Modderman
 
5
# Licsense: BSD
 
6
 
 
7
""" show banner of <host> <port> """
 
8
 
 
9
__author__ = "Wijnand 'tehmaze' Modderman"
 
10
__license__ = "BSD"
 
11
 
 
12
from gozerbot.commands import cmnds
 
13
from gozerbot.plughelp import plughelp
 
14
from gozerbot.examples import examples
 
15
from gozerbot.tests import tests
 
16
import socket
 
17
 
 
18
plughelp.add('banner', 'show if host:port is open')
 
19
 
 
20
def handle_banner(bot, ievent):
 
21
    """ banner <host> <port> .. check if host:port is open """
 
22
    try:
 
23
        (host, port) = ievent.args
 
24
        port = int(port)
 
25
    except ValueError:
 
26
        ievent.missing('<host> <port>')
 
27
        return
 
28
    banner = None
 
29
    try:
 
30
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
31
        sock.settimeout(2)
 
32
        sock.connect((host, port))
 
33
    except socket.timeout:
 
34
        ievent.reply('%s:%s is not up' % (host, port))
 
35
        return
 
36
    except Exception, ex:
 
37
        ievent.reply('%s:%s is not up ==>  %s' % (host, port, str(ex)))
 
38
        return
 
39
    try:
 
40
        msock = sock.makefile()
 
41
        banner = msock.readline()
 
42
    except:
 
43
        pass
 
44
    try:
 
45
        sock.close()
 
46
    except socket.error:
 
47
        pass
 
48
    if banner:
 
49
        ievent.reply('%s:%s is up: %s' % (host, port, banner))
 
50
    else:
 
51
        ievent.reply('%s:%s is up' % (host, port))
 
52
 
 
53
cmnds.add('banner', handle_banner, 'USER')
 
54
examples.add('banner', 'banner <host> <port> // show if host:port is open', \
 
55
'banner gozerbot.org 25')
 
56
tests.add('banner gozerbot.org 25', 'up')