Jump to content
morrislaptop

Get IP address of hostname e.g. stage.newsite.com

Recommended Posts

Link doesn't work anymore. Anybody up for a fix? 

 

I don't know workflow OP posted supports http:// input as well but it'd be nice to have. Another feature that'd be nice is reverse lookup. I mean, if an ip is specified then output the hostname for that ip.

 

A friendly soul out there? Thanks a lot

Share this post


Link to post

Damn not online anymore - would really need such a tool. Probably easy to build, ha? Any hints?

 

Find a whois service that has a public API that you can use. Then, pick your language (probably PHP, Python, or Ruby because of their ability to handle the probably JSON that the API will send back), and wire up the appropriate Alfred library. Adding in some URL validation would probably help.

Share this post


Link to post

Two options:

nslookup "{query}"|grep Address:|grep -v \#|head -n 1|sed 's|Address: ||g'

or

ping -c 1 "{query}"|grep PING|sed -E 's|.*\(([0-9.]*)\).*|\1|g'

"{query}" is obviously the website to look up. The output for both commands will be the IP address.

 

Caveat: if you try this with a bad host (my test case was the word "cheese"), then you get an IP for the nslookup, but with ping you get "ping: cannot resolve cheese: Unknown host"

Share this post


Link to post

Or, you could get the awesome workflow from daenishe instead:

http://www.packal.org/workflow/resolve-url

Thanks, Andreas :)

That only works with URLs (i.e. webservers), not arbitrary hostnames.

However, the relevant Python code from that workflow is:

import socket

def dns_info(hostname):
    """Return DNS info for hostname"""
    try:
        host, aliases, ipaddrs = socket.gethostbyname_ex(hostname)
    except Exception as err:
        print('Error fetching DNS for {} : {}'.format(hostname, err))
        raise
    return {
        'hostname': host,
        'aliases': aliases,
        'ipaddrs': ipaddrs
    }
Example output:

 

>>> dns_info('www.google.com')
{u'ipaddrs': ['74.125.136.106', '74.125.136.105', '74.125.136.103', '74.125.136.147', '74.125.136.99', '74.125.136.104'], u'hostname': 'www.google.com', u'aliases': []}
>>> dns_info('www.yahoo.com')
{u'ipaddrs': ['46.228.47.114', '46.228.47.115'], u'hostname': 'fd-fp3.wg1.b.yahoo.com', u'aliases': ['www.yahoo.com']}

Edited by deanishe

Share this post


Link to post

perhaps I should include a regex that offers better feedback - `ip` is for looking up the numerical ip address of a domain. `whois` is for looking up the server name of a numerical ip address.

Share this post


Link to post

Don't worry, just ignore me.

 

Because you said "both ways" and I'm used to calling whois with a hostname, I got it into my head that the ip keyword was a reverse DNS lookup that wanted an IP address as input, not a hostname.

Share this post


Link to post

You could also just use a single script filter to route it in case the user does what Dean does and (un)intentionally breaks it. So, here's some untested Ruby code:

query = ARGV[0].downcase if ARGV.length > 0
query.strip!

if /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/.match(query)
	/usr/bin/ruby __dir__/ip_lookup.rb query
else
	/usr/bin/ruby __dir__/whois.rb query
end

Or, you could use a more complex regex that actually checks to make sure that the ip is in range:

query = ARGV[0].downcase if ARGV.length > 0
query.strip!

regex = RegEx.compile('^(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)^(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$#46;){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')

if query.match regex
	/usr/bin/ruby __dir__/ip_lookup.rb query
else
	/usr/bin/ruby __dir__/whois.rb query
end

But, you should double-check these because they're untested (and because my head hasn't been in Ruby recently). But the logic is solid: check if the user is inputting an IP address; if so, use the ip script; if not, use the whois script.

Share this post


Link to post

thanks all, I will likely implement something like that. I agree whois was a bad choice for a keyword, given that it has other connotations

Share this post


Link to post

Mainly this was made to learn how alfred workflows work, but someone seemed to want the domain name to ip address functionality so... :)

Share this post


Link to post

Does anyone know if there's any way to get all the hostnames/subdomains/domains that map to a single IP address? Or can you only get the rDNS hostname?

Share this post


Link to post

Mainly this was made to learn how alfred workflows work, but someone seemed to want the domain name to ip address functionality so... :)

 

That's how we all start. It seems that the most common development pattern, for simple workflows, is to throw everything into a single script filter, and one that does more to guess what the user wants. Having as few keywords is a good thing to do, especially when people start installing 100+ workflows (like me..., and Dean), so that there are fewer to remember. Really complex ones (think the Evernote workflow, the Spotify Workflows, ZotQuery) have multiple ones for really good reasons.

 

 

Does anyone know if there's any way to get all the hostnames/subdomains/domains that map to a single IP address? Or can you only get the rDNS hostname?

 

There is no straightforward tool to do so, but it looks like you can find something with Robtex.com. Check out this example (https://www.robtex.com/en/advisory/ip/162/243/215/240/), which is a redirect for (https://www.robtex.com/ip/162.243.215.240). There's also an API, but I haven't looked into it too much.

Share this post


Link to post

Does anyone know if there's any way to get all the hostnames/subdomains/domains that map to a single IP address? Or can you only get the rDNS hostname?

 

The Robtex thing doesn't really show subdomains. But, I guess that you could try to get more information by finding starting with Robtex and then finding and parsing the zone file for each of those domains...

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...