CGI脚本跳过源命令

时间:2015-10-22 15:27:13

标签: bash shell cgi

我正在尝试编写一个写一个html文件的cgi,所以每次我想查看这些信息时都不必运行该脚本。

#!/bin/bash

echo "Content-type: text/html"
echo ""
echo "<HTML>"
echo "<head><title>Status</title></head>"
echo "<body>"
./auto.sh > ../secure/status.html
echo "Redirecting..."
echo "<script language="javascript">window.location.href="../secure/status.html";</script>"
echo "</body></HTML>"

但是,当我将浏览器定向到页面时,页面源会显示此信息,但它没有运行重定向命令,甚至没有重定向......

<HTML>
<head><title>Status</title></head>
<body>
Redirecting...
<script language=javascript>window.location.href = ../secure/status.html;</script>
</body></HTML>

我做错了什么? 谢谢!

编辑:这是我的auto.sh

#!/bin/bash

echo "<HTML><head><title>Status</title></head><body><pre>"
echo "$(date)"
echo ""
FAILED=$(sudo cat /var/log/secure | grep "$(date +"%b %d")" | grep "sshd" | grep "Failed")
ACCEPTED=$(sudo cat /var/log/secure | grep "$(date +"%b %d")" | grep "sshd" | grep "Accepted")
SPEED=$(python /script/speedtest-cli --server 4128 | grep "Download:\|Upload:")
BAN=$(sudo cat /var/log/fail2ban.log | grep "$(date +"%Y-%m-%d")" | grep "Ban")

echo "##########FAILED##########"
echo "$FAILED"
echo ""
echo "##########ACCEPTED##########"
echo "$ACCEPTED"
echo ""
echo "##########BANNED##########"
echo "$BAN"
echo ""
echo "##########SPEED##########"
echo "$SPEED"
echo ""
echo "##########STORAGE##########"
echo "$(df -h)"
echo ""
echo "##########MEMORY##########"
echo "$(free -m)"
echo ""
echo "<a href="../cgi-bin/status.cgi">Refresh</a>"
echo "</pre></body></HTML>"

1 个答案:

答案 0 :(得分:0)

我会将重定向代码(包括脚本标记)放在文档的<head>中。

我还会修改bash脚本,以便将目标放在双引号中(将输出呈现为)window.location.href="../secure/status.html"; 单引号也可以。看来你的bash脚本使用引号并没有像你想要的那样引用这些项目。

最后,如果您希望用户实际看到“重定向”字样,您应该在javascript代码中调用某种时间延迟。

尝试: echo "<script type='text/javascript'>window.location.href='../secure/status.html';</script>"

我获取了auto.sh文件的一小部分,然后将其输出重定向到文件。

#!/bin/bash
# subset of your file auto.sh

echo "<HTML><head><title>Status</title></head><body><pre>"
echo "$(date)"
echo ""
FAILED="Failed"
ACCEPTED="Accepted"
BAN="Ban"
echo "##########FAILED##########"
echo "$FAILED"
echo ""
echo "##########ACCEPTED##########"
echo "$ACCEPTED"
echo ""
echo "##########BANNED##########"
echo "$BAN"
echo ""
echo '<a href="../cgi-bin/status.cgi">Refresh</a>'
echo "</pre></body></HTML>"

请注意href行中的单引号。

然后,我在命令行上执行了这个: sh auto.sh > status.html

最后,我执行了cat status.html并得到了这个......

<HTML><head><title>Status</title></head><body><pre>
Thu, Oct 22, 2015  3:09:43 PM

##########FAILED##########
Failed

##########ACCEPTED##########
Accepted

##########BANNED##########
Ban

<a href="../cgi-bin/status.cgi">Refresh</a>
</pre></body></HTML>

这是你想要做的吗?