2013-12-16 57 views
2

以下是下面列出的程序。我試圖從PHP運行shell命令,所以我寫了下面的代碼:如何在php中傳遞參數來執行shell命令?

<?php  
$argument1 = $argv[1];  
$output = shell_exec('sudo whois ');  
echo "<pre>$output</pre>";  
?> 

但每次我執行它得到執行,但不顯示輸出的命令。只顯示選項。

我在shell命令是 php filename.php google.com

+1

'sudo'-ing命令來自php,無法在密碼中抨擊? –

回答

2

你是不是在你的命令中使用傳遞的參數。您需要使用的論點whois命令:

<?php  
$argument1 = escapeshellarg($argv[1]); 
$output = shell_exec('whois '. $argument1);  
echo "<pre>$output</pre>";  
?> 

PS:whois沒有sudo也可以囤地。

+4

不要忘記shell-escape輸入:-) –

+0

@EliasVanOotegem:謝謝我將它添加到代碼中。 – anubhava

+0

感謝@anubhava的幫助,以及如果我想從網絡調用這些而不是從命令行調用... – user3101586