5
__copyright__ = 'this file is in the public domain'
7
from gozerbot.commands import cmnds
8
from gozerbot.examples import examples
9
from gozerbot.plughelp import plughelp
14
plughelp.add('dns', 'do ip or host lookup')
16
_re_hexip = re.compile('^[\da-f]{8}$', re.I)
18
def handle_hostname(bot, ievent):
19
""" hostname <ipnr> .. get hostname of ip number"""
23
ievent.missing('<ipnr>')
26
hostname = socket.gethostbyaddr(item)
27
ievent.reply(hostname[0])
29
ievent.reply("can't match " + str(item))
31
cmnds.add('host', handle_hostname, 'USER')
32
examples.add('host', 'get hostname for ip', 'host 194.109.129.219')
34
def handle_ip(bot, ievent):
35
""" ip <hostname> .. get ip of hostname """
39
ievent.missing('<hostname>')
42
ipnr = socket.gethostbyname(item)
45
ievent.reply("can't match " + str(item))
47
cmnds.add('ip', handle_ip, 'USER')
48
examples.add('ip', 'show ip number of host', 'ip gozerbot.org')
50
def handle_dns(bot, ievent):
51
""" <host|ip> performs a DNS lookup an ip or hostname. """
53
ievent.missing('<host | ip>')
57
# If we support IPv6 ...
59
# ... then check if this is an IPv6 ip
61
socket.inet_pton(socket.AF_INET6, ievent.args[0])
65
# Ah not an IPv6 ip ...
69
socket.inet_pton(socket.AF_INET, ievent.args[0])
73
# Not an ip, must be a hostname then
77
if is_a in ['ipv4', 'ipv6']:
79
# ... try to resolve it
80
result = socket.gethostbyaddr(ievent.args[0])
82
result = 'primary: %s, aliases: %s' % \
83
(result[0], ', '.join(result[1]))
86
ievent.reply('%s ip %s resolves to %s' % \
87
(is_a, ievent.args[0], result))
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
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:
102
ievent.reply('could not resolve hostname %s: not found' % \
105
ievent.reply('%s resolves to: %s' % (ievent.args[0], \
108
ievent.reply('could not resolve hostname %s: %s' % \
109
(ievent.args[0], e[1]))
111
ievent.reply('lookup failed, no valid data found')
113
cmnds.add('dns', handle_dns, 'USER')
114
examples.add('dns', 'look up an ip or hostname', 'dns gozerbot.org')
116
def handle_hexip(bot, ievent):
117
""" <ip|hex ip> returns the reverse of the given argument. """
119
return ievent.missing('<ip | hex ip>')
121
if _re_hexip.match(ievent.args[0]):
125
socket.inet_pton(socket.AF_INET, ievent.args[0])
130
ievent.missing('<ip | hex ip>')
135
ip.append(str(int(ievent.args[0][i*2:i*2+2], 16)))
137
nevent = copy.copy(ievent)
139
handle_dns(bot, nevent)
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))
146
cmnds.add('hexip', handle_hexip, 'USER')
147
examples.add('hexip', 'return the hex ip notation of an ip, or vice versa', \