Jump to content

How to pass variable to exec command in php script?


shams

Recommended Posts

What do you need to use exec for?

 

If you want to display what the user entered use.

<?php

if(isset($_POST['submit']))
{
    $host = $_POST['host'];
    echo "My hostname is $host";
}

?>
<form mehtod="post">
   Hostname: <input type="text" name="host" />
   <input type="submit" name="submit" />
</form>

I want to use with ssh but it didn't get the variable:

 

-L 9001:$host:80

and get this error:

 

[Wed Nov 20 23:18:10.476065 2013] [:error] [pid 4690] [client 192.168.1.2:35511] PHP Parse error:  syntax error, unexpected '$host' (T_VARIABLE) in /home/www/example.net/htdocs/php/ssht.php on line 8, referer: http://example.net/php/index.html

I tried the all 3 above lines one by one and no one work and this is the script i run:

 

<?php

    $host = $_POST['localhost'];

   exec ("echo 'My host name is $host'");
  

?>

and these are the errors from apache log for the all 3 above lines one by one:

 

[Thu Nov 21 05:17:11.159618 2013] [mpm_prefork:notice] [pid 2749] AH00163: Apache/2.4.6 (Ubuntu) PHP/5.5.3-1ubuntu2 configured -- resuming normal operations
[Thu Nov 21 05:17:11.295630 2013] [core:notice] [pid 2749] AH00094: Command line: '/usr/sbin/apache2'
[Thu Nov 21 05:22:17.744680 2013] [:error] [pid 3410] [client 192.168.1.2:45137] PHP Notice:  Undefined index: localhost in /home/www/example.net/htdocs/php/t1.php on line 3
[Thu Nov 21 05:22:17.744768 2013] [:error] [pid 3410] [client 192.168.1.2:45137] PHP Stack trace:
[Thu Nov 21 05:22:17.744781 2013] [:error] [pid 3410] [client 192.168.1.2:45137] PHP   1. {main}() /home/www/example.net/htdocs/php/t1.php:0
[Thu Nov 21 05:25:08.683203 2013] [:error] [pid 3411] [client 192.168.1.2:45142] PHP Notice:  Undefined index: localhost in /home/www/example.net/htdocs/php/t1.php on line 3
[Thu Nov 21 05:25:08.683268 2013] [:error] [pid 3411] [client 192.168.1.2:45142] PHP Stack trace:
[Thu Nov 21 05:25:08.683287 2013] [:error] [pid 3411] [client 192.168.1.2:45142] PHP   1. {main}() /home/www/example.net/htdocs/php/t1.php:0
[Thu Nov 21 05:33:58.989598 2013] [:error] [pid 3413] [client 192.168.1.2:45191] PHP Notice:  Undefined index: localhost in /home/www/example.net/htdocs/php/t1.php on line 3
[Thu Nov 21 05:33:58.989644 2013] [:error] [pid 3413] [client 192.168.1.2:45191] PHP Stack trace:
[Thu Nov 21 05:33:58.989668 2013] [:error] [pid 3413] [client 192.168.1.2:45191] PHP   1. {main}() /home/www/example.net/htdocs/php/t1.php:0
 

Try this maybe?

<?php

if(isset($_POST['host']))
{
    $host = htmlspecialchars($_POST['host']);
    echo "My hostname is" . $host;
}else{
    echo "Not receiving POST variable";
}

?>

Otherewise it seems like your locahost or host variable is not getting set.

This is the output:

 

Not receiving POST variable

This is the apache error.log:

 

[Thu Nov 21 09:00:28.701411 2013] [mpm_prefork:notice] [pid 2746] AH00163: Apache/2.4.6 (Ubuntu) PHP/5.5.3-1ubuntu2 configured -- resuming normal operations
[Thu Nov 21 09:00:28.754984 2013] [core:notice] [pid 2746] AH00094: Command line: '/usr/sbin/apache2

I want to pass the variable $host to the php script:

 

<form mehtod="post"name="ssht" action="ssht.php">
   Hostname: <input type="text" name="host" />
   <input type="submit" name="submit" />
</form>
 

 

<?php

$host = $_POST['localhost'];

 

exec ("sshpass -p my_pass ssh -L 9001:$host:80 [email protected] -N");

 

?>

The above script didn't get the variable $host, with this error of apache log:

 

Wed Nov 20 23:18:10.476065 2013] [:error] [pid 4690] [client 192.168.1.2:35511] PHP Parse error:  syntax error, unexpected '$host' (T_VARIABLE) in /home/www/example.net/htdocs/php/ssht.php on line 8, referer: http://example.net/php/index.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.