douliedai4838 2019-05-28 15:35
浏览 112
已采纳

如何将变量从php传递到aed?

I'm have a raspbian running on a raspberry pi3, I'm making a system that requires the person to choose the hostname by typing it into a field. And to swap the hostname, I need to change two files inside the /etc/ folder.

I have already managed to make these changes by the sed command, however since I want the person to choose the hostname, I made a form for PHP and wanted to take this variable by POST and pass it on to the sed command.

<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
    ');

    }

?>

Only {} appears in the file that is modified. Would anyone know what I could change?

  • 写回答

2条回答 默认 最新

  • duansai1314 2019-05-28 15:43
    关注

    In PHP you need to use double quotes for a string if you want to pass a variables into it.

    Additionally if you want to pass user input as a shell script argument, you need to sanitize it, otherwise user will be able to execute any shell script on your machine. In PHP you can use escapeshellarg for this.

    Finally, your code should look like this:

    $HOSTNAME=escapeshellarg("s/nome/$SEU_HOSTNAME/");
    shell_exec ("
        cd /etc/
        sudo sed -i $HOSTNAME hosts
        sudo sed -i $HOSTNAME hostname
    ");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题