1

I am quite new to PHP, but I am building myself a tool, to avoid checking domain information using multiple sites, and have everything by clicking once.

The issue that I have, is that I am trying to run whois command, which works, if I specify the IP address (writing the IP manually within the code), as when I try to insert a variable instead of the IP (what customer inputs via website is domain name, I get the IP (A record) from the domain, and try to use it with the whois command) grep seems to not work anymore.

My idea is to grep "descr" so to see, to which company the IP belongs to.

Examples when it works:

$Host = shell_exec("whois 141.136.44.163 | grep 'descr'");
echo "<pre>$Host</pre>"

Output:

descr: Hostinger International Ltd. descr: HOSTINGER LT

As when I try to use a variable, which I get from the domain name with this command line:

$outputA = shell_exec("dig +short a $Domain");

And I use it for the whois:

$Host = shell_exec("whois $outputA | grep 'descr'");

The answer I receive from whois is the whole informarion, like you would run a command without grep: (whois IP:address)

It seems that grep isn't working anymore due to that. And I get something like this:enter image description here

Any idea how to fix this, or what could be causing this issue? Once I echo the whole whois command line with $outputA I receive a normal command line, which for example if I write to terminal, works without any issues (the IP from the $outputA is received, and grep is working via terminal).

For reference, my whole code:

$Domain = $_POST['DomainName'];
echo "DNS records for domain:", $Domain;
$outputNS = shell_exec("dig +short ns $Domain");
$outputA = shell_exec("dig +short a $Domain");
$outputMX = shell_exec("dig +short mx $Domain");
$outputTXT = shell_exec("dig +short txt $Domain");
echo "<pre>$outputNS</pre>";
echo "<pre>$outputA</pre>";
echo "<pre>$outputMX</pre>";
echo "<pre>$outputTXT</pre>";
$Registrar = shell_exec("whois $Domain | grep 'Registrar'");
$Host = shell_exec("whois $outputA | grep 'descr'");
echo "Original domain's Registrar:";
echo "<pre>$Registrar<pre>";
echo "<pre>$Host<pre>";
Hoolis
  • 47
  • 6

1 Answers1

0

You can try sprintf for this:

<?php
$outputA = '141.136.44.163';
$command = sprintf("whois %s | grep 'descr'", $outputA );
$Host = shell_exec($command);
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • It works this way that you have suggested, although now the issue seems to me more clear. The IP address which I get from "dig" seems to be the issue. If I input the IP address manually as you have written, it works, as when I use this: $outputA = shell_exec("dig +short a $Domain"); It seems to not get recognized or something, the IP seems to be passed correctly, but whois command does not grep anymore after using the dig IP :/ The most interesting this, is that the output of the dig IP and manually written IP while using the echo is identical 141.136.44.163 141.136.44.163 – Hoolis Jan 01 '22 at 15:03
  • Still quite confused and not sure, why the digged IP is reacting differently rather than manually written IP address. – Hoolis Jan 01 '22 at 15:12
  • @Hoolis are you try already exec() instead of shell_exed()? – Maik Lowrey Jan 01 '22 at 16:03
  • not sure what you exactly mean, as a normal exec() isn't working with whois command lines, and it requires to be exectuted via shell_exec() as a terminal command line. – Hoolis Jan 01 '22 at 18:32