PHP parametric + bash script combination


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PHP parametric + bash script combination
# 1  
Old 04-03-2017
PHP parametric + bash script combination

Hello,

I have a simple bash script and I manually run this script to put a file into related directory in apache normally. I need to run it in php with parameter.
My target is to get a download link by running below script but I do not know anything about php. Here is my bash script:

run.sh
Code:
#!/bin/bash
cd /root
u=$a
p=$b
mkdir /var/www/html/$u
mkdir /var/www/html/$u/$p
cp index.php /var/www/html/$u/index.php
cp index.php /var/www/html/$u/$p/index.php
cp list.txt /var/www/html/$u/$p/list.txt
#???
#???
#???
echo "your download link is" http://public_ip/download.php?user=$u&pass=$p&file=list.txt"

If that folder name $u, and $p matches it will allow the user to download list.txt file.

It will redirect the user to:
Code:
http://public_ip/$u/$p/list.txt

After installing php, I will give permissions for user to run bash script
My question is: what should be the php file?
I'd appreciate if you could lead me right direction or give an idea about the solution.

Thanks in advance
Boris
# 2  
Old 04-03-2017
Need to run it with what parameter?
# 3  
Old 04-05-2017
Hello Corona688,

For example when the user enters below link,

Code:
http://public_ip/download.php?user=user1&pass=file1&file=list.txt

Browser should be redirected to:

Code:
http://public_ip/user1/file1/list.txt


OR:


when below url is requested:

Code:
http://public_ip/download.php?user=mike&pass=mike&file=list455.txt

redirect to:

Code:
http://public_ip/mike/mike/list455.txt


Thanks in advance
Boris
# 4  
Old 04-05-2017
OK:

Code:
<?php

        $p=array("user","pass","file"); // List of arguments we want to check.

        // A very handy loop PHP has.  It will loop three times, with 
        // $x set to "user", "pass", and "file" in turn.
        foreach($x in $p)
        {
                // Check if they exist in the global array $_GET
                if(!array_key_exists($x, $_GET)) 
                {
                        printf("<h2>Missing Argument %s</h2>\n", $x);
                        exit(0);
                }

                // Reject forward slashes in all passwords, names, and files
                if(strchr($_GET[$x], "/")) 
                {
                        printf("<h2>Injection Attempt in %s</h2>\n", $x);
                        exit(0);
                }
        }

        header(sprintf("Location:  http://public_ip/%s/%s/%s", $_GET["user"], $_GET["pass"], $_GET["file"]));

?>

I don't seriously recommend this password protection scheme, of course. It's just something to start playing with.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 07-06-2017
Hello,
Stucked in above php code
I've manually created test folder and test2 subdirectory under /var/www/html
test.txt exists at /var/www/html/test/test2/test.txt
Then I ve run below url in Chrome but gives error 500, "this page is not working"

URL:
My test.php file:

Code:
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php
        $p=array("user","pass","file"); // List of arguments we want to check.

        // A very handy loop PHP has.  It will loop three times, with 
        // $x set to "user", "pass", and "file" in turn.
        foreach($x in $p)
        {
                // Check if they exist in the global array $_GET
                if(!array_key_exists($x, $_GET)) 
                {
                        printf("<h2>Missing Argument %s</h2>\n", $x);
                        exit(0);
                }

                // Reject forward slashes in all passwords, names, and files
                if(strchr($_GET[$x], "/")) 
                {
                        printf("<h2>Injection Attempt in %s</h2>\n", $x);
                        exit(0);
                }
        }

        header(sprintf("Location:  http://my_public_ip/%s/%s/%s", $_GET["user"], $_GET["pass"], $_GET["file"]));
?>
 </body>
</html>

I'd appreciate if you could let me know what I am missing

Thanks
Boris
# 6  
Old 07-06-2017
Welcome back.

