0

I want to run a script on my ubuntu server with a variable from php in it.

Here are some of the things i've tried to pass a variable to ubuntu...

shell_exec('vpsName=HI3');

vpsName=`mysql -D jake_db -h 127.0.0.1 -u jake -pXXXXXXX -se "SELECT vpsName FROM reinstalls WHERE status = 'pending'"`;

The final way in which i thought I could fix it was to avoid running the script through ubuntu all together and run it from shell_exec(); but it fails on running the guestfish commands.

Here is my entire reinstalls.sh script.

    sudo rm /var/lib/libvirt/images/"$vpsName".qcow2 && sudo wget -O /var/lib/libvi$
guestfish -a /var/lib/libvirt/images/"$vpsName".qcow2 <<'EOF'
 run
 mount /dev/ubuntu-vg/root /
  rm /etc/network/interfaces
EOF
sudo fusermount -u /mnt && virsh start "$vpsName" && echo "IT WORKED!"

I am open to any way of getting this to work, as long as its secure, Thanks in advance, Jake

EDIT:
If I run the script with a VPS name instead of a variable, it works. I just can't find a way to pass the variables from the website to the ubuntu16.04 OS.

1 Answers1

1

Well one way would be to run each command from PHP:

shell_exec("sudo rm /var/lib/libvirt/images/" . $vpsName . ".qcow2");
shell_exec("sudo wget -O /var/lib/libvi ... etc");

The other would be to invoke your shell script from PHP, passing the vps name as a parameter:

shell_exec("reinstalls.sh " . $vpsName)

But then you'd have to rewrite the shell script to pick up the command line parameter and apply it as necessary. In the case of bash, this explains how to go about that.

Community
  • 1
  • 1
Arie B.
  • 290
  • 1
  • 10
  • Thanks, I managed to get it working by passing it as a parameter like you said and doing vpsName=$1. Thanks for the help! – Jake Kirby May 01 '17 at 14:23