2011-08-20 40 views
1

有沒有程序將Url的列表轉換爲ip列表? 例如:轉換Url列表爲IP?

site1.com ip1 
site2.com ip2 
site3.com ip3 
+0

URL或只是域名? –

回答

1

在PHP中,你可以叫dns_get_record獲得與給定主機相關的DNS記錄。請注意,每個域都與零個或多個IPv4以及零個或多個IPv6地址相關聯,因此您可能想要返回一組地址。然而,這裏的

$domains = array('example.net', 'google.com', 'ipv6.google.com', 'example.404'); 
$ips = array_map(function($domain) { 
    $records = dns_get_record($domain, DNS_AAAA); 
    if (count($records) == 0) { // No IPv6 addresses, try IPv4 
     $records = dns_get_record($domain, DNS_A); 
     if (count($records) == 0) return false; // No mapping found 
     return $records[0]['ip']; 
    } else { 
     return $records[0]['ipv6']; 
    } 
}, $domains); 

var_export(array_combine($domains, $ips)); 

的輸出將是這樣的:

array (
    'example.net' => '2001:500:88:200::10', 
    'google.com' => '209.85.148.105', 
    'ipv6.google.com' => '2a00:1450:4001:c01::93', 
    'example.404' => false, 
) 
+0

我感謝你們所有人的幫助,但我在看代碼,如果我需要從hosts.txt文件獲取域名,代碼將如何? <?php $ hosts = Array( 'www.google.com', 'www.stackoverflow.com', 'kera.name' ); $ ips = Array();如果(($ ip = gethostbynamel($ host))!= FALSE) foreach($ hosts as $ host){ $ ips [] = $ ip; else $ ips [] = Array(); } print_r($ ips); ?> – Amenda

+0

@Amenda對不起,我不太關注 - 代碼是Tomalaks的答案和我的無關。如果你想讓'/ etc/hosts'影響你的結果,必須手動解析它或者使用[gethostbynamel](http://php.net/manual/en/function.gethostbyname.php)來代替' dns_get_records'(它只會返回IPv4地址)。在寫作時,php還沒有'getnameinfo'函數。 – phihag

1

使用PHP函數gethostbyname - 獲取對應於給定的Internet主機名的IPv4地址**

<?php 
$ip = gethostbyname('www.google.com'); 

echo $ip; 
?> 

你可能會看到這樣的事情:

$domains = "site1.com site2.com site3.com"; 
foreach(explode(" ", $domains) as $domain) 
{ 
    echo $domain ." ".gethostbyname($domain); 
} 

UPDATE:

如果域中有多個IP(例如:google.com),可以使用gethostbynamel - 獲取的IPv4地址的列表對應於給定的互聯網主機名。

<?php 
$hosts = gethostbynamel('www.example.com'); 
print_r($hosts); 
?> 


Array 
(
    [0] => 74.125.225.19 
    [1] => 74.125.225.16 
    [2] => 74.125.225.18 
    [3] => 74.125.225.20 
    [4] => 74.125.225.17 
) 
0

的搜索在互聯網上不損害任何人,通過谷歌在php.net找到搜索「從URL PHP IP」一點點:

gethostbyname()

+0

我的意思是如果我可以從名爲hosts.txt的文件獲取主機,然後打印只是行中的IP? – Amenda

+0

檢查我的新答案:) – Abbas

0

開始通過提取剛從你的網址上,然後用gethostbynamel如果你不關心的IPv6(TSK):

<?php 
$hosts = Array(
    'www.google.com', 
    'www.stackoverflow.com', 
    'kera.name' 
); 

$ips = Array(); 
foreach ($hosts as $host) { 
    if (($ip = gethostbynamel($host)) != FALSE) 
     $ips[] = $ip; 
    else 
     $ips[] = Array(); 
} 

print_r($ips); 
?> 

注意,有可能每個主機多個IP地址。

ideone和鍵盤都沒有設置DNS解析,因此我可以爲此提供一個實時演示。

0
$file = fopen("hosts.txt","r") or exit("Unable to open file"); 

while(!feof($file)) 
{ 
    $host = fgets($file); 
    echo "IP: " . gethostbyname($host) . "<br />"; 
} 

fclose($file); 
+0

感謝阿巴斯,但是當我測試時,它會打印除hosts.txt的最後一個url之外的所有網址(域名)它是一個ip – Amenda

+0

然後在調用函數gethostbyname()之前檢查該記錄。如果記錄是主機名,則使用該功能,否則僅打印該記錄,因爲它已經是IP。 :) – Abbas

+0

^__ ^非常感謝阿巴斯,如果你發佈解決我的問題,因爲我沒有理解你請! IP:xxxx.com IP:xxxx.org IP:xxxx.com IP:site.com IP:site1.com IP:63.247.140.166 – Amenda

0

您可以使用php函數gethostbyname將主機名解析爲IPv4地址。

例子:

$hosts = file("hosts.txt"); 
$fp = fopen("hostsip.txt", "w"); 

foreach ($hosts as $host) { 
    fwrite($fp, str_pad(trim($host), 20) . gethostbyname(trim($host)) . "\r\n"); 
} 

etc...
+0

或實際上是主機名的一部分的網址。 – tripleee

0

如果你需要的僅僅是處理文件,併產生像

array('site1.com' => 'ip1', 'site2.com' => 'ip2', 'site3.com' => 'ip3'); 

一個數組,那麼這段代碼應該工作(對不起,不能檢查現在,但這應該工作):

$data = file_get_contents('hosts.txt'); 
$lines = explode("\n", $data); 
$result = array(); 

foreach ($lines as $line) { 
    $pieces = explode(' ', trim($line)); 
    if (!empty($pieces[0]) && !empty($pieces[1])) { 
     $result[trim($pieces[0])] = trim($pieces[1]); 
    } 
} 

因此,在$結果你將有數組與urls作爲鍵和ips作爲 價值。

編輯:用PHP編寫的代碼

+0

謝謝itsmee,但它會很難,如果我這樣做,因爲我有約57個網址,我想ips只是打印在分離線就像這個網站http://freelabs.info/UrlToIpOnline.aspx – Amenda