Dipeshwar Singh
Dipeshwar Singh

Reputation: 9

Windows alternative of this python script for linux to get IP from URL

I am very new to Python. So in newboston's latest python tutorial Im making a web crawler. This Python script however only works on Linux. What would be the windows alternative to the following code?

import os

def get_ip_address(url):
  command = "host " + url     
  process = os.popen(command)
  results = str(process.read())
  marker = results.find('has address') + 12
  return results[marker:].splitlines()[0]

print(get_ip_address('google.com'))

I want it in the same format as I want to code multiple modules(i.e. get ip, who is etc.) and put them together as one Python program.

I am trying to obtain ONLY the ip address and nothing else in the results.

Alternatively, This is what I have for Windows so far:

import socket

def get_ips_for_host(url):
    try:
        ips = socket.gethostbyname_ex(url)
    except socket.gaierror:
        ips = []
    return ips

ips = get_ips_for_host('www.facebook.com')
print(repr(ips))

How can i modify it to make it get only the IP address?

Similarly how can I get the nmap scan and the 'who is'?

Upvotes: 1

Views: 76

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113988

the_ip = get_ips_for_host('www.facebook.com')[-1][0]

Upvotes: 1

Related Questions