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')) 

Mistake:

 File "C:\Users\Karen\Desktop\PocketSploit\modules\ip_address.py", line 9, in get_ip_address return results[marker:].splitlines()[0] IndexError: list index out of range 
  • Perhaps you are using Windows, which does not have such a command (host). therefore, from the syntax point, everything is correct, but at the output in the results variable there is nothing as it is equal to None - Konstantin Kozlenko

2 answers 2

Apparently, process.read() returns an empty string.

An example to reproduce the error:

 In [19]: results = str("") In [20]: marker = results.find('has address') + 12 In [21]: marker Out[21]: 11 In [22]: results[marker:].splitlines() Out[22]: [] In [23]: results[marker:].splitlines()[0] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-23-6962e431f3ce> in <module>() ----> 1 results[marker:].splitlines()[0] IndexError: list index out of range 

    when you define the "command" variable, it becomes equal to hostgoogle.com , I suspect that you need to get host google.com , try replacing command = 'host' + url with command = 'host ' + url .