Skip to content

Instantly share code, notes, and snippets.

@Cvar1984
Last active June 22, 2024 06:50
Show Gist options
  • Save Cvar1984/b2799ac2c1a5d98dc4b741682fa9faf2 to your computer and use it in GitHub Desktop.
Save Cvar1984/b2799ac2c1a5d98dc4b741682fa9faf2 to your computer and use it in GitHub Desktop.
Check if given domain list have same ip (living in the same server instance)
#!/usr/bin/env php
<?php
if ($argc < 3) {
echo sprintf('%s <domainList> <domain/ip>%s', $argv[0], PHP_EOL);
exit(1);
}
/**
* ValidateDomainOrIp
*
* @param string $domainOrIp
* @return array|false
*/
function validateDomainOrIp($domainOrIp): array|false // Returns array of IP addresses or false if invalid input
{
$ips = [];
if (filter_var($domainOrIp, FILTER_VALIDATE_IP)) {
$ips[] = $domainOrIp;
} else if (filter_var($domainOrIp, FILTER_VALIDATE_DOMAIN)) {
$ips = gethostbynamel($domainOrIp);
}
return $ips;
}
if (($inputIps = validateDomainOrIp($argv[2])) == false) {
echo sprintf('%s Is invalid or dead domain%s', $argv[2], PHP_EOL);
exit(1);
}
$domainList = file($argv[1], FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
$hosts = [];
foreach ($domainList as $domain) {
if (($ips = gethostbynamel($domain)) == false) {
continue;
}
foreach ($inputIps as $inputIp) {
if (in_array($inputIp, $ips)) {
$hosts[$domain][] = $inputIp; // for later use
echo $domain, PHP_EOL; // output domain that has the same ip
}
}
}
//print_r($hosts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment