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

« back to all changes in this revision

Viewing changes to gozerplugs/plugs/dns.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# plugs/dns.py
2
 
#
3
 
4
 
 
5
 
__copyright__ = 'this file is in the public domain'
6
 
 
7
 
from gozerbot.commands import cmnds
8
 
from gozerbot.examples import examples 
9
 
from gozerbot.plughelp import plughelp
10
 
import copy
11
 
import re
12
 
import socket
13
 
 
14
 
plughelp.add('dns', 'do ip or host lookup')
15
 
 
16
 
_re_hexip = re.compile('^[\da-f]{8}$', re.I)
17
 
 
18
 
def handle_hostname(bot, ievent):
19
 
    """ hostname <ipnr> .. get hostname of ip number"""
20
 
    try:
21
 
        item = ievent.args[0]
22
 
    except IndexError:
23
 
        ievent.missing('<ipnr>')
24
 
        return
25
 
    try:
26
 
        hostname = socket.gethostbyaddr(item)
27
 
        ievent.reply(hostname[0])
28
 
    except:
29
 
        ievent.reply("can't match " + str(item))
30
 
 
31
 
cmnds.add('host', handle_hostname, 'USER')
32
 
examples.add('host', 'get hostname for ip', 'host 194.109.129.219')
33
 
 
34
 
def handle_ip(bot, ievent):
35
 
    """ ip <hostname> .. get ip of hostname """
36
 
    try:
37
 
        item = ievent.args[0]
38
 
    except IndexError:
39
 
        ievent.missing('<hostname>')
40
 
        return
41
 
    try:
42
 
        ipnr = socket.gethostbyname(item)
43
 
        ievent.reply(ipnr)
44
 
    except:
45
 
        ievent.reply("can't match " + str(item))
46
 
 
47
 
cmnds.add('ip', handle_ip, 'USER')
48
 
examples.add('ip', 'show ip number of host', 'ip gozerbot.org')
49
 
 
50
 
def handle_dns(bot, ievent):
51
 
    """ <host|ip> performs a DNS lookup an ip or hostname. """
52
 
    if not ievent.args:
53
 
        ievent.missing('<host | ip>')
54
 
    else:
55
 
        is_a   = None
56
 
        result = None
57
 
        # If we support IPv6 ...
58
 
        if socket.has_ipv6:
59
 
            # ... then check if this is an IPv6 ip
60
 
            try:
61
 
                socket.inet_pton(socket.AF_INET6, ievent.args[0])
62
 
                is_a = 'ipv6'
63
 
            except socket.error:
64
 
                pass
65
 
        # Ah not an IPv6 ip ...
66
 
        if not is_a:
67
 
            # ... maybe IPv4 ?
68
 
            try:
69
 
                socket.inet_pton(socket.AF_INET, ievent.args[0])
70
 
                is_a = 'ipv4'
71
 
            except socket.error:
72
 
                pass
73
 
        # Not an ip, must be a hostname then
74
 
        if not is_a:
75
 
            is_a = 'host'
76
 
        # If it was an ip ...
77
 
        if is_a in ['ipv4', 'ipv6']:
78
 
            try:
79
 
                # ... try to resolve it
80
 
                result = socket.gethostbyaddr(ievent.args[0])
81
 
                if result[1]:
82
 
                    result = 'primary: %s, aliases: %s' % \
83
 
                        (result[0], ', '.join(result[1]))
84
 
                else:
85
 
                    result = result[0]
86
 
                ievent.reply('%s ip %s resolves to %s' % \
87
 
                    (is_a, ievent.args[0], result))
88
 
            except Exception, e:
89
 
                ievent.reply('could not resolve %s address %s: %s' % \
90
 
                    (is_a, ievent.args[0], e[1]))
91
 
        # Oh it's a host, lets resolve that
92
 
        elif is_a == 'host':
93
 
            try:
94
 
                result = []
95
 
                for info in socket.getaddrinfo(ievent.args[0], None):
96
 
                    if info[0] in [socket.AF_INET, socket.AF_INET6] and \
97
 
                        info[1] == socket.SOCK_STREAM:
98
 
                        ip = info[4][0]
99
 
                        if not ip in result:
100
 
                            result.append(ip)
101
 
                if not result:
102
 
                    ievent.reply('could not resolve hostname %s: not found' % \
103
 
ievent.args[0])
104
 
                else:
105
 
                    ievent.reply('%s resolves to: %s' % (ievent.args[0], \
106
 
', '.join(result)))
107
 
            except Exception, e:
108
 
                ievent.reply('could not resolve hostname %s: %s' % \
109
 
                    (ievent.args[0], e[1]))
110
 
        else:
111
 
            ievent.reply('lookup failed, no valid data found')
112
 
 
113
 
cmnds.add('dns', handle_dns, 'USER')
114
 
examples.add('dns', 'look up an ip or hostname', 'dns gozerbot.org')
115
 
 
116
 
def handle_hexip(bot, ievent):
117
 
    """ <ip|hex ip> returns the reverse of the given argument. """
118
 
    if not ievent.args:
119
 
        return ievent.missing('<ip | hex ip>')
120
 
    is_a = None
121
 
    if _re_hexip.match(ievent.args[0]):
122
 
        is_a = 'hexip'
123
 
    else:
124
 
        try:
125
 
            socket.inet_pton(socket.AF_INET, ievent.args[0])
126
 
            is_a = 'defip'
127
 
        except socket.error:
128
 
            pass
129
 
    if not is_a:
130
 
        ievent.missing('<ip | hex ip>')
131
 
        return
132
 
    if is_a == 'hexip':
133
 
        ip = []
134
 
        for i in range(4):
135
 
            ip.append(str(int(ievent.args[0][i*2:i*2+2], 16)))
136
 
        ip = '.'.join(ip)
137
 
        nevent = copy.copy(ievent)
138
 
        nevent.args = [ip]
139
 
        handle_dns(bot, nevent)
140
 
    else:
141
 
        test = ievent.args[0].split('.')
142
 
        ip = 16777216 * int(test[0]) + 65536 * int(test[1]) + 256 * \
143
 
int(test[2]) + int(test[3])
144
 
        ievent.reply('ip %s = %08x' % (ievent.args[0], ip))
145
 
 
146
 
cmnds.add('hexip', handle_hexip, 'USER')
147
 
examples.add('hexip', 'return the hex ip notation of an ip, or vice versa', \
148
 
'hexip 7F000001')