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>
Edited by Ch0cu3r
Link to comment
Share on other sites

Thanks for reply, i want to use the variable inside the exec function not for the echo but for ssh just for easy i use the echo command inside the exec function, I will use your script to edit for my need.

Sorry for the inconvinence.

Edited by shams
Link to comment
Share on other sites

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
 

Edited by shams
Link to comment
Share on other sites

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.

Edited by brandon66
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 user@shell.example.net -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

Edited by shams
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.