Jump to content

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


Recommended Posts

  • 1 month later...
  • 5 months later...

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

Link to comment
  • 7 months later...

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.

Link to comment

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"

Link to comment

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
Link to comment

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.

Link to comment

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.

Link to comment

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...

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...