Variable via POST to sed

Asked

Viewed 60 times

0

I’m with a Raspbian running on a Raspberry Pi3, I’m making a system on top of it that needs the person to choose the hostname typing it into a field. And to change the hostname from rasp I need to change two files inside the folder etc. I have already managed to make these changes by command sed, but how do I want the person to choose the hostname, I made a form by PHP and I wanted to pick up via POST this variable and pass it to the command sed. Follows the codes.

<link rel="stylesheet" type="text/css" href="css/style.css">

<form method="POST">

<input  type="text" name="hostname">
<input type="submit" name="Comentar" value=" Salvar">

</form>
<?php 

if(isset($_POST['Comentar'])){   
        echo "botão foi clicado"."<br/>";
        $SEU_HOSTNAME = $_POST["hostname"];
        echo  "hostname digitado: ".$SEU_HOSTNAME; 


    shell_exec ('

        cd /etc/

        sudo sed -i "s/nome/${SEU_HOSTNAME}/" hosts
        sudo sed -i "s/nome/${SEU_HOSTNAME}/" hostname


    ');

    }

?>

In doing so, only the { } appear in the modifying file. Would anyone know what that could be?

  • Osmar, do not post code as image, the site has support for the codes, just use them. You can do the [tour] to learn the basics of the site, read the guide of [Ask] and access the [help].

2 answers

0


Hello,

In order for the PHP file to be able to change the file, it must be run as sudo and try to change the code to the following:

$command1 = 'sudo sed -i "s/nome/'.$SEU_HOSTNAME.'/" hosts';
$command2 = 'sudo sed -i "s/nome/'.$SEU_HOSTNAME.'/" hostname';

shell_exec("cd /etc/");
shell_exec($command1);
shell_exec($command2);

I hope I helped, hug!

0

Doing this way I also got results. After testing the above code, I searched a little more and found this solution.

$HOSTNAME=escapeshellarg("s/nome/$SEU_HOSTNAME/");
shell_exec ("
    cd /etc/
    sudo sed -i $HOSTNAME hosts
    sudo sed -i $HOSTNAME hostname
");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.