doujiaci7976 2013-12-18 12:08
浏览 95
已采纳

如何将文本框值传递给bash脚本

This is my first question, CMIIW :

here my sample php script

<form method="GET" action="/var/www/cgi-bin/mode2.sh">
     <table align="center" nowrap>
<tr>><td>IP address :</TD><TD><input type="text" name="ip"></td></tr>
<tr><td>netmask :</TD><TD><input type="text" name="ip2"></td></tr>
</tr></table>

 <input type="submit" value="Send">
 <input type="reset" value="Reset"></p></form>

bash script :

#!/bin/bash -x

echo "IPADDR=$ip"       >>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "NETMASK=$ip2"     >>/etc/sysconfig/network-scripts/ifcfg-eth0

I want input textbox value form web browser for example IP and netmask and send the value to bash and save to ifcfg-eth0 file. i know it sound risky, i just want to learn. any sugestion?

  • 写回答

2条回答 默认 最新

  • duanjiuhong5843 2013-12-18 12:58
    关注

    Instead of having action attribute point to shell script you want to run, Make action attribute to point to php file like

    <form method="GET" action="UserInputIPAddrPHP.php">
    

    And in the UserInputIPAddrPHP.php you can get the User Inputted IP and Netmask as follows

    <html>
    <body>
    <?php
    $IP_Addr = $_GET['ip'];
    $NetMask = $_GET['ip2'];
    $command="/path/to/mode2.sh ".escapeshellarg($IP_Addr)." ".escapeshellarg($NetMask);
    exec ($command,$output=array(),$return_value);
    if($return_value!==0) {
        #print appropriate message
    }
    
    ?>
    </body>
    </html>
    

    $command is actual command that you type to run the script at your shell. So set it accordingly.

    Write your script mode2.sh like this (mind the quotes):

    #!/bin/sh
    command "$1" "$2"; # command is actual command you want to run like cp,mv etc
    

    Render it executable:

    me@somewhere$ chmod +x mode2.sh
    

    Hope this might help you

    Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效