I made some typos in that.
Code:
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php
        $x=array("user","pass","file"); // List of arguments we want to check.

        // A very handy loop PHP has.  It will loop three times, with
        // $p set to "user", "pass", and "file" in turn.
        foreach($x as $p)
        {
                // Check if they exist in the global array $_GET
                if(!array_key_exists($p, $_GET))
                {
                        printf("<h2>Missing Argument %s</h2>\n", $p);
                        exit(0);
                }

                // Reject forward slashes in all passwords, names, and files
                if(strchr($_GET[$p], "/"))
                {
                        printf("<h2>Injection Attempt in %s</h2>\n", $p);
                        exit(0);
                }
        }

        header(sprintf("Location:  http://my_public_ip/%s/%s/%s", $_GET["user"], $_GET["pass"], $_GET["file"]));

?>
 </body>
</html>


Last edited by Corona688; 07-06-2017 at 12:52 PM..
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 07-06-2017
Hello Corona,
Many thanks
Works fine.

Kind regards
Boris
This User Gave Thanks to baris35 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Passing variable from PHP to bash script

I am totally new to PHP and I am trying to create a script that will as a user for a hostname and then use the "hostname" variable to generate a report using REST API. I am able to create the html script and php script to GET the "hostname" but I am having trouble passing the hostname variable... (10 Replies)
Discussion started by: kieranfoley
10 Replies

2. Shell Programming and Scripting

Basic Combination Shell Script

I need to have a script read a file that has a list of words in a single column like below:Black Blue Brown Orange Red Yellow Green White Purple Silver Grey Tan Then print to another file just all of the two-word possible combinations. Example: Black,Blue Anyone want to take a... (4 Replies)
Discussion started by: vespasian
4 Replies

3. Shell Programming and Scripting

Arbitrary permutation and combination script

#!/bin/bash # permutation_combination.sh # Version: 2.0 # Author : YongYe <complex.invoke@gmail.com> arg0=-1 argv=${3} number=${2} eval ary=({1..${1}}) length=${#ary} percom(){ nsloop i ${1} number${2} ${3} ${4} ${5}; } invoke(){ echo $(percom ${argu} nsloop -1) prtcom $(percom... (1 Reply)
Discussion started by: complex.invoke
1 Replies

4. Shell Programming and Scripting

Select combination unique using shell script

Hi All, bash-3.00$ gzgrep -i '\ ExecuteThread:' /******/******/******/******/stdout.log.txt.gz <Jan 7, 2012 5:54:55 PM UTC> <Error> <WebLogicServer> <BEA-000337> < ExecuteThread: '414' for queue: 'weblogic.kernel.Default (self-tuning)' has been busy for "696" seconds working on the request... (4 Replies)
Discussion started by: osmanux
4 Replies

5. Shell Programming and Scripting

Detecting key combination in bash

I need to grab key combination from a bash script and store it in a variable. Is there any way to do this? Thanks in advance! (1 Reply)
Discussion started by: prism1
1 Replies

6. UNIX for Dummies Questions & Answers

Integrating bash script into php (page)

I have written a bash script...now i need to call the script from php page. Can you give me an example to demonstrate how it is done?:( (1 Reply)
Discussion started by: xerox
1 Replies

7. Shell Programming and Scripting

bash script + php

Hello, I want to move some of my bash scripts to php ( add samba user, add new PC in dhcp and etc. ).With Google, I found some articles for bash and php, but there is only simple examples, nothing for variables. Example: #!/bin/bash status=`/usr/local/etc/rc.d/isc-dhcpd status` ############... (1 Reply)
Discussion started by: mrowcp
1 Replies

8. Shell Programming and Scripting

awk - getline from parametric file name

Hi! I have an input file for an awk script that I need to split into several files and the process them separately line by line. I have splitted the input file into the other files, that have been created correctly. But, since their names are parametric (i.e. output_1.txt, output_2.txt..... (2 Replies)
Discussion started by: Alice236
2 Replies

9. Shell Programming and Scripting

Combination of case and If else in shell script

Would it be right forme to combine case statement and if else in one shell script? Would it work? (2 Replies)
Discussion started by: Pauline mugisha
2 Replies

10. Shell Programming and Scripting

Combination backup/VI script

I was needing a script that basically covers my butt, lol. I have the nasty habit of making many changes without backing up files, and then after a ton of changes, if I have to go back to the original version, it makes it harder to go back. Now, I have a script that takes a file, renames it to... (3 Replies)
Discussion started by: cbo0485
3 Replies
Login or Register to Ask a Question