PHP shellexec escape


PHP shellexec escape

我想知道如何转义撇号'并在php中使用shell_exec中的变量。我正在尝试类似的东西:

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

两个命令都不起作用。

谢谢你抽出时间。

Bob

您之前有变量$user$host吗?

<?php                            
    $user = 'username';             
    $host = 'userForLogin@192.168.0.1';                    
    $output = shell_exec('su - '.$user.' -c ''ssh '.$host.'''');

您假设$user存在于Shell中。事实并非如此。尝试执行以下操作:

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

输出命令应该类似于

su - username -c 'ssh user@server cat $fic'

不需要转义字符。

以下代码不起作用:

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

shell中的命令有效:

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