Kurtis Bro
Kurtis Bro

Reputation: 43

I need to run some bash from PHP webpage

I have a bash script as below :

#!/bin/bash
for i in `cat domains` ; do
tag=$(echo -n $i" -   "; whois $i | grep -o "Expir.*")
reg=$(echo -n -"     "; whois $i | grep "Registrar:")
echo $tag $reg

sleep .5s
done;

I wish to have a php page where a user can paste in a list of domains and when they hit send it calls the bash script processes the domains and returns the output. Is this possible?

Upvotes: 1

Views: 106

Answers (3)

Barmar
Barmar

Reputation: 781004

Do you really need to run the bash script? Here's the equivalent PHP code:

foreach ($domains as $domain) {
  $domain = addslashes($domain);
  exec("whois '$domain'", $results);
  foreach ($results as $line) {
    if (preg_match('/Expir.*/', $line, $matches)) $tag = $matches[0];
    if (preg_match('/Registrar:/', $line)) $reg = $line;
  }
  echo $domain.' - '.$tag.' - '$reg."\n";
  usleep(500000);
}

Upvotes: 1

Travesty3
Travesty3

Reputation: 14469

It is possible, but you will need to take care when executing a command with user input. You can use exec() or backticks to execute a command on the server from PHP.

Just take care to make sure that what the user entered is actually a URL and not something meant to execute a malicious command on your server.


Example:

Your code might look something like this:

$output = array();
$urls = $_POST["urls"];
// perform necessary sanitation checks if needed
exec('/path/to/your/script '. implode(' ', $urls), $output);
echo $output;

Upvotes: 4

Rohitashv Singhal
Rohitashv Singhal

Reputation: 4557

yep you can use exec() or shell_exec() command

Upvotes: 1

Related Questions