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条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作