PHP shellexec逃脱

时间:2016-05-13 06:37:48

标签: php

我想知道如何逃避撇号'并使用shell_execphp的变量。我正在尝试类似的事情:

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

这两个命令都不起作用。

感谢您的时间。

鲍勃

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

您假设Shell中存在$user。它没有。尝试执行此操作:

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

输出命令应该是这样的

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

不需要逃脱字符。

答案 2 :(得分:0)

以下代码不起作用:

$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'