1 year ago

#298341

test-img

Chloe

PHP shell_exec can't change to root directory

i want to do a command to run ansible on linux terminal via web interface with php shell exec. My web interface is in /var/www/html while my ansible playbook is in /root/git/ansible. Here is the directory structure of my linux :

/
├── var
│   └── www
│       └── html
│           └── index.php
└── root
    └── git
        └── ansible
            └── ansible-playbook.yml

as you can see, my php script in index.php so the working directory is in /var/www/html. To run ansible-playbook.yml, i have to change the working directory to /root/git/ansible. However, I can't access /root and can only be on /var.

When i tried shell_exec('cd ; pwd'); the output will be the parent directory which is /var/www . I also tried the shell_exec('cd .. ; cd .. ; cd .. ; cd root/git/ansible ; pwd); expecting to get /root/git/ansible as result, but i got / instead which seems like the cd root/git/ansible command is ignored. I also tried the chdir() before executing shell_exec() but the result is the same. I still can't change to root directory.

I read somewhere that it is for security concerns, but is there a possible way to do it? Thank you so much.

php

shell-exec

0 Answers

Your Answer

Accepted video resources