2010-04-12 87 views
2

讓我們想象一下,我們有一個簡單的PHP腳本,應該從$ _GET數組獲得ssh_host,ssh_username,ssh_port並嘗試使用此參數連接到SSH。安全執行shell腳本;在執行前逃脫變量

$port  = escapeshellcmd($_GET['ssh_port']); 
$host  = escapeshellcmd($_GET['ssh_host']); 
$username = escapeshellcmd($_GET['ssh_username']); 

$answer = shell_exec("ssh -p " . $port . " " . $user . "@" . $host); 

escapeshellcmd()夠了還是我需要更棘手的東西? 或者我應該在這個例子中使用escapeshellarg()

謝謝。

回答

2

我會說這就是escapeshellarg功能已經創建 - 因此,爲了逃避參數,這是你應該使用的。

基本上:


引用他們各自的單證:

escapeshellcmd()將:
此功能可用於確保所有數據來自用戶的輸入來這個數據傳遞到exec()之前逃脫或system()函數或反引號操作符。

escapeshellarg():
這個函數應用於轉義個體參數殼從用戶輸入來的功能。