本文以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 onclick='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

Web页面执行shell命令的更多相关文章

  1. 通过web php 执行shell脚本,获取的结果与直接在命令行下获取的结果不同。

    公司项目中的一项小功能,统计设备的连接数.其中用到shell脚本来获取已连接设备的统计.使用命令 /bin/netstat -an| grep ESTABLISHED | awk '{print $4 ...

  2. python中执行shell命令的几个方法小结

    原文 http://www.jb51.net/article/55327.htm 最近有个需求就是页面上执行shell命令,第一想到的就是os.system, os.system('cat /proc ...

  3. python中执行shell命令的几个方法小结(转载)

    转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...

  4. c++ cgi执行shell命令l发送qq邮件mailx

    #include <cstdlib> //随机数 #include <iostream> #include <cstdio> //popen函数调用的需要 #inc ...

  5. Linux远程执行Shell命令或脚本

    ## 远程执行shell命令 ssh [user]@[server] '[command]' # eg. ssh root@192.168.1.1 'uptime' ## 远程执行本地shell脚本 ...

  6. vim中执行shell命令

    1):!command   不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容   例如   :!ls -l   特别的可以运行:!bas ...

  7. Android Java执行Shell命令

    最新内容建议直接访问原文:http://www.trinea.cn/android/android-java-execute-shell-commands/ 主要介绍Android或Java应用中如何 ...

  8. subprocess模块还提供了很多方便的方法来使得执行 shell 命令

    现在你可以看到它正常地处理了转义. 注意 实际上你也可以在shell=False那里直接使用一个单独的字符串作为参数, 但是它必须是命令程序本身,这种做法和在一个列表中定义一个args没什么区别.而如 ...

  9. nodejs 执行shell 命令

    有需要从前端操作服务器执行shell命令的需求 建立一个process.js文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 var process =  ...

随机推荐

  1. Karen and Game CodeForces - 816C (暴力+构造)

    On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as fo ...

  2. 福州大学软件工程1816 | W班 第6次作业WordCount成绩排名

    作业链接 WordCount进阶需求 评分细则 本次个人项目分数由两部分组成(博客分满分40分+程序得分满分60分) 博客评分细则(满分60,最终折算为40分) 在文章开头给出结对同学的博客链接.本作 ...

  3. 多线程系列之三:Immutable 模式

    一,什么是Immutable模式?immutable就是不变的,不发生改变的.Immutable模式中存在着确保实例状态不发生变化改变的类.这些实例不需要互斥处理.String就是一个Immutabl ...

  4. php获取URL扩展名

    一切拿代码来说话: 举例:'http://www.sina.com.cn/abc/de/fg.php?id=1': $url = 'http://www.sina.com.cn/abc/de/fg.p ...

  5. Bootstrap知识记录:表单和图片

    一.表单Bootstrap 提供了一些丰富的表单样式供开发者使用.1.基本格式//实现基本的表单样式<form><div class="form-group"&g ...

  6. vue二次实战

    vue爬坑之路 npm uninstall 模块名(删除指定模块) https://www.cnblogs.com/wisewrong/p/6255817.html vue快速入门 https://s ...

  7. day 7-19 Mysql索引原理与查询优化

    一,介绍 1.什么是索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语 ...

  8. 补充:pyhton 2 和3中的beyts类型

    在python2里,bytes == str python2里还有个单独的类型是unicode , 把字符串解码后,就会变成unicode. 既然python2中byets == str,那为什么还要 ...

  9. 关于golang.org/x包问题

    关于golang.org/x包问题 由于谷歌被墙,跟谷歌相关的模块无法通过go get来下载,解决方法: git clone https://github.com/golang/net.git $GO ...

  10. 安装splash

    参考: https://blog.csdn.net/qq_41020281/article/details/82599075