I run a cron job (perl script) which fetches the PHP-version from servers and stores them in a database. This has always worked with CentOS PHP versions. I had to modify it a little in order to make it work with Debian. So far all is good. However it's getting the wrong version / information with Ubtuntu PHP.
This is the code snippet:
@temp = `/usr/sbin/vzctl exec $server 'php -v 2>/dev/null | /bin/grep "built\\|cgi\\|cli" 2>&1'`;
chomp(@temp);
@temp = split /\s+/,$temp[0];
$php = $temp[1];
$php =~ s/^.*(\d+\.\d+\.\d+).*$/$1/;
Below are 3 examples from different PHP-versions on different operating systems:
CentOS 7
php -v outputs: PHP 7.0.33 (cli) (built: Oct 22 2019 10:24:41) ( NTS )
Result in database: 7.0.33 (= correct)
Debian 9
php -v outputs: PHP 7.3.10-1+0~20191008.45+debian9~1.gbp365209 (cli) (built: Oct 8 2019 05:48:14) ( NTS )
Result in database: 7.3.10 (= correct)
Ubuntu 18.04
php -v outputs: PHP 7.2.19-0ubuntu0.18.04.2 (cli) (built: Aug 12 2019 19:34:28) ( NTS )
Result in database: 8.04.2 (= incorrect)
I think the current regex expression is considering the last part as the PHP version, hence it's storing 8.04.2 in the database.
How can the current code:
$php =~ s/^.*(\d+\.\d+\.\d+).*$/$1/;
Be modified so it reads the correct PHP version for all operating systems?
I checked a few older servers with dates operating systems, but they don't differ much from the above 3 examples. So I am guessing if the current code can be tweaked to also work with Ubuntu, I am set for now and the near future.
Please keep in mind that I want to be able to use the current code (as displayed above) but with a modified regex obviously.
So two questions (both also for learning):
- Is it possible to adjust the current regex to match PHP versions for all OS as shown above?
- isn't it better to make the regex search the text PHP and then grab the version somehow?
I know the code is old, but it's from our technician who unfortunately is not amongst us anymore.
Maybe someone cares to take a look and come up with a working solution based on it's current form?
Thanks!