Web页面执行shell命令

本文以apache为web服务器为例

  1. 安装apache服务
    yum -y install httpd
  2. 启动apache
    systemctl restart httpd
  3. 创建shell脚本
    cd /var/www/cgi-bin/
    vim shell
    #!/bin/sh  
    alias urldecode='sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b"'  
    echo -e "Content-type: text/plain\n"  
    decoded_str=`echo $QUERY_STRING | urldecode`  
    echo -e "`$decoded_str` \n"
    shell
    测试:在浏览器中输入http://127.0.0.1/cgi-bin/shell?pwd,即可列出目录
  4. 提供web接口
    cd /var/www/html
    vim index.html
    <html>
    <head>
    <script>
    function httpGet(url)  
    {  
            var xmlHttp = new XMLHttpRequest();  
            xmlHttp.open("GET", url, false); // false: wait respond  
            xmlHttp.send(null);  
            return xmlHttp.responseText;  
    }  
    function f()  
    {  
            var url = "http://127.0.0.1/cgi-bin/shell?"+ document.getElementById('in').value;  
            document.getElementById('out').innerHTML = httpGet(url);  
    }  
    </script>
    </head>
    <body>
    <span>command: </span>
    <input id='in'></input>
    <button οnclick='f()'>send</button>
    <br/>
    <pre id='out'></pre>
    </body>
    </html>
    index.html

    注意修改代码中ip,更改为服务器ip或域名

  5. 效果图如图所示
  6. cgi-bin目录执行shell脚本格式
    #!/bin/sh
    printf "Content-Type: text/plain\n\n"
    your_commands_here
  7. 安全性优化
    限制用户访问cgi-bin目录,修改/etc/httpd/conf/httpd.conf
    <Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
    Deny From all
    Allow From 127.0.0.1 your-ip-address
    </Directory>
    配置http页面账号密码访问,也可实现安全性
  8. 弊端
    无法执行复杂的脚本命令,如带有" |等特殊符号的命令无法执行,如yum、top命令执行结果不完整、仅适用于简单带输出脚本命令,脚本运行账号为apache

转载于:https://www.cnblogs.com/Mrhuangrui/p/7798028.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值