从python内部执行的命令'host'获取IP

时间:2018-11-12 04:14:04

标签: python arrays linux host

我有此功能向我显示域的第一个IP:

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

但这只会显示我的第一个IP。我只想显示IP。标记用于不具有“具有地址”,如下所示(假设我输入“ reddit.com”:

['151.101.65.140', 'reddit.com has address 151.101.129.140', 'reddit.com has address 151.101.193.140', 'reddit.com has address 151.101.1.140', 'reddit.com mail is handled by 1 aspmx.l.google.com.', 'reddit.com mail is handled by 10 aspmx2.googlemail.com.', 'reddit.com mail is handled by 10 aspmx3.googlemail.com.', 'reddit.com mail is handled by 5 alt1.aspmx.l.google.com.', 'reddit.com mail is handled by 5 alt2.aspmx.l.google.com.']

我只想显示IP,而不想reddit.com has address,也不要显示mail is handled等。

我尝试过

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

        print results[marker:].splitlines()[i]
        arrayIps.append(results[marker:].splitlines()[i])
        print("array")
        print arrayIps[i]
        i=i+1
    return arrayIps

但是它不起作用!甚至没有返回任何有用的东西!

我期望的是一个数组(在这种情况下):

'151.101.65.140', '151.101.129.140', '151.101.193.140', '151.101.1.140'

3 个答案:

答案 0 :(得分:2)

看到它根据需要显示了多个主机。您可以使用map函数生成输出

In [132]: socket.getaddrinfo("reddit.com", 80, proto=socket.IPPROTO_TCP)
Out[132]: 
[(<AddressFamily.AF_INET: 2>,
  <SocketKind.SOCK_STREAM: 1>,
  6,
  '',
  ('151.101.65.140', 80)),
 (<AddressFamily.AF_INET: 2>,
  <SocketKind.SOCK_STREAM: 1>,
  6,
  '',
  ('151.101.1.140', 80)),
 (<AddressFamily.AF_INET: 2>,
  <SocketKind.SOCK_STREAM: 1>,
  6,
  '',
  ('151.101.129.140', 80)),
 (<AddressFamily.AF_INET: 2>,
  <SocketKind.SOCK_STREAM: 1>,
  6,
  '',
  ('151.101.193.140', 80))]


In [134]: list(map(lambda x:x[4][0],socket.getaddrinfo("reddit.com", 80, proto=socket.IPPROTO_TCP)))
Out[134]: ['151.101.129.140', '151.101.193.140', '151.101.65.140', '151.101.1.140']

答案 1 :(得分:0)

您应该尝试使用dig命令而不是host并同时应用适当的正则表达式:

from subprocess import run, PIPE

def get_answer(url):
    response = run(["dig", "+noall", "+answer", "+short","{url}".format(url=url)], stdout=PIPE, universal_newlines=True)
    return response.stdout

注释:

编辑

如评论中所述,您可以使用dig中的+ short选项除去正则表达式部分。

答案 2 :(得分:-1)

尝试使用.split()在其空格处拆分字符串IP,然后取其最后一项[-1],然后在句点{{1处,拆分最后一项(应为IP地址)” }}。使用.split('.')连接从其中返回的字符串,其中可迭代的是''.join(iterable)的值。使用.split('.')检查连接的字符串是否为数字值,如果该值为True,则打印并返回最后一项。

.isnumeric()

坏消息是我似乎无法将def get_ip_address(url): command="host "+url process=os.popen(command) results=str(process.read()) marker=results.find('has address')+12 ip_list=[] for ip in results[marker:].splitlines()[0]: if ''.join(ip.split()[-1].split('.')).isnumeric(): ip_list.append(ip.split()[-1]) print ip_list return ip_list 设置为任何值。它目前仅返回一个空字符串,但希望您会遇到更多的运气:D

(编辑:如您的评论所述,results=str(process.read())适用于色彩鲜艳。不确定在Python 2.7上是否适用,但在3.7.1中适用于此)

(Edit2:这是您在3.7.1中的代码。应该在2.7中类似)

socket.getaddrinfo()