1

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?

1
  • 1
    The preferred way to iterate over a text file is while read -r i; do ...; done < domains. See Bash FAQ 001. Commented Aug 24, 2012 at 13:29

3 Answers 3

4

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;
Sign up to request clarification or add additional context in comments.

5 Comments

I am not too fussed about security in the sense that it is a work tool, and only 4 people will have access to it, and they have root on the server anyhow!
Variable expansions are not rescanned as commands, so the script can't execute a malicious command. However, he you still quote "$i" to prevent wildcards from being expanded.
@Barmar: But he has to execute a command in PHP to call the bash script, and he will have to pass parameters to that bash file. So PHP could execute a malicious command. Right? (This is more for general knowledge, since the OP has indicated that it will be used in a controlled environment).
I was assuming he was running the above script, which doesn't take any parameters, it just reads from a file. But see my use of addslashes in my PHP version of his script.
@Barmar: Yeah, I like your solution.
1

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

2 Comments

So I need to look at getting the input to the bash script, how would I do that?
Take a look at proc_open, it allows you to provide pipes for input and output of the process.
1

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);
}

2 Comments

+1 - I like this solution better than relying on an external dependency. If that's all the bash script does, this seems like a better way to do it.
oh i missed a part of the code off** reg=$(echo -n -" "; whois $i | grep "Registrar:") should be **reg=$(echo -n -" "; whois $i | grep -A1 "Registrar:" | tr -d '\n')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.