add reverse dns lookup results for viewer's current IPs to session, show on hosts overview view (see panel at lower right)

not showing it in home view's jumbotron due to potential cosmetic issues
This commit is contained in:
Thomas Waldmann 2013-11-27 08:16:49 +01:00
parent 71509fe935
commit 6b9c7e9511
4 changed files with 22 additions and 5 deletions

View File

@ -79,7 +79,9 @@ class AjaxGetIps(View):
"""
response = dict(
ipv4=request.session.get('ipv4', ''),
ipv4_rdns=request.session.get('ipv4_rdns', ''),
ipv6=request.session.get('ipv6', ''),
ipv6_rdns=request.session.get('ipv6_rdns', ''),
)
logger.debug("ajax_get_ips response: %r" % (response, ))
return HttpResponse(json.dumps(response), content_type='application/json')

View File

@ -187,10 +187,18 @@ def rev_lookup(ipaddr):
"""
do a normal reverse DNS lookup, IP to name
note: this call may be slow, especially if there is no reverse dns entry
:param ipaddr: ip address (str)
:return: hostname
:return: hostname (or empty string if lookup failed)
"""
return socket.gethostbyaddr(ipaddr)[0]
name = ''
if ipaddr:
try:
name = socket.gethostbyaddr(ipaddr)[0]
except socket.error:
pass
return name
def parse_name(fqdn, origin=None):
@ -337,8 +345,9 @@ def put_ip_into_session(session, ipaddr, kind=None, max_age=0,
# we try to avoid modifying the session if not necessary...
if session.get(kind) != ipaddr:
# we have a new ip, remember it, with timestamp
session[kind] = ipaddr
session[kind + '_timestamp'] = int(time.time())
session[kind] = ipaddr
session[kind + '_rdns'] = rev_lookup(ipaddr) # may be slow
else:
old_timestamp = session.get(kind + '_timestamp')
if not max_age or old_timestamp is None or old_timestamp + max_age < int(time.time()):

View File

@ -61,8 +61,12 @@
</div>
<div class="well well-sm">
<h3>Your current IP(s):</h3>
<p><b>IPv4:</b> <span id="ipv4" class="ipv4addr">{{ request.session.ipv4 }}</span></p>
<p><b>IPv6:</b> <span id="ipv6" class="ipv6addr">{{ request.session.ipv6 }}</span></p>
<p><b>IPv4:</b> <span id="ipv4" class="ipv4addr">{{ request.session.ipv4 }}</span>
<span id="ipv4_rdns" class="ipv4addr">{{ request.session.ipv4_rdns }}</span>
</p>
<p><b>IPv6:</b> <span id="ipv6" class="ipv6addr">{{ request.session.ipv6 }}</span>
<span id="ipv6_rdns" class="ipv6addr">{{ request.session.ipv6_rdns }}</span>
</p>
</div>
</div>
</div>

View File

@ -155,7 +155,9 @@
$.getJSON("{% url 'ajax_get_ips' %}")
.done(function(data) {
$('#ipv4').text(data['ipv4']);
$('#ipv4_rdns').text(data['ipv4_rdns']);
$('#ipv6').text(data['ipv6']);
$('#ipv6_rdns').text(data['ipv6_rdns']);
});
}
setTimeout(insert_ips, 1500);