rmo rissabikis
rmo rissabikis

Reputation: 29

PHP shellexec escape

I was wondering how can I escape the apostrophe ' and use variable from shell_exec in php. I am trying something like :

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

Both commands doesn't work.

Thank you for your time.

Bob

Upvotes: 0

Views: 100

Answers (3)

rmo rissabikis
rmo rissabikis

Reputation: 29

The following code does not work :

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

The command in shell works :

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

Upvotes: 0

Juanjo Salvador
Juanjo Salvador

Reputation: 1093

You are assuming that $user exists in Shell. It doesn't. Try executing this:

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

Output command should be something like this

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

No need of escape chars.

Upvotes: 0

Ivan
Ivan

Reputation: 2579

you have variable $user and $host before?

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

Upvotes: 2

Related Questions