I have a script that runs when I click a button on the http server on my linux machine. The file is in cgi-bin and is executable.
我有一个脚本,当我点击我的linux机器上的http服务器上的按钮时运行。该文件位于cgi-bin中并且是可执行的。
But it seems like two lines of this script arent working
但似乎这个脚本的两行不起作用
#!/bin/bash
IPADDR=`echo "$QUERY_STRING" | sed -n 's/^.*IPADDR=\([^&]*\).*$/\1/p' | sed "s/+/ /g"`
SUBNET=`echo "$QUERY_STRING" | sed -n 's/^.*SUBNET=\([^&]*\).*$/\1/p' | sed "s/+/ /g"`
DHCP=`echo "$QUERY_STRING" | sed -n 's/^.*DHCP=\([^&]*\).*$/\1/p' | sed "s/+/ /g"`
(2lines that arent working)
(2行不起作用)
sed -i.bak "s/IPADDR=.*/IPADDR=$IPADDR/g" test
sed -i.bak "s/NETMASK=.*/NETMASK=$SUBNET/g" test
echo "Content-type: text/html"
echo ""
DHCP=`echo "$QUERY_STRING" | sed -n 's/^.*DHCP=\([^&]*\).*$/\1/p' | sed "s/+/ /g"`
echo "<html><head><title>IP CHANGED</title></head>"
echo "<body>IP changed to: "
echo "$IPADDR <br>"
echo "<body>SUBNET changed to: "
echo "$SUBNET <br>"
echo "DHCP $DHCP"
echo "</body></html>"
Contents of file test
文件测试的内容
DEVICE=p32p1
BOOTPROTO=static
DHCPCLASS=
HWADDR=00:01:2e:48:f0:f3
IPADDR=3333333333
NETMASK=4444444444
ONBOOT=yes
sed never changes the file.
sed从不更改文件。
Also here is my HTML
这也是我的HTML
<form action="cgi-bin/IPChange.sh" method="get">
Enter an IP Address: <input type="text" name="IPADDR"></input><br>
Enter a Subnet Mask: <input type="text" name="SUBNET"></input><br>
<input type="radio" name="DHCP" value="on">Enable DHCP
<input type="radio" name="DHCP" value="off">Disable DHCP<br>
<input type="submit" name="subbtn" value="Submit">
<form>
1 个解决方案
#1
1
This must be a combination of filesystem permissions and SELinux configuration problem.
这必须是文件系统权限和SELinux配置问题的组合。
The filesystem part is easier to fix. Your web server process probably runs as the apache
user, so make sure it has the correct permissions to the file you want to rewrite. To confirm the filesystem permissions are ok, temporarily disable SELinux and check if the writing works:
文件系统部分更容易修复。您的Web服务器进程可能以apache用户身份运行,因此请确保它对您要重写的文件具有正确的权限。要确认文件系统权限是否正常,请暂时禁用SELinux并检查写入是否有效:
echo 0 >/selinux/enforce
If this works, then turn SELinux back on:
如果这样做,那么重新打开SELinux:
echo 1 >/selinux/enforce
and then fix your SELinux settings. Here are some hints for that from this other answer, in particular:
然后修复SELinux设置。以下是其他答案中的一些提示,特别是:
You must either give the directory structure a context of
httpd_sys_rw_content_t
, or give them a context ofpublic_content_rw_t
and enableallow_httpd_anon_write
and/orallow_httpd_sys_script_anon_write
. See thehttpd_selinux(8)
man page for details.您必须为目录结构提供httpd_sys_rw_content_t的上下文,或者为它们提供public_content_rw_t的上下文并启用allow_httpd_anon_write和/或allow_httpd_sys_script_anon_write。有关详细信息,请参见httpd_selinux(8)手册页。
In addition, I would rewrite your script like this:
另外,我会像这样重写你的脚本:
#!/bin/bash
config=/var/www/net.config
while IFS== read name value; do
case $name in
IPADDR) IPADDR=$value ;;
SUBNET) SUBNET=$value ;;
DHCP) DHCP=$value ;;
esac
done < <(sed -e 's/&/\n/g' <<< $QUERY_STRING)
is_valid() {
# todo: validate the input params
return 1
}
if is_valid; then
sed -i.bak e "s/IPADDR=.*/IPADDR=$IPADDR/g" -e "s/NETMASK=.*/NETMASK=$SUBNET/g" $config
title='IP CHANGED'
body=$(cat << EOF
IP changed to: $IPADDR <br>
SUBNET changed to: $SUBNET <br>
DHCP $DHCP
EOF
)
else
body='What are you trying to pull, mister?'
fi
cat << EOF
Content-type: text/html
<html><head><title>$title</title></head>
<body>$body</body></html>
EOF
#1
1
This must be a combination of filesystem permissions and SELinux configuration problem.
这必须是文件系统权限和SELinux配置问题的组合。
The filesystem part is easier to fix. Your web server process probably runs as the apache
user, so make sure it has the correct permissions to the file you want to rewrite. To confirm the filesystem permissions are ok, temporarily disable SELinux and check if the writing works:
文件系统部分更容易修复。您的Web服务器进程可能以apache用户身份运行,因此请确保它对您要重写的文件具有正确的权限。要确认文件系统权限是否正常,请暂时禁用SELinux并检查写入是否有效:
echo 0 >/selinux/enforce
If this works, then turn SELinux back on:
如果这样做,那么重新打开SELinux:
echo 1 >/selinux/enforce
and then fix your SELinux settings. Here are some hints for that from this other answer, in particular:
然后修复SELinux设置。以下是其他答案中的一些提示,特别是:
You must either give the directory structure a context of
httpd_sys_rw_content_t
, or give them a context ofpublic_content_rw_t
and enableallow_httpd_anon_write
and/orallow_httpd_sys_script_anon_write
. See thehttpd_selinux(8)
man page for details.您必须为目录结构提供httpd_sys_rw_content_t的上下文,或者为它们提供public_content_rw_t的上下文并启用allow_httpd_anon_write和/或allow_httpd_sys_script_anon_write。有关详细信息,请参见httpd_selinux(8)手册页。
In addition, I would rewrite your script like this:
另外,我会像这样重写你的脚本:
#!/bin/bash
config=/var/www/net.config
while IFS== read name value; do
case $name in
IPADDR) IPADDR=$value ;;
SUBNET) SUBNET=$value ;;
DHCP) DHCP=$value ;;
esac
done < <(sed -e 's/&/\n/g' <<< $QUERY_STRING)
is_valid() {
# todo: validate the input params
return 1
}
if is_valid; then
sed -i.bak e "s/IPADDR=.*/IPADDR=$IPADDR/g" -e "s/NETMASK=.*/NETMASK=$SUBNET/g" $config
title='IP CHANGED'
body=$(cat << EOF
IP changed to: $IPADDR <br>
SUBNET changed to: $SUBNET <br>
DHCP $DHCP
EOF
)
else
body='What are you trying to pull, mister?'
fi
cat << EOF
Content-type: text/html
<html><head><title>$title</title></head>
<body>$body</body></html>
EOF