2015-01-07 59 views
0

我需要獲取域的SSL證書詳細信息(如失效日期,頒發者)。爲此,我從PHP文件運行以下命令。PHP - 如何從shell_exec輸出獲取參數

ssl.php

$cdates= shell_exec('openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates'); 
$issuer= shell_exec('openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -issuer'); 
echo "$cdates"; 
echo "$issuer"; 

得到以下輸出:

notBefore=Dec 10 11:54:23 2014 GMT notAfter=Mar 10 00:00:00 2015 GMT 

issuer= /C=US/O=Google Inc/CN=Google Internet Authority G2 

現在我的問題是我怎樣才能從變量參數?

例如,我需要獲得CN值(Google Internet Authority G2)從$issuer變量。

那麼是否有任何命令/函數存在或我需要使用正則表達式從字符串中提取CN

任何建議將不勝感激。

+0

*我需要使用正則表達式。*是的,你需要如。 preg_match – donald123

+0

@ donald123感謝您的確認..好吧,我會嘗試:) –

回答

1

看起來這將是(使用phpseclib)清潔方法:

<?php 
include('File/X509.php'); 

$x509 = new File_X509(); 
$cert = $x509->loadX509(shell_exec('openssl s_client -connect example.com:443 2>/dev/null')); 

print_r($x509->getIssuerDNProp('CN')); 
?> 
+0

嗯,我不假設使用任何外部庫。無論如何感謝您的答案。 :) –