2016-05-13 33 views
0

我想知道如何逃避撇號'並使用shell_exec中的變量php。我正在嘗試類似於:PHP shellexec轉義

$output = shell_exec("su - $user -c 'ssh $hosts cat $fic'");  
$output = shell_exec("su - ".escapeshellarg($user)" -c 'ssh ".escapeshellarg($host)" cat ".escapeshellarg($file)"'"); 

這兩個命令都不起作用。

謝謝你的時間。

鮑勃

回答

2

你有可變$user$host過嗎?

<?php        
    $user = 'username';    
    $host = '[email protected]';      
    $output = shell_exec('su - '.$user.' -c \'ssh '.$host.'\''); 
0

假設$user存在於Shell中。它沒有。嘗試執行此:

$output = shell_exec("su - " . $user . " -c 'ssh " . $host . " cat $fic'"); 

輸出命令應該是這樣的

su - username -c 'ssh [email protected] cat $fic' 

無需逃逸字符的。

0

下面的代碼不起作用:

$user = 'Ucheck'; 
$host = '192.168.1.100'; 
$fic = '/tmp/zones'; 
$output = shell_exec("su - " . $user . " -c 'ssh " . $host . " cat " . $fic . "'"); 

的命令外殼作品:

su - Ucheck -c 'ssh 192.168.1.100 cat /tmp/zones'