0

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
  • This is not going to work like this, at the very least you need to set the action attibute to a path that is accessible by the web-server, in your case probably: /cgi-bin/mode2.sh. Then the web-server will probably just show you mode2.sh as a text file, unless you have some special configuration that will allow the web-server to execute shell scripts. Commented Dec 18, 2013 at 12:12
  • my sample php, i cut my script.any help? Commented Dec 18, 2013 at 12:14

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

i have tried to follow your step, still cant send the value to bash. UserInputIPAddrPHP.php sript : $sh="/path/to/mode2.sh ".escapeshellarg($IP_Addr)." ".escapeshellarg($NetMask); exec ($sh,$output=array(),$return_value); bash script : echo =echo "IPADDR=$1" >>/etc/sysconfig/network-scripts/ifcfg-eth0 echo "NETMASK=$2" >>/etc/sysconfig/network-scripts/ifcfg-eth0
@Haruru Please find the below script to redirect the user input(IP,NetMask) to file /etc/sysconfig/network-scripts/ifcfg-eth0 IP="$1"; NetMask="$2"; echo "$IP" >>/etc/sysconfig/network-scripts/ifcfg-eth0 echo "$NetMask" >>/etc/sysconfig/network-scripts/ifcfg-eth0 Hope this might help you Thanks
Also try to print the values of variables before sending to bash scripts i.e print $IP and $Netmask
@Haruru Alwys Welcome. Any help please let me know
0

You should not use bash as cgi script. But if there is really a need, you can get IP and netmask by manipulating variable $QUERY_STRING

#!/bin/bash
echo Content-type:text/plain
echo ""

read IP MASK <<< $(echo $QUERY_STRING |  sed -r 's/&?ip2?=/ /g')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.