简体   繁体   中英

edit bash file using php script

I have been trying to run the bash script using php shell_exec() , before that i want to edit the bash variables using php script. Ex: i want to edit this domainname, value, txtdata values in the editdns.sh file before the execution.

Bash script:

#!bin/bash
domainname="mydomain.com"
value="mydomain.com."
txtdata="test2348734"
whmapi1 editzonerecord domain=$domainname line=24 name=$value class=IN ttl=14400 type=TXT txtdata=$txtdata

I suggest to change the shell script to accept command line arguments:

#!bin/bash
domainname=$1
value=$2
txtdata=$3
whmapi1 editzonerecord domain=$domainname line=24 name=$value class=IN ttl=14400 type=TXT txtdata=$txtdata

And then call the script from PHP with the required arguments:

$salida = shell_exec('/path/to/script.sh mydomain.com mydomain.com. test2348734');

Hope this helps !

No need to edit your bash for that. Do not use static values on variables. You can pass arguments to bash by command line.

#!bin/bash
domainname=$1
value=$2
txtdata=$3
whmapi1 editzonerecord domain=$domainname line=24 name=$value class=IN ttl=14400 type=TXT txtdata=$txtdata

better code:

#!bin/bash
whmapi1 editzonerecord domain=$1 line=24 name=$2 class=IN ttl=14400 type=TXT txtdata=$3

so you hav to run it using:

$myresults=shell_exec("editdns.sh mydomain.com mydomain.com. test2348734");

change to the correct path before excecute

$pathkeep = getcwd();  //keep the currect
chdir('/mypath/');    //change to the script path
$data = shell_exec('./editdns.sh var1 var2');    //excecute script
chdir($pathkeep );    //return to prev path

Don't change script, this may make some troubles or security problems. You may read parameters in bash script and in php call bash command with parameters:

#!bin/bash
domainname=$1 # <-- you can validate input here or in php
value=$2 # <-- you can validate input
txtdata=$3 # <-- you can validate input
whmapi1 editzonerecord domain=$domainname line=24 name=$value class=IN ttl=14400 type=TXT txtdata=$txtdata

a then in php command string:

editdns.sh "mydomain.com" "mydomain.com." "test2348734"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM